Untitled Game engine no.5  1.0
Animation.hpp
1 //
2 // Created by jibbo on 3/15/21.
3 //
4 
5 #ifndef ENGINE_PROJ_ANIMATION_H
6 #define ENGINE_PROJ_ANIMATION_H
7 
8 #include <utility>
9 #include "MeshRenderer.hpp"
10 #include "Scene.hpp"
11 #include "Time.hpp"
12 #include "Texture.hpp"
13 
14 namespace Engine {
16  struct Animation {
17  private:
20  bool singleTexture = true;
21 
23  size_t m_currentFrame = 0;
24  size_t m_lastFrame = 0;
26  float time = 0;
28  float spf = 0.1f;
30  float scale = 1.f;
31  public:
35  std::vector<Ref<TextureLookup>> lookup = std::vector<Ref<TextureLookup>>();
36 
37 
42  static void Update(const Scene &scene) {
43  auto ecs = scene.GetECS();
44  for (auto e : ecs.GetEntitiesWith<Animation>()) {
46  }
47  };
48 
49 
50 
51  Animation() = default;
52 
58  Animation(const Ref <MeshRenderer> &meshRenderer,
59  const std::vector<Ref<TextureLookup>>& textureLookup)
60  : renderer (meshRenderer), lookup(textureLookup) {};
61 
63  void UpdateTime() {
64  time += (float) Time::dt * scale;
65 
66  if (time >= spf) {
67  m_currentFrame++;
68  time = time - spf;
69  }
70 
71  if (m_currentFrame >= lookup.size()) {
72  m_currentFrame = 0;
73  }
74 
75  // for negative animation
76  if (time < 0) {
77  m_currentFrame--;
78  time = time + spf;
79  }
80 
81  if (m_currentFrame < 0) {
82  m_currentFrame = lookup.size() - 1;
83  }
84 
85  if (m_currentFrame != m_lastFrame) {
86  drawFrame();
87  }
88  };
89 
95  virtual void drawFrame() {
96  auto texData = lookup[m_currentFrame];
97  if (renderer == nullptr || texData == nullptr)
98  return;
99 
100  renderer->map = texData->map;
101  renderer->texture = texData->tex;
102  renderer->mesh = CreateRef<Mesh>(Mesh::quad(glm::scale(glm::vec3{texData->width, texData->height, 1})));
103  };
104  };
105 
106 
107 }
108 
109 #endif //ENGINE_PROJ_ANIMATION_H
Engine::Scene
A container for entities currently in the Scene.
Definition: Scene.hpp:9
Engine::Animation::Animation
Animation(const Ref< MeshRenderer > &meshRenderer, const std::vector< Ref< TextureLookup >> &textureLookup)
Definition: Animation.hpp:58
Engine::Ref
std::shared_ptr< T > Ref
Has stuff for making references a lot more easily shared smart pointer.
Definition: Base.hpp:21
Engine::Animation
Container for texture based animations.
Definition: Animation.hpp:16
Engine::Animation::renderer
Ref< MeshRenderer > renderer
Renderer Reference.
Definition: Animation.hpp:33
Engine::Animation::Update
static void Update(const Scene &scene)
Definition: Animation.hpp:42
Engine::ECS::GetComponent
Ref< C > GetComponent(const EntityID &id)
Definition: ECS.hpp:80
Engine::Animation::lookup
std::vector< Ref< TextureLookup > > lookup
lookup table for animation frame data
Definition: Animation.hpp:35
Engine::Animation::drawFrame
virtual void drawFrame()
Definition: Animation.hpp:95
Engine
Definition: Animation.hpp:14
Engine::Time::dt
static double dt
time since last update
Definition: Time.hpp:12
Engine::Animation::UpdateTime
void UpdateTime()
Advance frame data if needed.
Definition: Animation.hpp:63