From 3ec7505214e2cb1cafafe5d215519f8706820893 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Fri, 29 Dec 2023 20:03:01 +0100 Subject: [PATCH] Generalize easing system (works for all components now) --- game/components.h | 14 +++----------- game/systems/s_animation.c | 28 +++++++--------------------- game/systems/s_event.c | 3 ++- game/systems/systems.h | 12 +++++------- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/game/components.h b/game/components.h index 2811c10..8ea175f 100644 --- a/game/components.h +++ b/game/components.h @@ -111,20 +111,13 @@ typedef struct Animation { } Animation; extern ECS_COMPONENT_DECLARE(Animation); -typedef enum EasingType { - EASE_ROTATION, - EASE_POS_X, - EASE_POS_Y, - EASE_SIZE_X, - EASE_SIZE_Y, -} EasingType; - typedef struct Easing { - EasingType type; + ecs_entity_t compID; + size_t memberOffset; + BzEaseType easingFunc; // start + target * (easeStart + (easeTarget * x) + easeOffset) + offset - f32 target; f32 start; f32 offset; @@ -135,7 +128,6 @@ typedef struct Easing { f32 x; f32 elapsed; f32 duration; - //struct Easing *next; } Easing; extern ECS_COMPONENT_DECLARE(Easing); diff --git a/game/systems/s_animation.c b/game/systems/s_animation.c index 669acb0..68f2588 100644 --- a/game/systems/s_animation.c +++ b/game/systems/s_animation.c @@ -51,15 +51,14 @@ void updateAnimation(ecs_iter_t *it) { } void updateEasingSystem(ecs_iter_t *it) { - Easing *easing = ecs_field(it, Easing, 1); - Position *position = ecs_field(it, Position, 2); - Size *size = ecs_field(it, Size, 3); - Rotation *rotation = ecs_field(it, Rotation, 4); + Easing *easing = ecs_field(it, Easing, 1); f32 dt = GetFrameTime(); for (i32 i = 0; i < it->count; i++) { ecs_entity_t entity = it->entities[i]; + if (!ecs_has_id(ECS, entity, easing[i].compID)) + continue; if (easing[i].elapsed > easing[i].duration) { ecs_remove(ECS, entity, Easing); continue; @@ -76,22 +75,9 @@ void updateEasingSystem(ecs_iter_t *it) { // Outer x = e->start + e->target * x + e->offset; - switch (easing->type) { - case EASE_ROTATION: - rotation[i] = x; - break; - case EASE_POS_X: - position[i].x = x; - break; - case EASE_POS_Y: - position[i].y = x; - break; - case EASE_SIZE_X: - size[i].x = x; - break; - case EASE_SIZE_Y: - size[i].y = x; - break; - } + u8 *compData = ecs_get_mut_id(ECS, entity, easing[i].compID); + compData += easing[i].memberOffset; + + *(f32 *) compData = x; } } diff --git a/game/systems/s_event.c b/game/systems/s_event.c index 4a0600a..6d5f0b5 100644 --- a/game/systems/s_event.c +++ b/game/systems/s_event.c @@ -7,7 +7,8 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) { BZ_ASSERT(ecs_has(ECS, entity, Resource)); ecs_set(ECS, entity, Easing, { - .type = EASE_ROTATION, + .compID = ecs_id(Rotation), + .memberOffset = 0, .easingFunc = BZ_EASE_OUT_ELASTIC, .duration = 0.4f, diff --git a/game/systems/systems.h b/game/systems/systems.h index d04b0ec..fbdd0ac 100644 --- a/game/systems/systems.h +++ b/game/systems/systems.h @@ -36,13 +36,6 @@ void updateUnitAISystem(ecs_iter_t *it); */ void updateUnitActionsSystem(ecs_iter_t *it); -/* - * 1: Easing - * 2: Position - * 3: Size - * 4: Rotation - */ -void updateEasingSystem(ecs_iter_t *it); /********************************** * Animation Systems @@ -61,6 +54,11 @@ void updateAnimationState(ecs_iter_t *it); */ void updateAnimation(ecs_iter_t *it); +/* + * 1: Easing + */ +void updateEasingSystem(ecs_iter_t *it); + /********************************** * Entity Systems **********************************/