Add hp, Add attack collision detection, Add destroy on death #3

Merged
doylet merged 1 commits from hurty_collisions into main 2023-09-26 12:43:00 +00:00
3 changed files with 34 additions and 0 deletions

View File

@ -815,6 +815,36 @@ 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;
// TODO(josh): This check should be updated based on an entity attackable flag
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) {
FP_Game_DeleteEntity(game, target->handle);
}
}
}
}
}
}
// 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) {

View File

@ -44,11 +44,13 @@ static FP_GameEntityHandle FP_Entity_CreateSmoochie(FP_Game *game, Dqn_V2 pos, D
va_end(args); va_end(args);
entity->type = FP_EntityType_Smoochie; entity->type = FP_EntityType_Smoochie;
entity->hp = 3;
entity->local_pos = pos; entity->local_pos = pos;
entity->sprite_height.meters = 1.6f; entity->sprite_height.meters = 1.6f;
entity->local_hit_box_size = Dqn_V2_InitNx2(0.4f, 1.6f) * FP_Game_MetersToPixels(game); entity->local_hit_box_size = Dqn_V2_InitNx2(0.4f, 1.6f) * FP_Game_MetersToPixels(game);
FP_Entity_AddDebugEditorFlags(game, entity->handle); FP_Entity_AddDebugEditorFlags(game, entity->handle);
entity->flags |= FP_GameEntityFlag_NonTraversable; entity->flags |= FP_GameEntityFlag_NonTraversable;
entity->flags |= FP_GameEntityFlag_Attackable;
return result; return result;
} }

View File

@ -15,6 +15,7 @@ enum FP_GameEntityFlag
FP_GameEntityFlag_MobSpawner = 1 << 7, FP_GameEntityFlag_MobSpawner = 1 << 7,
FP_GameEntityFlag_MobSpawnerWaypoint = 1 << 8, FP_GameEntityFlag_MobSpawnerWaypoint = 1 << 8,
FP_GameEntityFlag_AggrosWhenNearTerry = 1 << 9, FP_GameEntityFlag_AggrosWhenNearTerry = 1 << 9,
FP_GameEntityFlag_Attackable = 1 << 9,
}; };
enum FP_GameShapeType enum FP_GameShapeType
@ -118,6 +119,7 @@ struct FP_GameEntity
uint64_t spawn_cap; uint64_t spawn_cap;
uint64_t flags; uint64_t flags;
uint64_t hp;
FP_GameDirection direction; FP_GameDirection direction;
Dqn_V2 local_pos; Dqn_V2 local_pos;
Dqn_f64 alive_time_s; Dqn_f64 alive_time_s;