fp: Separate entity input from update

This commit is contained in:
doyle 2023-09-29 15:28:11 +10:00
parent 70b1596bb9
commit 543e759682

View File

@ -816,6 +816,8 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
}
Dqn_ProfilerZone update_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: Entity loop"), FP_ProfileZone_FPUpdate_EntityLoop);
// NOTE: Handle input ==========================================================================
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
entity->alive_time_s += PHYSICS_STEP;
@ -982,6 +984,13 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
}
}
// NOTE: Move entity by mouse ==============================================================
if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
entity->velocity = {};
acceleration_meters_per_s = {};
entity->local_pos += input->mouse_p_delta;
}
// NOTE: Core equations of motion ==========================================================
bool has_collision = false;
{
@ -1073,14 +1082,15 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
}
}
// NOTE: Move entity by mouse ==============================================================
if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
if (entity->flags & FP_GameEntityFlag_MoveByMouse) {
entity->velocity = {};
entity->local_pos += input->mouse_p_delta;
}
}
// NOTE: Tick the state machine
FP_EntityActionStateMachine(game, input, entity, dir_vector);
}
// NOTE: Update entity =========================================================================
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
// NOTE: Derive dynmamic bounding boxes ====================================================
if (entity->flags & FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox) {
Dqn_Rect children_bbox = {};
@ -1101,9 +1111,6 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
entity->local_hit_box_size = padded_bbox.size;
}
// NOTE: Handle input on entity ============================================================
FP_EntityActionStateMachine(game, input, entity, dir_vector);
// NOTE: Mob spawner =======================================================================
if (entity->flags & FP_GameEntityFlag_MobSpawner) {
@ -1144,55 +1151,55 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input
}
}
}
// NOTE: Do attacks ============================================================================
{
Dqn_Profiler_ZoneScopeWithIndex("FP_Update: Attacks", FP_ProfileZone_FPUpdate_Attacks);
FP_GameEntity *attacker = entity;
// NOTE: Resolve attack boxes
if (!Dqn_V2_Area(attacker->attack_box_size))
continue;
Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle);
Dqn_V2 attacker_world_pos = FP_Game_CalcEntityWorldPos(game, attacker->handle);
for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->root_entity); ) {
FP_GameEntity *defender = defender_it.entity;
if (defender->handle == attacker->handle)
continue;
if ((defender->flags & FP_GameEntityFlag_Attackable) == 0)
continue;
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
if (!Dqn_Rect_Intersects(attacker_box, defender_box))
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 attack_dir_vector = {};
if (attacker_world_pos.x < defender_world_pos.x)
attack_dir_vector.x = 1.f;
else
attack_dir_vector.x = -1.f;
Dqn_V2 acceleration = attack_dir_vector * 500'000.f;
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(PHYSICS_STEP);
Dqn_f32 t_squared = DQN_SQUARED(t);
Dqn_V2 delta_p = (acceleration * 0.5f * t_squared) + (defender->velocity * t);
defender->velocity = (acceleration * t) + defender->velocity * 2.0f;
}
}
}
Dqn_Profiler_EndZone(update_zone);
// NOTE: Do attacks ============================================================================
auto attack_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: Attacks"), FP_ProfileZone_FPUpdate_Attacks);
for (FP_GameEntityIterator attacker_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &attacker_it, game->root_entity); ) {
FP_GameEntity *attacker = attacker_it.entity;
// NOTE: Resolve attack boxes
if (!Dqn_V2_Area(attacker->attack_box_size))
continue;
Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle);
Dqn_V2 attacker_world_pos = FP_Game_CalcEntityWorldPos(game, attacker->handle);
for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->root_entity); ) {
FP_GameEntity *defender = defender_it.entity;
if (defender->handle == attacker->handle)
continue;
if ((defender->flags & FP_GameEntityFlag_Attackable) == 0)
continue;
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
if (!Dqn_Rect_Intersects(attacker_box, defender_box))
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 attack_dir_vector = {};
if (attacker_world_pos.x < defender_world_pos.x)
attack_dir_vector.x = 1.f;
else
attack_dir_vector.x = -1.f;
Dqn_V2 acceleration = attack_dir_vector * 500'000.f;
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(PHYSICS_STEP);
Dqn_f32 t_squared = DQN_SQUARED(t);
Dqn_V2 delta_p = (acceleration * 0.5f * t_squared) + (defender->velocity * t);
defender->velocity = (acceleration * t) + defender->velocity * 2.0f;
}
}
Dqn_Profiler_EndZone(attack_zone);
}
void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer)