fp: Merge duplicate attack loops

This commit is contained in:
doyle 2023-09-29 15:18:38 +10:00
parent 4bbe4787a5
commit 70b1596bb9

View File

@ -836,6 +836,11 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
move_entity = *state == FP_EntitySmoochieState_Run || *state == FP_EntitySmoochieState_Idle; move_entity = *state == FP_EntitySmoochieState_Run || *state == FP_EntitySmoochieState_Idle;
} break; } break;
case FP_EntityType_Clinger: {
auto *state = DQN_CAST(FP_EntityClingerState *)&entity->action.state;
move_entity = *state == FP_EntityClingerState_Run || *state == FP_EntityClingerState_Idle;
} break;
case FP_EntityType_Merchant: break; case FP_EntityType_Merchant: break;
case FP_EntityType_Nil: break; case FP_EntityType_Nil: break;
} }
@ -1067,35 +1072,6 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
entity->local_pos += delta_pos; entity->local_pos += delta_pos;
} }
} }
// NOTE: Attack collisions =================================================================
{
// NOTE: Check if there's an active attack box
if (entity->attack_box_size.x != 0 && entity->attack_box_size.y != 0) {
Dqn_Rect entity_attack_box = {};
// NOTE: Convert from centre origin to top-left origin
entity_attack_box.pos = entity->local_pos + entity->attack_box_offset - (entity->attack_box_size * 0.5f);
entity_attack_box.size = entity->attack_box_size;
for (FP_GameEntityIterator target_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &target_it, game->root_entity);) {
FP_GameEntity *target = target_it.entity;
if (target->handle == entity->handle)
continue;
if (target->flags & FP_GameEntityFlag_Attackable) {
Dqn_Rect target_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, target->handle);
if (Dqn_Rect_Intersects(entity_attack_box, target_world_hit_box)) {
target->hp -= 1;
if (target->hp <= 0) {
target->is_dying = true;
}
}
}
}
}
}
// NOTE: Move entity by mouse ============================================================== // NOTE: Move entity by mouse ==============================================================
if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) { if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
@ -1188,10 +1164,19 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
if (defender->handle == attacker->handle) if (defender->handle == attacker->handle)
continue; continue;
if ((defender->flags & FP_GameEntityFlag_Attackable) == 0)
continue;
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle); Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
if (!Dqn_Rect_Intersects(attacker_box, defender_box)) if (!Dqn_Rect_Intersects(attacker_box, defender_box))
continue; continue;
// NOTE: Do HP =========================================================================
defender->hp -= 1;
if (defender->hp <= 0)
defender->is_dying = true;
// NOTE: Kickback ======================================================================
Dqn_V2 defender_world_pos = Dqn_Rect_Center(defender_box); Dqn_V2 defender_world_pos = Dqn_Rect_Center(defender_box);
Dqn_V2 attack_dir_vector = {}; Dqn_V2 attack_dir_vector = {};
if (attacker_world_pos.x < defender_world_pos.x) if (attacker_world_pos.x < defender_world_pos.x)