From f62614191f7ddd280614916e7a8e94a6d3af08d2 Mon Sep 17 00:00:00 2001 From: bog Date: Tue, 14 Nov 2023 20:46:05 +0100 Subject: [PATCH] :sparkles: Actor animation. --- meson.build | 1 + src/EntityFactory.cpp | 2 ++ src/arena/Arena.cpp | 3 +++ src/comps/AnimationC.hpp | 25 ++++++++++++++++++++ src/conf.in.hpp | 1 + src/sys/AnimationSystem.cpp | 46 +++++++++++++++++++++++++++++++++++++ src/sys/AnimationSystem.hpp | 24 +++++++++++++++++++ 7 files changed, 102 insertions(+) create mode 100644 src/comps/AnimationC.hpp create mode 100644 src/sys/AnimationSystem.cpp create mode 100644 src/sys/AnimationSystem.hpp diff --git a/meson.build b/meson.build index cc278ad..64bfe37 100644 --- a/meson.build +++ b/meson.build @@ -37,6 +37,7 @@ executable( 'src/sys/BodySystem.cpp', 'src/sys/ActorSystem.cpp', 'src/sys/PlayerSystem.cpp', + 'src/sys/AnimationSystem.cpp', # arena 'src/arena/Arena.cpp', diff --git a/src/EntityFactory.cpp b/src/EntityFactory.cpp index d3cba89..78a210b 100644 --- a/src/EntityFactory.cpp +++ b/src/EntityFactory.cpp @@ -2,6 +2,7 @@ #include "comps/BodyC.hpp" #include "comps/SpriteC.hpp" #include "comps/PlayerC.hpp" +#include "comps/AnimationC.hpp" namespace rid { @@ -27,6 +28,7 @@ namespace rid { size_t e = create_actor(x, y); m_ecs.attach_component(e, controller); + m_ecs.attach_component(e, 0.1f, 4); return e; } diff --git a/src/arena/Arena.cpp b/src/arena/Arena.cpp index 6dc3959..8f05662 100644 --- a/src/arena/Arena.cpp +++ b/src/arena/Arena.cpp @@ -7,12 +7,14 @@ #include "../comps/BodyC.hpp" #include "../comps/SpriteC.hpp" #include "../comps/PlayerC.hpp" +#include "../comps/AnimationC.hpp" // Systems // ------- #include "../sys/BodySystem.hpp" #include "../sys/ActorSystem.hpp" #include "../sys/PlayerSystem.hpp" +#include "../sys/AnimationSystem.hpp" namespace rid { @@ -22,6 +24,7 @@ namespace rid m_ecs.add_system(std::make_unique()); m_ecs.add_system(std::make_unique()); m_ecs.add_system(std::make_unique()); + m_ecs.add_system(std::make_unique()); EntityFactory factory {m_ecs}; factory.create_player(0, 512.f, 512.f); diff --git a/src/comps/AnimationC.hpp b/src/comps/AnimationC.hpp new file mode 100644 index 0000000..bc74b3d --- /dev/null +++ b/src/comps/AnimationC.hpp @@ -0,0 +1,25 @@ +#ifndef rid_ANIMATIONC_HPP +#define rid_ANIMATIONC_HPP + +#include "conf.hpp" +#include "../BaseComponent.hpp" + +namespace rid +{ + struct AnimationC: public BaseComponent + { + float time; + float timer = 0.0f; + + size_t duration; + size_t current = 0; + + explicit AnimationC(float _time, size_t _duration) + : time { _time } + , duration { _duration } + { + } + }; +} + +#endif diff --git a/src/conf.in.hpp b/src/conf.in.hpp index c36570e..5a53869 100644 --- a/src/conf.in.hpp +++ b/src/conf.in.hpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/src/sys/AnimationSystem.cpp b/src/sys/AnimationSystem.cpp new file mode 100644 index 0000000..203ee74 --- /dev/null +++ b/src/sys/AnimationSystem.cpp @@ -0,0 +1,46 @@ +#include "AnimationSystem.hpp" +#include "../ECS.hpp" +#include "../comps/AnimationC.hpp" +#include "../comps/SpriteC.hpp" +#include "../comps/BodyC.hpp" + +namespace rid +{ + /*explicit*/ AnimationSystem::AnimationSystem() + : BaseSystem() + { + auto texture = std::make_unique(); + texture->load(RID_DATADIR / "assets" / "images" / "walk_0.png"); + + m_canvas.set_texture(std::move(texture)); + } + + /*virtual*/ AnimationSystem::~AnimationSystem() + { + } + + void AnimationSystem::update(ECS& ecs, std::vector const& entities) /*override*/ + { + for (auto entity: entities) + { + auto& anim = ecs.getc(entity); + auto& sprite = ecs.getc(entity); + auto& body = ecs.getc(entity); + + if (glm::length(body.vel) > 0.0f && anim.timer >= anim.time) + { + anim.current++; + anim.current %= anim.duration; + anim.timer = 0.0f; + sprite.frame.x = sprite.frame.w * anim.current; + } + + anim.timer += ecs.dt(); + } + } + + void AnimationSystem::draw(ECS& ecs, std::vector const& entities) /*override*/ + { + } + +} diff --git a/src/sys/AnimationSystem.hpp b/src/sys/AnimationSystem.hpp new file mode 100644 index 0000000..fe46183 --- /dev/null +++ b/src/sys/AnimationSystem.hpp @@ -0,0 +1,24 @@ +#ifndef rid_ANIMATIONSYSTEM_HPP +#define rid_ANIMATIONSYSTEM_HPP + +#include "conf.hpp" +#include "../BaseSystem.hpp" +#include "../gfx/Canvas.hpp" + +namespace rid +{ + class AnimationSystem: public BaseSystem + { + public: + explicit AnimationSystem(); + virtual ~AnimationSystem(); + + void update(ECS& ecs, std::vector const& entities) override; + void draw(ECS& ecs, std::vector const& entities) override; + + private: + Canvas m_canvas; + }; +} + +#endif