Add hurt, die animation when taking damage
This commit is contained in:
@@ -3,6 +3,36 @@
|
||||
#include "../game_state.h"
|
||||
#include "../sounds.h"
|
||||
|
||||
void damageEvent(ecs_entity_t entity, DamageEvent event) {
|
||||
BZ_ASSERT(ecs_has(ECS, entity, Health));
|
||||
|
||||
Health *health = ecs_get_mut(ECS, entity, Health);
|
||||
health->hp -= event.amount;
|
||||
|
||||
bool hasAnimation = ecs_has(ECS, entity, Animation);
|
||||
if (hasAnimation && health->hp > 0) {
|
||||
// Still alive, just play hurt anim
|
||||
Animation *animation = ecs_get_mut(ECS, entity, Animation);
|
||||
animationSetState(animation, ANIM_HURT, true);
|
||||
} else if (hasAnimation) {
|
||||
// Delay delete
|
||||
Animation *animation = ecs_get_mut(ECS, entity, Animation);
|
||||
animationSetState(animation, ANIM_DIE, true);
|
||||
ecs_set(ECS, entity, DelayDelete, {
|
||||
.time = entityGetAnimationLength(animation->entityType, ANIM_DIE)
|
||||
});
|
||||
// Remove, so it becomes inactive
|
||||
ecs_remove_id(ECS, entity, Selectable);
|
||||
ecs_remove(ECS, entity, Health);
|
||||
ecs_remove(ECS, entity, Unit);
|
||||
ecs_remove(ECS, entity, Building);
|
||||
} else {
|
||||
// No animation, delete right away
|
||||
ecs_delete(ECS, entity);
|
||||
}
|
||||
health->lastChanged = GetTime();
|
||||
}
|
||||
|
||||
i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
|
||||
BZ_ASSERT(ecs_has_id(ECS, entity, ecs_id(Harvestable)));
|
||||
BZ_ASSERT(ecs_has(ECS, entity, Resource));
|
||||
|
||||
Reference in New Issue
Block a user