Very basic animations
This commit is contained in:
@@ -30,6 +30,20 @@ typedef struct TextureRegion {
|
||||
Rectangle rec;
|
||||
} TextureRegion;
|
||||
|
||||
typedef enum AnimationType {
|
||||
ANIMATION_IDLE,
|
||||
ANIMATION_WALK,
|
||||
} AnimationType;
|
||||
|
||||
typedef struct Animation {
|
||||
TextureRegion firstFrame;
|
||||
AnimationType currAnimation;
|
||||
i32 currFrame;
|
||||
i32 frameCount;
|
||||
f32 frameDuration;
|
||||
f32 elapsed;
|
||||
} Animation;
|
||||
|
||||
|
||||
|
||||
#endif //PIXELDEFENSE_COMPONENTS_H
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <rlImGui.h>
|
||||
|
||||
#include "systems/systems.h"
|
||||
#include "utils/building_types.h"
|
||||
#include "components.h"
|
||||
#include "game_state.h"
|
||||
@@ -82,6 +83,8 @@ bool init(Game *game) {
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
||||
|
||||
ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion);
|
||||
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -146,6 +149,8 @@ void render(float dt, Game *game) {
|
||||
bzTileMapDraw(&game->map);
|
||||
bzTileMapDrawColliders(&game->map);
|
||||
|
||||
ecs_progress(ECS, dt);
|
||||
|
||||
EndMode2D();
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
ECS_COMPONENT(ECS, Size);
|
||||
ECS_COMPONENT(ECS, Rotation);
|
||||
ECS_COMPONENT(ECS, TextureRegion);
|
||||
ECS_COMPONENT(ECS, Animation);
|
||||
|
||||
BzTileset *objectTileset = bzTileObjectGroupGetTileset(&GAME->map, objectGroup);
|
||||
if (!objectTileset) return true;
|
||||
@@ -33,6 +34,16 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
|
||||
ecs_set(ECS, e, Rotation, {0.0f});
|
||||
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
|
||||
ecs_set(ECS, e, Animation, {
|
||||
.firstFrame=(TextureRegion) {
|
||||
objectTileset->tiles,
|
||||
bzTilesetGetTileRegion(objectTileset, object.gid)
|
||||
},
|
||||
.currAnimation=ANIMATION_IDLE,
|
||||
.currFrame=0,
|
||||
.frameCount=2,
|
||||
.frameDuration=0.36f
|
||||
});
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
17
game/systems/animations.c
Normal file
17
game/systems/animations.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "systems.h"
|
||||
|
||||
void updateAnimations(ecs_iter_t *it) {
|
||||
Animation *anim = ecs_field(it, Animation, 1);
|
||||
TextureRegion *t = ecs_field(it, TextureRegion, 2);
|
||||
|
||||
float dt = GetFrameTime();
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
anim[i].elapsed += dt;
|
||||
if (anim[i].elapsed < anim[i].frameDuration) continue;
|
||||
|
||||
anim[i].currFrame = (anim[i].currFrame + 1) % anim[i].frameCount;
|
||||
anim[i].elapsed = 0.0f;
|
||||
t[i].rec.x = anim[i].firstFrame.rec.x + anim[i].currFrame * t[i].rec.width;
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,6 @@
|
||||
#include "../components.h"
|
||||
|
||||
void renderEntities(ecs_iter_t *it);
|
||||
void updateAnimations(ecs_iter_t *it);
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
Reference in New Issue
Block a user