Very basic animations

This commit is contained in:
2023-11-12 18:03:55 +01:00
parent bf56b150cc
commit 526d292fb5
9 changed files with 95 additions and 6 deletions

View File

@@ -9,6 +9,7 @@ add_subdirectory(engine/)
add_executable(PixelDefense
game/systems/animations.c
game/systems/entity_renderer.c
game/systems/systems.h

View File

@@ -11,18 +11,42 @@
"tiles":[
{
"id":0,
"properties":[
{
"name":"animation",
"type":"string",
"value":"idle_0"
}],
"type":"worker"
},
{
"id":1,
"properties":[
{
"name":"animation",
"type":"string",
"value":"idle_1"
}],
"type":"worker"
},
{
"id":2,
"properties":[
{
"name":"animation",
"type":"string",
"value":"walk_0"
}],
"type":"worker"
},
{
"id":3,
"properties":[
{
"name":"animation",
"type":"string",
"value":"walk_1"
}],
"type":"worker"
},
{

View File

@@ -109,8 +109,8 @@ int main(int argc, const char **argv) {
if (appDesc.update)
appDesc.update(dt, appDesc.userData);
if (ECS)
ecs_progress(ECS, dt);
//if (ECS)
// ecs_progress(ECS, dt);
BeginDrawing();
if (appDesc.render)

View File

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

View File

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

View File

@@ -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
View 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;
}
}

View File

@@ -6,5 +6,6 @@
#include "../components.h"
void renderEntities(ecs_iter_t *it);
void updateAnimations(ecs_iter_t *it);
#endif //PIXELDEFENSE_SYSTEMS_H

View File

@@ -4,9 +4,25 @@
<export target="../assets/entities.tsj" format="json"/>
</editorsettings>
<image source="../assets/entities.png" width="1024" height="1024"/>
<tile id="0" type="worker"/>
<tile id="1" type="worker"/>
<tile id="2" type="worker"/>
<tile id="3" type="worker"/>
<tile id="0" type="worker">
<properties>
<property name="animation" value="idle_0"/>
</properties>
</tile>
<tile id="1" type="worker">
<properties>
<property name="animation" value="idle_1"/>
</properties>
</tile>
<tile id="2" type="worker">
<properties>
<property name="animation" value="walk_0"/>
</properties>
</tile>
<tile id="3" type="worker">
<properties>
<property name="animation" value="walk_1"/>
</properties>
</tile>
<tile id="64" type="axe"/>
</tileset>