fix: input delay workaround

This commit is contained in:
dark98
2026-09-25 21:14:07 +01:00
parent 31b93c2a9a
commit ea3ccd3b83
4 changed files with 60 additions and 65 deletions
+40 -36
View File
@@ -13,21 +13,27 @@ typedef enum {
} game_state_t;
static game_state_t s_game_state = GAME_STATE_READY;
static int16_t s_player_y = GROUND_Y;
static int16_t s_player_vy = 0;
/* Fixed-point 8-bit physics for smooth 60 FPS jump arcs */
#define FP_SHIFT 8
#define TO_FP(val) ((int32_t)((val) << FP_SHIFT))
#define FROM_FP(val) ((int16_t)((val) >> FP_SHIFT))
static int32_t s_player_y_fp = TO_FP(GROUND_Y);
static int32_t s_player_vy_fp = 0;
static bool s_is_jumping = false;
static int16_t s_obs_x = 72;
static int32_t s_obs_x_fp = TO_FP(72);
static int16_t s_obs_h = 6;
static uint16_t s_score = 0;
static uint16_t s_high_score = 0;
static void game_reset(void)
{
s_player_y = GROUND_Y;
s_player_vy = 0;
s_player_y_fp = TO_FP(GROUND_Y);
s_player_vy_fp = 0;
s_is_jumping = false;
s_obs_x = 72;
s_obs_x_fp = TO_FP(72);
s_obs_h = 5 + (rand() % 4);
s_score = 0;
}
@@ -58,45 +64,43 @@ static void game_update(uint32_t delta_ms)
}
if (s_game_state == GAME_STATE_PLAYING) {
static uint32_t jump_acc = 0;
// 60 FPS Jump Physics
if (s_is_jumping) {
jump_acc += delta_ms;
if (jump_acc >= 45) {
jump_acc = 0;
s_player_y += s_player_vy;
s_player_vy += 1;
if (s_player_y >= GROUND_Y) {
s_player_y = GROUND_Y;
s_player_vy = 0;
s_is_jumping = false;
}
}
} else {
jump_acc = 0;
}
s_player_y_fp += s_player_vy_fp;
s_player_vy_fp += 34; // Gravity per frame
static uint32_t move_acc = 0;
move_acc += delta_ms;
if (move_acc >= 16) {
move_acc = 0;
s_obs_x -= 1;
if (s_obs_x < -4) {
s_obs_x = 72 + (rand() % 16);
s_obs_h = 5 + (rand() % 5);
s_score++;
if (s_score > s_high_score) s_high_score = s_score;
if (s_player_y_fp >= TO_FP(GROUND_Y)) {
s_player_y_fp = TO_FP(GROUND_Y);
s_player_vy_fp = 0;
s_is_jumping = false;
}
}
if (s_obs_x <= 13 && (s_obs_x + 3) >= 8) {
if ((s_player_y - 1) >= (GROUND_Y - s_obs_h + 1)) {
// 60 FPS Obstacle Movement (~70 px/sec)
s_obs_x_fp -= 300; // ~1.17 pixels per 60fps frame
int16_t obs_x = FROM_FP(s_obs_x_fp);
if (obs_x < -4) {
s_obs_x_fp = TO_FP(72 + (rand() % 16));
obs_x = FROM_FP(s_obs_x_fp);
s_obs_h = 5 + (rand() % 5);
s_score++;
if (s_score > s_high_score) s_high_score = s_score;
}
int16_t player_y = FROM_FP(s_player_y_fp);
// Collision Check (Hitbox: player is 5x5 at x=8..12, obs is 3xH at obs_x..obs_x+2)
if (obs_x <= 13 && (obs_x + 3) >= 8) {
if ((player_y - 1) >= (GROUND_Y - s_obs_h + 1)) {
s_game_state = GAME_STATE_OVER;
sys_gfx_show_toast("GAME OVER", 800);
}
}
sys_gfx_fill_rect(8, s_player_y - 5, 5, 5, COLOR_WHITE);
sys_gfx_fill_rect(s_obs_x, GROUND_Y - s_obs_h + 1, 3, s_obs_h, COLOR_WHITE);
// Render Player and Obstacle
sys_gfx_fill_rect(8, player_y - 5, 5, 5, COLOR_WHITE);
sys_gfx_fill_rect(obs_x, GROUND_Y - s_obs_h + 1, 3, s_obs_h, COLOR_WHITE);
char score_str[12];
snprintf(score_str, sizeof(score_str), "SC:%u", s_score);
@@ -125,7 +129,7 @@ static void game_handle_input(input_action_t action)
} else if (s_game_state == GAME_STATE_PLAYING) {
if (!s_is_jumping) {
s_is_jumping = true;
s_player_vy = -5;
s_player_vy_fp = -740; // Instant jump impulse (-2.9 px/frame)
}
} else if (s_game_state == GAME_STATE_OVER) {
s_game_state = GAME_STATE_PLAYING;
+2 -2
View File
@@ -52,8 +52,8 @@ static void settings_handle_input(input_action_t action)
if (action == INPUT_ACTION_CLICK) {
s_selected_idx = (s_selected_idx + 1) % SETTINGS_COUNT;
} else if (action == INPUT_ACTION_DOUBLE_CLICK) {
if (s_selected_idx == 0) s_selected_idx = SETTINGS_COUNT - 1;
else s_selected_idx--;
/* Tap 1 already stepped +1; stepping -2 results in a net -1 (previous entry) */
s_selected_idx = (s_selected_idx + SETTINGS_COUNT - 2) % SETTINGS_COUNT;
} else if (action == INPUT_ACTION_LONG_PRESS) {
switch (s_selected_idx) {
case 0: