From 61bb33b0fa99dfebac3083872dbda94f9f949165 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 10 Oct 2022 19:11:52 -0700 Subject: game: add rudimentary touch support When a touch is active, the player progresses towards the touch position. The scaling of the touch coordinates is a bit oversized so you can place the adult off-screen to rescue babies, without your finger actually going off-screen. --- src/game.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/game.c b/src/game.c index e938298..990ebf3 100644 --- a/src/game.c +++ b/src/game.c @@ -124,6 +124,13 @@ union entity_t { typedef struct game_t { game_state_t state; + struct { + SDL_TouchID touch_id; + SDL_FingerID finger_id; + v2f_t position; /* -1 .. +1 */ + int active; + } touch; + sars_t *sars; stage_t *stage; stage_t *game_node; @@ -571,6 +578,18 @@ static void game_update(play_t *play, void *context) move = &dir; } + if (game->touch.active) { + float distance; + + dir = v2f_sub(&game->touch.position, &game->adult->entity.position); + distance = v2f_length(&dir); + if (distance) { + dir = v2f_normalize(&dir); + dir = v2f_mult_scalar(&dir, distance < GAME_ADULT_SPEED ? distance : GAME_ADULT_SPEED); + move = &dir; + } + } + if (move) game_move_adult(game, move); } @@ -629,6 +648,41 @@ static void game_dispatch(play_t *play, void *context, SDL_Event *event) break; + case SDL_FINGERDOWN: + if (game->state == GAME_STATE_OVER_WAITING) + reset_game(game); + /* fallthrough */ + case SDL_FINGERMOTION: + if ((game->touch.active && + game->touch.touch_id == event->tfinger.touchId && + game->touch.finger_id == event->tfinger.fingerId) || + !game->touch.active) { + game->touch.active = 1; + game->touch.touch_id = event->tfinger.touchId; + game->touch.finger_id = event->tfinger.fingerId; + + sars_ndc_to_bpc(game->sars, + (event->tfinger.x + -.5f), + -(event->tfinger.y + -.5f), + &game->touch.position.x, + &game->touch.position.y); + + /* XXX: note the scaling is larger than -1..+1 so the babies can be saved fullscreen, + * this also has the effect of putting the adult a bit offset from the finger so you + * can actually see what you're doing more of the time (not near the center though). + */ + game->touch.position.x *= 2.4f; + game->touch.position.y *= 2.4f; + } + break; + + case SDL_FINGERUP: + if (game->touch.active && + game->touch.touch_id == event->tfinger.touchId && + game->touch.finger_id == event->tfinger.fingerId) + game->touch.active = 0; + break; + default: break; } -- cgit v1.2.3