diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-11-26 15:22:17 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-11-26 15:25:29 -0800 |
commit | 7aa592e7fb8eceed8ca4373eee5d38aa6eebf7cd (patch) | |
tree | a99393e583e92dd587389a90cfabf2e8ec37deb0 | |
parent | 6e001056c512014bce7bdc26adc9d7b84c983d98 (diff) |
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.
-rw-r--r-- | src/game.c | 20 |
1 files changed, 13 insertions, 7 deletions
@@ -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)) { |