Move systems into subdirectory, add tree shake animation

This commit is contained in:
2023-12-29 14:46:11 +01:00
parent aa3bfbf823
commit 31a9289770
15 changed files with 352 additions and 105 deletions

View File

@@ -0,0 +1,97 @@
#include "systems.h"
#include "../game_state.h"
#include <raymath.h>
void updateAnimationState(ecs_iter_t *it) {
Animation *anim = ecs_field(it, Animation, 1);
TextureRegion *text = ecs_field(it, TextureRegion, 2);
for (i32 i = 0; i < it->count; 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;
}
}
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 updateAnimation(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Animation *anim = ecs_field(it, Animation, 1);
TextureRegion *texture = ecs_field(it, TextureRegion, 2);
float dt = GetFrameTime();
for (i32 i = 0; i < it->count; i++) {
AnimationFrame frame = anim[i].frame;
AnimationSequence seq = anim[i].sequence;
anim[i].elapsed += dt;
if (anim[i].elapsed < frame.duration) continue;
i32 nextFrame = (anim[i].curFrame + 1) % seq.frameCount;
anim[i].curFrame = nextFrame;
anim[i].frame = entityGetAnimationFrame(anim[i].entityType, anim[i].animType, nextFrame);
anim[i].elapsed = 0.0f;
texture[i].rec = bzTilesetGetTileRegion(anim[i].tileset, anim[i].frame.frame);
}
}
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 (easing[i].elapsed > easing[i].duration) {
ecs_remove(ECS, entity, Easing);
continue;
}
easing[i].elapsed += dt;
f32 alpha = easing[i].elapsed / easing[i].duration;
alpha = BZ_MIN(1.0f, alpha);
easing[i].x = bzEase(easing[i].easingFunc, alpha);
const Easing *e = &easing[i];
f32 x = e->x;
// Inner
x = e->easeStart + e->easeTarget * x + e->easeOffset;
// 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;
}
}
}