feat: prevent reversing directly into snake neck and play denied sound effect
This commit is contained in:
1 parent
60740a53c2
commit
fd87ad3091
7 files changed
+89
-19
No files matched your search
+66
-13
@@ -4,6 +4,8 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include "consts.h"
|
||||
|
||||
bool handle_input_frame(InputContext &ctx) {
|
||||
scanKeys();
|
||||
const uint32_t keys = keysDown();
|
||||
@@ -69,25 +71,76 @@ bool handle_input_frame(InputContext &ctx) {
|
||||
|
||||
if (!ctx.state->paused && !ctx.state->gameOver) {
|
||||
const bool cursed = ctx.state->curseTicksRemaining > 0;
|
||||
uint16_t requested_dir = 0;
|
||||
bool has_dir_input = false;
|
||||
|
||||
if ((keys != 0U) && ((keys & KEY_UP) != 0U)) {
|
||||
ctx.state->snakeDirection = cursed ? 2 : 0;
|
||||
draw_game(*ctx.state, *ctx.renderAssets);
|
||||
requested_dir = cursed ? 2 : 0;
|
||||
has_dir_input = true;
|
||||
} else if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
|
||||
requested_dir = cursed ? 3 : 1;
|
||||
has_dir_input = true;
|
||||
} else if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
|
||||
requested_dir = cursed ? 0 : 2;
|
||||
has_dir_input = true;
|
||||
} else if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
|
||||
requested_dir = cursed ? 1 : 3;
|
||||
has_dir_input = true;
|
||||
}
|
||||
|
||||
if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
|
||||
ctx.state->snakeDirection = cursed ? 3 : 1;
|
||||
draw_game(*ctx.state, *ctx.renderAssets);
|
||||
}
|
||||
if (has_dir_input) {
|
||||
bool is_reversing = false;
|
||||
|
||||
if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
|
||||
ctx.state->snakeDirection = cursed ? 0 : 2;
|
||||
draw_game(*ctx.state, *ctx.renderAssets);
|
||||
}
|
||||
if (ctx.state->snake.size() >= 2) {
|
||||
const auto &head = ctx.state->snake.front();
|
||||
const auto &neck = ctx.state->snake.at(1);
|
||||
int next_x = head[0];
|
||||
int next_y = head[1];
|
||||
|
||||
if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
|
||||
ctx.state->snakeDirection = cursed ? 1 : 3;
|
||||
draw_game(*ctx.state, *ctx.renderAssets);
|
||||
switch (requested_dir) {
|
||||
case 0:
|
||||
next_y--;
|
||||
break;
|
||||
case 1:
|
||||
next_x++;
|
||||
break;
|
||||
case 2:
|
||||
next_y++;
|
||||
break;
|
||||
case 3:
|
||||
next_x--;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (next_x > X_MAX) {
|
||||
next_x = 0;
|
||||
} else if (next_x < 0) {
|
||||
next_x = X_MAX;
|
||||
}
|
||||
|
||||
if (next_y > Y_MAX) {
|
||||
next_y = 0;
|
||||
} else if (next_y < 0) {
|
||||
next_y = Y_MAX;
|
||||
}
|
||||
|
||||
if (next_x == neck[0] && next_y == neck[1]) {
|
||||
is_reversing = true;
|
||||
}
|
||||
} else {
|
||||
if ((ctx.state->snakeDirection + 2) % 4 == requested_dir) {
|
||||
is_reversing = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_reversing) {
|
||||
audio_play_denied(*ctx.audio);
|
||||
} else if (ctx.state->snakeDirection != requested_dir) {
|
||||
ctx.state->snakeDirection = requested_dir;
|
||||
draw_game(*ctx.state, *ctx.renderAssets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user