Generalize easing system (works for all components now)
This commit is contained in:
@@ -111,20 +111,13 @@ typedef struct Animation {
|
|||||||
} Animation;
|
} Animation;
|
||||||
extern ECS_COMPONENT_DECLARE(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 {
|
typedef struct Easing {
|
||||||
EasingType type;
|
ecs_entity_t compID;
|
||||||
|
size_t memberOffset;
|
||||||
|
|
||||||
BzEaseType easingFunc;
|
BzEaseType easingFunc;
|
||||||
|
|
||||||
// start + target * (easeStart + (easeTarget * x) + easeOffset) + offset
|
// start + target * (easeStart + (easeTarget * x) + easeOffset) + offset
|
||||||
|
|
||||||
f32 target;
|
f32 target;
|
||||||
f32 start;
|
f32 start;
|
||||||
f32 offset;
|
f32 offset;
|
||||||
@@ -135,7 +128,6 @@ typedef struct Easing {
|
|||||||
f32 x;
|
f32 x;
|
||||||
f32 elapsed;
|
f32 elapsed;
|
||||||
f32 duration;
|
f32 duration;
|
||||||
|
|
||||||
//struct Easing *next;
|
//struct Easing *next;
|
||||||
} Easing;
|
} Easing;
|
||||||
extern ECS_COMPONENT_DECLARE(Easing);
|
extern ECS_COMPONENT_DECLARE(Easing);
|
||||||
|
|||||||
@@ -51,15 +51,14 @@ void updateAnimation(ecs_iter_t *it) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateEasingSystem(ecs_iter_t *it) {
|
void updateEasingSystem(ecs_iter_t *it) {
|
||||||
Easing *easing = ecs_field(it, Easing, 1);
|
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();
|
f32 dt = GetFrameTime();
|
||||||
|
|
||||||
for (i32 i = 0; i < it->count; i++) {
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
ecs_entity_t entity = it->entities[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) {
|
if (easing[i].elapsed > easing[i].duration) {
|
||||||
ecs_remove(ECS, entity, Easing);
|
ecs_remove(ECS, entity, Easing);
|
||||||
continue;
|
continue;
|
||||||
@@ -76,22 +75,9 @@ void updateEasingSystem(ecs_iter_t *it) {
|
|||||||
// Outer
|
// Outer
|
||||||
x = e->start + e->target * x + e->offset;
|
x = e->start + e->target * x + e->offset;
|
||||||
|
|
||||||
switch (easing->type) {
|
u8 *compData = ecs_get_mut_id(ECS, entity, easing[i].compID);
|
||||||
case EASE_ROTATION:
|
compData += easing[i].memberOffset;
|
||||||
rotation[i] = x;
|
|
||||||
break;
|
*(f32 *) compData = x;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
|
|||||||
BZ_ASSERT(ecs_has(ECS, entity, Resource));
|
BZ_ASSERT(ecs_has(ECS, entity, Resource));
|
||||||
|
|
||||||
ecs_set(ECS, entity, Easing, {
|
ecs_set(ECS, entity, Easing, {
|
||||||
.type = EASE_ROTATION,
|
.compID = ecs_id(Rotation),
|
||||||
|
.memberOffset = 0,
|
||||||
.easingFunc = BZ_EASE_OUT_ELASTIC,
|
.easingFunc = BZ_EASE_OUT_ELASTIC,
|
||||||
.duration = 0.4f,
|
.duration = 0.4f,
|
||||||
|
|
||||||
|
|||||||
@@ -36,13 +36,6 @@ void updateUnitAISystem(ecs_iter_t *it);
|
|||||||
*/
|
*/
|
||||||
void updateUnitActionsSystem(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
|
* Animation Systems
|
||||||
@@ -61,6 +54,11 @@ void updateAnimationState(ecs_iter_t *it);
|
|||||||
*/
|
*/
|
||||||
void updateAnimation(ecs_iter_t *it);
|
void updateAnimation(ecs_iter_t *it);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 1: Easing
|
||||||
|
*/
|
||||||
|
void updateEasingSystem(ecs_iter_t *it);
|
||||||
|
|
||||||
/**********************************
|
/**********************************
|
||||||
* Entity Systems
|
* Entity Systems
|
||||||
**********************************/
|
**********************************/
|
||||||
|
|||||||
Reference in New Issue
Block a user