From 6b3ba17fc9718b6e14eadef3fa0f65dd3eddab96 Mon Sep 17 00:00:00 2001 From: Joshalosh Date: Sun, 24 Sep 2023 15:33:02 +1000 Subject: [PATCH] Add further gamepad button support to character actions --- feely_pona.cpp | 34 +++++++++++++++++++++------------- feely_pona_game.h | 9 +++++---- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/feely_pona.cpp b/feely_pona.cpp index 953b80b..7a30c2b 100644 --- a/feely_pona.cpp +++ b/feely_pona.cpp @@ -574,6 +574,7 @@ void TELY_DLL_Init(void *user_data) entity->flags |= FP_GameEntityFlag_Clickable; entity->flags |= FP_GameEntityFlag_MoveByKeyboard; entity->flags |= FP_GameEntityFlag_MoveByMouse; + entity->flags |= FP_GameEntityFlag_MoveByGamepad; entity->flags |= FP_GameEntityFlag_NonTraversable; entity->facing_left = true; game->clicked_entity = entity->handle; @@ -633,6 +634,7 @@ void TELY_DLL_Init(void *user_data) entity->flags |= FP_GameEntityFlag_Clickable; entity->flags |= FP_GameEntityFlag_MoveByKeyboard; entity->flags |= FP_GameEntityFlag_MoveByMouse; + entity->flags |= FP_GameEntityFlag_MoveByGamepad; entity->flags |= FP_GameEntityFlag_MobSpawner; } @@ -664,11 +666,12 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, dir_vector.x = +1.f; // NOTE: Gamepad movement input - for (uint32_t gamepad = 0; gamepad < MAX_GAMEPADS; gamepad++) { - if (input->button_codes[gamepad]) { - dir_vector.x += input->left_stick[gamepad].x; - dir_vector.y += input->left_stick[gamepad].y; - } + // NOTE: button_codes 0 should be the first gamepad connected, we can + // get this working with other gamepads later + uint32_t gamepad = 0; + if (input->button_codes[gamepad]) { + dir_vector.x += input->left_stick[gamepad].x; + dir_vector.y += input->left_stick[gamepad].y; } @@ -689,11 +692,11 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, FP_GameEntity *entity = it.entity; entity->alive_time_s += input->delta_s; - // NOTE: Move entity by keyboard =========================================================== + // NOTE: Move entity by keyboard and gamepad =============================================== Dqn_V2 acceleration = {}; if (game->clicked_entity == entity->handle && (entity->action.state == FP_GameEntityState_Run || entity->action.state == FP_GameEntityState_Idle)) { - if (entity->flags & FP_GameEntityFlag_MoveByKeyboard) { + if (entity->flags & (FP_GameEntityFlag_MoveByKeyboard || FP_GameEntityFlag_MoveByGamepad)) { acceleration = dir_vector * 10000000.f; if (dir_vector.x) entity->facing_left = dir_vector.x < 0.f; @@ -876,7 +879,8 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("terry_walk_idle")).index; FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim); } else if (we_are_clicked_entity) { - if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) { + if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) || + TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) { FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackA); } else if (dir_vector.x || dir_vector.y) { FP_Game_EntityActionSetState(action, FP_GameEntityState_Run); @@ -891,7 +895,8 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, } else if (action_has_finished) { FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle); } else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) { - if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) { + if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) || + TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) { Dqn_f32 t01 = action->timer_s / action->end_at_s; if (t01 > 0.5f) FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackB); @@ -908,7 +913,8 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, } else if (action_has_finished) { FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle); } else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) { - if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) { + if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) || + TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) { Dqn_f32 t01 = action->timer_s / action->end_at_s; if (t01 > 0.5f) FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackC); @@ -932,9 +938,11 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("terry_walk_right")).index; FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim); } else if (we_are_clicked_entity) { - if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) { + if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) || + TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) { FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackA); - } else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_LeftShift)) { + } else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_LeftShift) || + TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_A)) { FP_Game_EntityActionSetState(action, FP_GameEntityState_Dash); } } @@ -1316,5 +1324,5 @@ void TELY_DLL_FrameUpdate(void *user_data) } TELY_RFui_Flush(rfui, renderer, input, assets); - TELY_Audio_MixPlaybackSamples(audio, assets); + //TELY_Audio_MixPlaybackSamples(audio, assets); } diff --git a/feely_pona_game.h b/feely_pona_game.h index 13c1118..2b8aa4c 100644 --- a/feely_pona_game.h +++ b/feely_pona_game.h @@ -8,10 +8,11 @@ enum FP_GameEntityFlag FP_GameEntityFlag_Clickable = 1 << 0, FP_GameEntityFlag_MoveByKeyboard = 1 << 1, FP_GameEntityFlag_MoveByMouse = 1 << 2, - FP_GameEntityFlag_DrawHitBox = 1 << 3, - FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 4, - FP_GameEntityFlag_NonTraversable = 1 << 5, - FP_GameEntityFlag_MobSpawner = 1 << 6, + FP_GameEntityFlag_MoveByGamepad = 1 << 3, + FP_GameEntityFlag_DrawHitBox = 1 << 4, + FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 5, + FP_GameEntityFlag_NonTraversable = 1 << 6, + FP_GameEntityFlag_MobSpawner = 1 << 7, }; enum FP_GameShapeType