From 7aa592e7fb8eceed8ca4373eee5d38aa6eebf7cd Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 26 Nov 2022 15:22:17 -0800 Subject: game: normalize all movements Until now keyboard movements could "cheat" by moving faster on the diagonals. Instead put all the movements through the same normalizing introduced by the touch handling. --- src/game.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/game.c b/src/game.c index 156a439..04e9494 100644 --- a/src/game.c +++ b/src/game.c @@ -977,6 +977,10 @@ static void game_update(play_t *play, void *context) const Uint8 *key_state = SDL_GetKeyboardState(NULL); v2f_t dir = {}, *move = NULL; + /* TODO: acceleration curve for movement? it'd enable more precise + * negotiating of obstacles, but that's not really worthwhile until there's + * pixel-precision collision detection... + */ if (key_state[SDL_SCANCODE_LEFT] || key_state[SDL_SCANCODE_A]) { dir.x += -GAME_ADULT_SPEED; move = &dir; @@ -998,19 +1002,21 @@ static void game_update(play_t *play, void *context) } if (game->touch.active) { + dir = v2f_sub(&game->touch.position, &game->adult->entity.position); + move = &dir; + } + + if (move) { float distance; - dir = v2f_sub(&game->touch.position, &game->adult->entity.position); - distance = v2f_length(&dir); + distance = v2f_length(move); if (distance) { - dir = v2f_normalize(&dir); - dir = v2f_mult_scalar(&dir, distance < GAME_ADULT_SPEED ? distance : GAME_ADULT_SPEED); - move = &dir; + *move = v2f_normalize(move); + *move = v2f_mult_scalar(move, distance < GAME_ADULT_SPEED ? distance : GAME_ADULT_SPEED); } - } - if (move) game_move_adult(game, move); + } } if (play_ticks_elapsed(play, GAME_TV_TIMER, GAME_TV_DELAY_MS)) { -- cgit v1.2.3