#include "systems.h" #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)); ecs_set(ECS, entity, Easing, { .compID = ecs_id(Rotation), .memberOffset = 0, .easingFunc = BZ_EASE_OUT_ELASTIC, .duration = 0.6f, // 45 * (1.0f + (-1.0f) * x) .target = GetRandomValue(30, 55) * GetRandomValue(1, 2) - 1, .easeTarget = -1.0f, .easeStart = 1.0f }); SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState); soundsPlay(sounds, SOUND_WOOD_PUNCH); Resource *res = ecs_get_mut(ECS, entity, Resource); BZ_ASSERT(res->type == event.type); event.amount = BZ_MIN(event.amount, res->amount); res->amount -= event.amount; if (res->amount <= 0) ecs_delete(ECS, entity); return event.amount; } void depositEvent(ecs_entity_t entity, DepositEvent event) { BZ_ASSERT(ecs_has(ECS, entity, Storage)); BZ_ASSERT(ecs_has(ECS, entity, Owner)); Owner owner = *ecs_get(ECS, entity, Owner); Game *game = ecs_singleton_get_mut(ECS, Game); PlayerResources *res = &game->playerResources[owner.player]; switch (event.type) { case RES_WOOD: res->wood += event.amount; break; case RES_GOLD: res->gold += event.amount; break; case RES_FOOD: res->food += event.amount; break; default: BZ_ASSERT(false); break; } }