Add further gamepad button support to character actions

This commit is contained in:
Joshalosh 2023-09-24 15:33:02 +10:00 committed by doyle
parent ca6abb7684
commit 6b3ba17fc9
2 changed files with 26 additions and 17 deletions

View File

@ -574,6 +574,7 @@ void TELY_DLL_Init(void *user_data)
entity->flags |= FP_GameEntityFlag_Clickable; entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard; entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse; entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
entity->flags |= FP_GameEntityFlag_NonTraversable; entity->flags |= FP_GameEntityFlag_NonTraversable;
entity->facing_left = true; entity->facing_left = true;
game->clicked_entity = entity->handle; 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_Clickable;
entity->flags |= FP_GameEntityFlag_MoveByKeyboard; entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_GameEntityFlag_MoveByMouse; entity->flags |= FP_GameEntityFlag_MoveByMouse;
entity->flags |= FP_GameEntityFlag_MoveByGamepad;
entity->flags |= FP_GameEntityFlag_MobSpawner; entity->flags |= FP_GameEntityFlag_MobSpawner;
} }
@ -664,12 +666,13 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer,
dir_vector.x = +1.f; dir_vector.x = +1.f;
// NOTE: Gamepad movement input // NOTE: Gamepad movement input
for (uint32_t gamepad = 0; gamepad < MAX_GAMEPADS; gamepad++) { // 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]) { if (input->button_codes[gamepad]) {
dir_vector.x += input->left_stick[gamepad].x; dir_vector.x += input->left_stick[gamepad].x;
dir_vector.y += input->left_stick[gamepad].y; dir_vector.y += input->left_stick[gamepad].y;
} }
}
if (dir_vector.x && dir_vector.y) { if (dir_vector.x && dir_vector.y) {
@ -689,11 +692,11 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer,
FP_GameEntity *entity = it.entity; FP_GameEntity *entity = it.entity;
entity->alive_time_s += input->delta_s; entity->alive_time_s += input->delta_s;
// NOTE: Move entity by keyboard =========================================================== // NOTE: Move entity by keyboard and gamepad ===============================================
Dqn_V2 acceleration = {}; Dqn_V2 acceleration = {};
if (game->clicked_entity == entity->handle && if (game->clicked_entity == entity->handle &&
(entity->action.state == FP_GameEntityState_Run || entity->action.state == FP_GameEntityState_Idle)) { (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; acceleration = dir_vector * 10000000.f;
if (dir_vector.x) if (dir_vector.x)
entity->facing_left = dir_vector.x < 0.f; 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; 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); FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim);
} else if (we_are_clicked_entity) { } 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); FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackA);
} else if (dir_vector.x || dir_vector.y) { } else if (dir_vector.x || dir_vector.y) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Run); 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) { } else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle); FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
} else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) { } 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; Dqn_f32 t01 = action->timer_s / action->end_at_s;
if (t01 > 0.5f) if (t01 > 0.5f)
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackB); 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) { } else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle); FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
} else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) { } 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; Dqn_f32 t01 = action->timer_s / action->end_at_s;
if (t01 > 0.5f) if (t01 > 0.5f)
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackC); 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; 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); FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim);
} else if (we_are_clicked_entity) { } 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); 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); 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_RFui_Flush(rfui, renderer, input, assets);
TELY_Audio_MixPlaybackSamples(audio, assets); //TELY_Audio_MixPlaybackSamples(audio, assets);
} }

View File

@ -8,10 +8,11 @@ enum FP_GameEntityFlag
FP_GameEntityFlag_Clickable = 1 << 0, FP_GameEntityFlag_Clickable = 1 << 0,
FP_GameEntityFlag_MoveByKeyboard = 1 << 1, FP_GameEntityFlag_MoveByKeyboard = 1 << 1,
FP_GameEntityFlag_MoveByMouse = 1 << 2, FP_GameEntityFlag_MoveByMouse = 1 << 2,
FP_GameEntityFlag_DrawHitBox = 1 << 3, FP_GameEntityFlag_MoveByGamepad = 1 << 3,
FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 4, FP_GameEntityFlag_DrawHitBox = 1 << 4,
FP_GameEntityFlag_NonTraversable = 1 << 5, FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 5,
FP_GameEntityFlag_MobSpawner = 1 << 6, FP_GameEntityFlag_NonTraversable = 1 << 6,
FP_GameEntityFlag_MobSpawner = 1 << 7,
}; };
enum FP_GameShapeType enum FP_GameShapeType