From 0c9fae77819d20b8940770c392d38f0b6dc78524 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Fri, 29 Dec 2023 08:02:19 +0100 Subject: [PATCH] Add walk animation --- game/main.c | 2 +- game/systems.h | 18 ++---------------- game/systems_entity.c | 29 ++++++++++++++++++----------- game/systems_input.c | 6 +++--- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/game/main.c b/game/main.c index 280d45a..6a39fa7 100644 --- a/game/main.c +++ b/game/main.c @@ -205,7 +205,7 @@ bool init(void *userData) { // Needs to be called after AI update, since it removes finished actions ECS_SYSTEM(ECS, updateUnitActionsSystem, EcsOnUpdate, UnitAction); - //ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Velocity, AnimationType); + ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Animation, TextureRegion); ECS_SYSTEM(ECS, entityUpdateAnimation, EcsOnUpdate, Animation, TextureRegion); ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path); diff --git a/game/systems.h b/game/systems.h index 09b3bfa..66a853b 100644 --- a/game/systems.h +++ b/game/systems.h @@ -53,13 +53,6 @@ void entitySpatialRemove(ecs_iter_t *it); */ void entityPathRemove(ecs_iter_t *it); -/* Observer (for updating animation state) - * 1: Animation - * 2: AnimationType - */ -void entitySetAnimationState(ecs_iter_t *it); - - /* * 0: Game (singleton) for entity map * 1: Position @@ -93,17 +86,10 @@ void entityMoveToTarget(ecs_iter_t *it); */ void entityFollowPath(ecs_iter_t *it); -/* - * 0: Game (for pathfinding) - * 1: Position - * 2: Rotation - * 3: HarvestTask - */ -void entityHarvestTaskSystem(ecs_iter_t *it); /* - * 1: Velocity - * 2: AnimationType + * 1: Animation + * 2: TextureRegion */ void entityUpdateAnimationState(ecs_iter_t *it); /* diff --git a/game/systems_entity.c b/game/systems_entity.c index 6836973..a4b4abf 100644 --- a/game/systems_entity.c +++ b/game/systems_entity.c @@ -253,19 +253,26 @@ void entityHarvestTaskSystem(ecs_iter_t *it) { */ void entityUpdateAnimationState(ecs_iter_t *it) { - Velocity *velocity = ecs_field(it, Velocity, 1); - //AnimationType *animType = ecs_field(it, AnimationType , 2); + Animation *anim = ecs_field(it, Animation, 1); + TextureRegion *text = ecs_field(it, TextureRegion, 2); for (i32 i = 0; i < it->count; i++) { - f32 len = Vector2Length(velocity[i]); + ecs_entity_t entity = it->entities[i]; + AnimType type = ANIM_IDLE; + if (ecs_has(ECS, entity, Velocity)) { + Velocity vel = *ecs_get(ECS, entity, Velocity); + f32 len = Vector2Length(vel); + if (len > 1.0f) { + type = ANIM_WALK; + text[i].flipX = vel.x < 0; + } + } - /* - ecs_entity_t entity = it->entities[i]; - AnimationType type = ANIM_IDLE; - if (len > 1.0f) - type = ANIM_WALK; - - ecs_set(it->world, entity, AnimationType, {type}); - */ + if (type != anim[i].animType) { + anim[i].animType = type; + anim[i].sequence = entityGetAnimationSequence(anim[i].entityType, type); + anim[i].curFrame = 0; + anim[i].elapsed = 0; + } } } void entityUpdateAnimation(ecs_iter_t *it) { diff --git a/game/systems_input.c b/game/systems_input.c index 5b5c2bd..a4e2bd1 100644 --- a/game/systems_input.c +++ b/game/systems_input.c @@ -112,17 +112,17 @@ void inputUnitAction(Game *game, InputState *input) { if (isInputBtnJustUp(input, actionBtn)) { // Note: We mustn't use ecs ecs_remove_all since this will also // remove ongoing paths that are not part of this query. - iterateSelectedUnits(input->queries.selected, iterRemovePaths); + ecs_defer_begin(ECS); + iterateSelectedUnits(query, iterRemovePaths); + ecs_defer_end(ECS); const Position target = input->mouseWorld; ecs_iter_t it = ecs_query_iter(ECS, query); ecs_defer_begin(ECS); while (ecs_iter_next(&it)) { - const Position *pos = ecs_field(&it, Position, 1); for (i32 i = 0; i < it.count; i++) { const ecs_entity_t entity = it.entities[i]; - entitySetPath(entity, target, game); } }