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 = {};
switch (entity->type) {
case FP_EntityType_Nil: {
} break;
case FP_EntityType_Terry: {
FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state;
TELY_AssetSpriteSheet *sheet = &game->terry_sprite_sheet;
result.sheet = sheet;
if (*state == FP_EntityTerryState_Nil)
switch (*state) {
case FP_EntityTerryState_Nil: {
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);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result);
} 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);
}
}
}
} 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);
action->flip_on_x = entity->direction == FP_GameDirection_Right;
@ -362,27 +384,9 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
}
}
#endif
}
} break;
if (*state == 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);
}
}
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) {
case FP_EntityTerryState_Run: {
Dqn_String8 desired_action_name = {};
switch (entity->direction) {
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*/) {
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);
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);
}
}
} break;
}
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;
result.sheet = sheet;
if (*state == FP_EntitySmoochieState_Nil)
switch (*state) {
case FP_EntitySmoochieState_Nil: {
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);
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);
}
}
}
} break;
if (*state == FP_EntitySmoochieState_AttackDown) {
case FP_EntitySmoochieState_AttackDown: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_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_EntitySmoochieState_Idle);
}
}
} break;
if (*state == FP_EntitySmoochieState_AttackSide) {
case FP_EntitySmoochieState_AttackSide: {
result.anim = TELY_Asset_GetSpriteAnimation(sheet, g_anim_names.smoochie_attack_side);
action->flip_on_x = entity->direction == FP_GameDirection_Right;
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);
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 = {};
switch (entity->direction) {
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*/) {
FP_Game_EntityActionSetState(action, FP_EntitySmoochieState_Idle);
}
} break;
}
if (*state == FP_EntitySmoochieState_AttackSide || *state == FP_EntitySmoochieState_AttackDown) {
@ -579,7 +590,6 @@ FP_GameEntityActionSprite FP_EntityActionStateMachine(FP_Game *game, TELY_Platfo
} else {
entity->attack_box_size = {};
}
} break;
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;
result.sheet = sheet;
if (*state == FP_EntityTerryMerchantState_Nil)
switch (*state) {
case FP_EntityTerryMerchantState_Nil: {
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);
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, result);
}
} break;
}
} break;
}