Generalize easing system (works for all components now)

This commit is contained in:
2023-12-29 20:03:01 +01:00
parent 69232cebf4
commit 3ec7505214
4 changed files with 17 additions and 40 deletions

View File

@@ -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);

View File

@@ -52,14 +52,13 @@ 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);
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;
}
}

View File

@@ -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,

View File

@@ -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
**********************************/