Files
snakePlus/source/game_input.cpp
T
phillip 0e7477031f Add cursed apple: a status-effect apple that inverts controls
Eating a cursed apple inverts the snake's steering for
CURSE_DURATION_TICKS ticks, shown on the top screen while active. The
sprite is a recolored copy of the rotten-apple tile since all existing
sprite tiles are already in use.
2026-09-03 22:27:22 +02:00

94 lines
2.7 KiB
C++

#include "game_input.h"
#include <bitset>
#include <cstdint>
#include <cstdio>
bool handle_input_frame(InputContext &ctx) {
scanKeys();
const uint32_t keys = keysDown();
if ((keys != 0U) && *ctx.internals) {
consoleSelect(ctx.topScreen);
std::string key_str = "\x1b[6;0Hkeys = " + std::bitset<16>(keys).to_string();
puts(key_str.c_str());
}
if (((keys & KEY_START) != 0U) && ((keys & KEY_L) != 0U)) {
return false;
}
if ((keys != 0U) && ((keys & KEY_SELECT) != 0U)) {
*ctx.internals = !(*ctx.internals);
if (*ctx.internals) {
setBackdropColor(RGB15(0, 0, 7));
} else {
setBackdropColor(RGB15(0, 0, 0));
consoleSelect(ctx.topScreen);
consoleClear();
}
}
if (!ctx.state->paused && !ctx.state->gameOver) {
const bool cursed = ctx.state->curseTicksRemaining > 0;
if ((keys != 0U) && ((keys & KEY_UP) != 0U)) {
ctx.state->snakeDirection = cursed ? 2 : 0;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_RIGHT) != 0U)) {
ctx.state->snakeDirection = cursed ? 3 : 1;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_DOWN) != 0U)) {
ctx.state->snakeDirection = cursed ? 0 : 2;
draw_game(*ctx.state, *ctx.renderAssets);
}
if ((keys != 0U) && ((keys & KEY_LEFT) != 0U)) {
ctx.state->snakeDirection = cursed ? 1 : 3;
draw_game(*ctx.state, *ctx.renderAssets);
}
}
if (ctx.state->paused) {
if ((keys != 0U) && ((keys & KEY_X) != 0U)) {
audio_toggle_music(*ctx.audio);
}
if ((keys != 0U) && ((keys & KEY_Y) != 0U)) {
ctx.state->hardMode = !ctx.state->hardMode;
ctx.init_game();
draw_game(*ctx.state, *ctx.renderAssets);
}
}
if ((keys != 0U) && ((keys & KEY_R) != 0U)) {
lcdSwap();
}
if ((keys != 0U) && ((keys & KEY_START) != 0U)) {
if (!ctx.state->gameOver) {
ctx.state->paused = !ctx.state->paused;
if (ctx.state->paused) {
consoleSelect(ctx.topScreen);
audio_play_pause(*ctx.audio);
audio_pause_music_if_enabled(*ctx.audio);
puts("\x1b[11;12HPaused");
} else {
audio_resume_music_if_enabled(*ctx.audio);
audio_play_unpause(*ctx.audio);
consoleSelect(ctx.topScreen);
consoleClear();
}
} else {
ctx.init_game();
}
}
return true;
}