Actor animation.

main
bog 2023-11-14 20:46:05 +01:00
parent b605d7393d
commit f62614191f
7 changed files with 102 additions and 0 deletions

View File

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

View File

@ -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<PlayerC>(e, controller);
m_ecs.attach_component<AnimationC>(e, 0.1f, 4);
return e;
}

View File

@ -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<BodyC>(std::make_unique<BodySystem>());
m_ecs.add_system<BodyC, SpriteC>(std::make_unique<ActorSystem>());
m_ecs.add_system<PlayerC, BodyC>(std::make_unique<PlayerSystem>());
m_ecs.add_system<SpriteC, AnimationC, BodyC>(std::make_unique<AnimationSystem>());
EntityFactory factory {m_ecs};
factory.create_player(0, 512.f, 512.f);

25
src/comps/AnimationC.hpp Normal file
View File

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

View File

@ -6,6 +6,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <optional>
#include <unordered_map>
#include <filesystem>
#include <stdexcept>
#include <string>

View File

@ -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>();
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<size_t> const& entities) /*override*/
{
for (auto entity: entities)
{
auto& anim = ecs.getc<AnimationC>(entity);
auto& sprite = ecs.getc<SpriteC>(entity);
auto& body = ecs.getc<BodyC>(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<size_t> const& entities) /*override*/
{
}
}

View File

@ -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<size_t> const& entities) override;
void draw(ECS& ecs, std::vector<size_t> const& entities) override;
private:
Canvas m_canvas;
};
}
#endif