fp: Use switch statement for state machine

This commit is contained in:
doyle 2023-09-24 22:00:08 +10:00
parent 468c9995bd
commit 858a66786a

View File

@ -300,17 +300,21 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
FP_GameEntityActionSprite result = {}; FP_GameEntityActionSprite result = {};
switch (entity->type) { switch (entity->type) {
case FP_EntityType_Nil: {
} break;
case FP_EntityType_Terry: { case FP_EntityType_Terry: {
FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state; FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state;
TELY_AssetSpriteSheet *sheet = &game->terry_sprite_sheet; TELY_AssetSpriteSheet *sheet = &game->terry_sprite_sheet;
result.sheet = sheet; result.sheet = sheet;
if (*state == FP_EntityTerryState_Nil) switch (*state) {
case FP_EntityTerryState_Nil: {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle); FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
} break;
if (*state == FP_EntityTerryState_Idle) { case FP_EntityTerryState_Idle: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_idle); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_idle);
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result); FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result);
} else if (we_are_clicked_entity) { } else if (we_are_clicked_entity) {
@ -337,9 +341,27 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Run); FP_Game_EntityActionSetState(action, FP_EntityTerryState_Run);
} }
} }
} } break;
if (*state == FP_EntityTerryState_AttackSide) { case FP_EntityTerryState_AttackUp: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_attack_up);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
}
} break;
case FP_EntityTerryState_AttackDown: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_down);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
}
} break;
case FP_EntityTerryState_AttackSide: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_attack_side); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_attack_side);
action->flip_on_x = entity->direction == FP_GameDirection_Right; action->flip_on_x = entity->direction == FP_GameDirection_Right;
@ -362,27 +384,9 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
} }
} }
#endif #endif
} } break;
if (*state == FP_EntityTerryState_AttackUp) { case FP_EntityTerryState_Run: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_attack_up);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
}
}
if (*state == FP_EntityTerryState_AttackDown) {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_down);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
}
}
if (*state == FP_EntityTerryState_Run) {
Dqn_String8 desired_action_name = {}; Dqn_String8 desired_action_name = {};
switch (entity->direction) { switch (entity->direction) {
case FP_GameDirection_Up: desired_action_name = g_anim_names.terry_walk_up; break; case FP_GameDirection_Up: desired_action_name = g_anim_names.terry_walk_up; break;
@ -426,9 +430,9 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
if (!entity_has_velocity /*&& !has_collision*/) { if (!entity_has_velocity /*&& !has_collision*/) {
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle); FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
} }
} } break;
if (*state == FP_EntityTerryState_Dash) { case FP_EntityTerryState_Dash: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_right); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_walk_right);
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
@ -454,6 +458,7 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle); FP_Game_EntityActionSetState(action, FP_EntityTerryState_Idle);
} }
} }
} break;
} }
if (*state == FP_EntityTerryState_AttackUp || if (*state == FP_EntityTerryState_AttackUp ||
@ -479,10 +484,12 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
TELY_AssetSpriteSheet *sheet = &game->smoochie_sprite_sheet; TELY_AssetSpriteSheet *sheet = &game->smoochie_sprite_sheet;
result.sheet = sheet; result.sheet = sheet;
if (*state == FP_EntitySmoochieState_Nil) switch (*state) {
case FP_EntitySmoochieState_Nil: {
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle); FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle);
} break;
if (*state == FP_EntitySmoochieState_Idle) { case FP_EntitySmoochieState_Idle: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_walk_down); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_walk_down);
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
@ -506,18 +513,18 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Run); FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Run);
} }
} }
} } break;
if (*state == FP_EntitySmoochieState_AttackDown) { case FP_EntitySmoochieState_AttackDown: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_down); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_down);
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result); FP_Game_EntityActionReset(action, result.anim->count * result.anim->seconds_per_frame, result);
} else if (action_has_finished) { } else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle); FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle);
} }
} } break;
if (*state == FP_EntitySmoochieState_AttackSide) { case FP_EntitySmoochieState_AttackSide: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_side); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_side);
action->flip_on_x = entity->direction == FP_GameDirection_Right; action->flip_on_x = entity->direction == FP_GameDirection_Right;
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
@ -526,9 +533,12 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle); FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle);
action->flip_on_x = false; action->flip_on_x = false;
} }
} } break;
if (*state == FP_EntitySmoochieState_Run) { case FP_EntitySmoochieState_AttackHeart: {
} break;
case FP_EntitySmoochieState_Run: {
Dqn_String8 desired_action_name = {}; Dqn_String8 desired_action_name = {};
switch (entity->direction) { switch (entity->direction) {
case FP_GameDirection_Up: desired_action_name = g_anim_names.smoochie_walk_up; break; case FP_GameDirection_Up: desired_action_name = g_anim_names.smoochie_walk_up; break;
@ -564,6 +574,7 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
if (!entity_has_velocity /*&& !has_collision*/) { if (!entity_has_velocity /*&& !has_collision*/) {
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle); FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle);
} }
} break;
} }
if (*state == FP_EntitySmoochieState_AttackSide || *state == FP_EntitySmoochieState_AttackDown) { if (*state == FP_EntitySmoochieState_AttackSide || *state == FP_EntitySmoochieState_AttackDown) {
@ -579,7 +590,6 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
} else { } else {
entity->attack_box_size = {}; entity->attack_box_size = {};
} }
} break; } break;
case FP_EntityType_Merchant: { case FP_EntityType_Merchant: {
@ -587,14 +597,17 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
TELY_AssetSpriteSheet *sheet = &game->terry_merchant_sprite_sheet; TELY_AssetSpriteSheet *sheet = &game->terry_merchant_sprite_sheet;
result.sheet = sheet; result.sheet = sheet;
if (*state == FP_EntityTerryMerchantState_Nil) switch (*state) {
case FP_EntityTerryMerchantState_Nil: {
FP_Game_EntityActionSetState(action, FP_EntityTerryMerchantState_Idle); FP_Game_EntityActionSetState(action, FP_EntityTerryMerchantState_Idle);
} break;
if (*state == FP_EntityTerryMerchantState_Idle) { case FP_EntityTerryMerchantState_Idle: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_merchant); result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.terry_merchant);
if (action->flags & FP_GameEntityActionFlag_StateTransition) { if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result); FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result);
} }
} break;
} }
} break; } break;
} }