Move systems into subdirectory, add tree shake animation
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "game_state.h"
|
||||
#include "../game_state.h"
|
||||
|
||||
#include "unit_ai.h"
|
||||
#include "unit_actions.h"
|
||||
#include "../unit_ai.h"
|
||||
#include "../unit_actions.h"
|
||||
|
||||
void handleUnitActionsSystem(ecs_iter_t *it) {
|
||||
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||
|
||||
97
game/systems/s_animation.c
Normal file
97
game/systems/s_animation.c
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "systems.h"
|
||||
|
||||
|
||||
#include "game_state.h"
|
||||
#include "input.h"
|
||||
#include "pathfinding.h"
|
||||
#include "../game_state.h"
|
||||
#include "../input.h"
|
||||
#include "../pathfinding.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <raymath.h>
|
||||
@@ -88,7 +88,7 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
||||
|
||||
Vector2 mouse = input->mouseDownWorld;
|
||||
f32 rot = Vector2Angle(position[i], mouse) + 270 * DEG2RAD;
|
||||
rotation[i] = rot;
|
||||
//rotation[i] = rot;
|
||||
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ void entityMoveToTarget(ecs_iter_t *it) {
|
||||
|
||||
if (Vector2Length(velocity[i]) > 10.0f) {
|
||||
f32 rot = Vector2Angle(position[i], target);
|
||||
rotation[i] = rot;
|
||||
//rotation[i] = rot;
|
||||
}
|
||||
|
||||
if (dst < 8.0f) {
|
||||
@@ -162,52 +162,6 @@ void entityFollowPath(ecs_iter_t *it) {
|
||||
}
|
||||
}
|
||||
|
||||
void entityUpdateAnimationState(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 entityUpdateAnimation(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 renderColliders(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
Size *size = ecs_field(it, Size, 2);
|
||||
|
||||
28
game/systems/s_event.c
Normal file
28
game/systems/s_event.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "../game_state.h"
|
||||
|
||||
i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
|
||||
BZ_ASSERT(ecs_has_id(ECS, entity, Harvestable));
|
||||
BZ_ASSERT(ecs_has(ECS, entity, Resource));
|
||||
|
||||
ecs_set(ECS, entity, Easing, {
|
||||
.type = EASE_ROTATION,
|
||||
.easingFunc = BZ_EASE_OUT_ELASTIC,
|
||||
.duration = 0.4f,
|
||||
|
||||
// 45 * (1.0f + (-1.0f) * x)
|
||||
.target = 45,
|
||||
.easeTarget = -1.0f,
|
||||
.easeStart = 1.0f
|
||||
});
|
||||
|
||||
Resource *res = ecs_get_mut(ECS, entity, Resource);
|
||||
event.amount = BZ_MIN(event.amount, res->amount);
|
||||
res->amount -= event.amount;
|
||||
|
||||
if (res->amount <= 0)
|
||||
ecs_delete(ECS, entity);
|
||||
|
||||
return event.amount;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
#include "systems.h"
|
||||
#include "game_state.h"
|
||||
#include "input.h"
|
||||
#include "buildings.h"
|
||||
#include "pathfinding.h"
|
||||
#include "unit_ai.h"
|
||||
#include "unit_actions.h"
|
||||
|
||||
#include "../game_state.h"
|
||||
#include "../input.h"
|
||||
#include "../buildings.h"
|
||||
#include "../pathfinding.h"
|
||||
#include "../unit_ai.h"
|
||||
#include "../unit_actions.h"
|
||||
|
||||
#include <rlImGui.h>
|
||||
#include <raymath.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "game_state.h"
|
||||
#include "../game_state.h"
|
||||
|
||||
void uiTask(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
#include "components.h"
|
||||
#include "../components.h"
|
||||
|
||||
typedef struct Game Game;
|
||||
|
||||
@@ -36,6 +36,30 @@ 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
|
||||
**********************************/
|
||||
|
||||
|
||||
/*
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void updateAnimationState(ecs_iter_t *it);
|
||||
/*
|
||||
* 0:
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void updateAnimation(ecs_iter_t *it);
|
||||
|
||||
/**********************************
|
||||
* Entity Systems
|
||||
@@ -81,19 +105,6 @@ void entityMoveToTarget(ecs_iter_t *it);
|
||||
void entityFollowPath(ecs_iter_t *it);
|
||||
|
||||
|
||||
/*
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void entityUpdateAnimationState(ecs_iter_t *it);
|
||||
/*
|
||||
* 0:
|
||||
* 1: Animation
|
||||
* 2: TextureRegion
|
||||
*/
|
||||
void entityUpdateAnimation(ecs_iter_t *it);
|
||||
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Size
|
||||
@@ -147,4 +158,33 @@ void drawPlayerInputUI();
|
||||
* UI systems
|
||||
**********************************/
|
||||
|
||||
/**********************************
|
||||
* MISC
|
||||
**********************************/
|
||||
|
||||
static void setupSystems(ecs_world_t *ecs) {
|
||||
|
||||
ECS_OBSERVER(ecs, entityPathRemove, EcsOnRemove, Path);
|
||||
|
||||
ECS_SYSTEM(ecs, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
|
||||
ECS_SYSTEM(ecs, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, Steering);
|
||||
|
||||
ECS_SYSTEM(ecs, entityMoveToTarget, EcsOnUpdate, Position, Rotation, Velocity, TargetPosition, Steering);
|
||||
ECS_SYSTEM(ecs, entityFollowPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ecs, handleUnitActionsSystem, EcsOnUpdate, UnitAction);
|
||||
ECS_SYSTEM(ecs, updateUnitAISystem, EcsOnUpdate, UnitAI, UnitAction);
|
||||
// Needs to be called after AI update, since it removes finished actions
|
||||
ECS_SYSTEM(ecs, updateUnitActionsSystem, EcsOnUpdate, UnitAction);
|
||||
|
||||
ECS_SYSTEM(ecs, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
|
||||
ECS_SYSTEM(ecs, updateAnimation, EcsOnUpdate, Animation, TextureRegion);
|
||||
ECS_SYSTEM(ecs, updateEasingSystem, EcsOnUpdate, Easing, Position, Size, Rotation);
|
||||
|
||||
ECS_SYSTEM(ecs, renderDebugPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ecs, renderColliders, EcsOnUpdate, Position, Size);
|
||||
ECS_SYSTEM(ecs, renderRotationDirection, EcsOnUpdate, Position, Rotation);
|
||||
}
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
Reference in New Issue
Block a user