Nymph Game Engine
Chaiscript based Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tile_animator.hpp
Go to the documentation of this file.
1 #ifndef TILE_ANIMATOR_H
2 #define TILE_ANIMATOR_H
3 #include <glm/glm.hpp>
4 #include "component.h"
5 #include "graphics/vertex_data.h"
6 #include "events/subject.h"
8 #include "set_active_event.h"
10 #include "cloneable.hpp"
11 
12 namespace Graphics {
16  template<typename StateType>
17  class TileAnimator : public Component, public Cloneable<TileAnimator<StateType>> {
18  private:
19  glm::vec2 multiplier;
20  //in ms
21  float frame_time_accumulator;
22  unsigned int tile_width;
23  unsigned int tile_height;
24  unsigned int tileset_width;
25  unsigned int tileset_height;
26 
27  std::map<StateType, std::list<std::pair<glm::ivec2, unsigned int>>> triggerable_animations;
28  StateType current_state;
29 
30  public:
31  TileAnimator() = delete;
40  TileAnimator(const unsigned int tileset_width, const unsigned int tileset_height, const unsigned int tile_width_pixels, const unsigned int tile_height_pixels) : current_state((StateType)0), frame_time_accumulator(0.0), tile_width(tile_width_pixels), tile_height(tile_height_pixels), tileset_width(tileset_width), tileset_height(tileset_height) {
41  }
52  static std::shared_ptr<TileAnimator<StateType>> create(const unsigned int tileset_width, const unsigned int tileset_height, const unsigned int tile_width, const unsigned int tile_height) {
53  return std::make_shared<TileAnimator<StateType>>(tileset_width, tileset_height, tile_width, tile_height);
54  }
60  void setStartingState(const StateType& state) {
61  current_state = state;
62  }
71  void addFrameFront(const StateType& state, const glm::ivec2& frame_pos, const unsigned int frame_time, bool set_current = false) {
72  if(set_current)
73  current_state = state;
74  triggerable_animations[state].push_front(std::pair<glm::ivec2, unsigned int>(frame_pos, frame_time));
75  }
76 
85  void addFrameBack(const StateType& state, const glm::ivec2& frame_pos, const unsigned int frame_time, bool set_current = false){
86  if(set_current)
87  current_state = state;
88  triggerable_animations[state].push_back(std::pair<glm::ivec2, unsigned int>(frame_pos, frame_time));
89  }
90 
96  void popFrameFront(const StateType& state) {
97  if(triggerable_animations[state].size() > 0)
98  triggerable_animations[state].pop_front();
99  }
100 
106  void popFrameBack(const StateType& state) {
107  if(triggerable_animations[state].size() > 0)
108  triggerable_animations[state].pop_back();
109  }
110 
116  void triggerAnimation(const StateType& state) {
117  current_state = state;
118  }
119 
120  virtual std::string className() const noexcept override {
121  return "Graphics::TileAnimator";
122  }
123 
124  virtual void onStart() override {
125  frame_time_accumulator = 0.0;
126 
127  if(triggerable_animations[current_state].size() > 0) {
128  Uniform tile_coord;
129  tile_coord.setData("tile_coord", triggerable_animations[current_state].front().first);
130  notify(SetUniformEvent::create(tile_coord));
131 
132  float normalized_width = float(tile_width) / float(tileset_width);
133  float normalized_height = float(tile_height) / float(tileset_height);
134  multiplier = glm::vec2(normalized_width, normalized_height);
135 
136  Uniform tile_coord_multiplier;
137  tile_coord_multiplier.setData("tile_coord_multiplier", multiplier);
138  notify(SetUniformEvent::create(tile_coord_multiplier));
139  }
140  }
141 
142  virtual bool onUpdate(const double delta) override {
143  if(!active)
144  return false;
145 
146  while(eventsWaiting()) {
148  }
149 
150  frame_time_accumulator += delta;
151  if(triggerable_animations[current_state].size() > 0) {
152  if(frame_time_accumulator > triggerable_animations[current_state].front().second) {
153  triggerable_animations[current_state].push_back(triggerable_animations[current_state].front());
154  triggerable_animations[current_state].pop_front();
155  frame_time_accumulator = 0.0;
156  Uniform tile_coord;
157  tile_coord.setData(std::string("tile_coord"), triggerable_animations[current_state].front().first);
158  notifyNow(SetUniformEvent::create(tile_coord));
159  Uniform tile_coord_multiplier;
160  tile_coord_multiplier.setData(std::string("tile_coord_multiplier"), multiplier);
161  notifyNow(SetUniformEvent::create(tile_coord_multiplier));
162  }
163 
164  return true;
165  }
166  }
167 
168  virtual void onDestroy() override {
169  triggerable_animations.clear();
170  }
171 
172  void handleQueuedEvent(std::shared_ptr<Events::Event> event) override {
173  switch(event->getEventType()) {
175  auto casted_event = std::static_pointer_cast<Game::AnimationTriggerEvent<StateType>>(event);
176  triggerAnimation(casted_event->getState());
177  break;
178  }
179  default:
181  break;
182  }
183  }
184 
185  void onNotifyNow(std::shared_ptr<Events::Event> event) override {
186  handleQueuedEvent(event);
187  }
188 
189  virtual unsigned long long getValueForSorting() const noexcept override {
190  return getId();
191  }
192 
193  virtual void log(el::base::type::ostream_t& os) const {
194  os << "Current State: " << current_state << " Tile Width: "<<tile_width<<" Tile Height: "<<tile_height<<" Tileset Width: "<<tileset_width<<" Tileset Height: "<<tileset_height;
195  Component::log(os);
196  }
197  };
198 }
199 
200 #endif
void popFrameBack(const StateType &state)
Pops a frame from the back.
Definition: tile_animator.hpp:106
unsigned int getId() const noexcept
Gets the identifier.
Definition: component.cpp:55
virtual void log(el::base::type::ostream_t &os) const override
Definition: component.cpp:73
void setStartingState(const StateType &state)
Sets the starting state.
Definition: tile_animator.hpp:60
virtual void notify(std::shared_ptr< Event > event)
notify is used to tell observers of an event.
Definition: subject.cpp:13
Base Class for all components.
Definition: component.h:20
Class for shader uniform.
Definition: uniform.h:10
Class for animation trigger event.
Definition: animation_trigger_event.hpp:13
virtual void onStart() override
Called when the engine starts and when a new scene is loaded.
Definition: tile_animator.hpp:124
virtual std::string className() const noexceptoverride
Returns a string representing the class name.
Definition: tile_animator.hpp:120
Class for tile animator.
Definition: tile_animator.hpp:17
Definition: cloneable.hpp:6
virtual bool onUpdate(const double delta) override
Called every engine loop.
Definition: tile_animator.hpp:142
void popFrameFront(const StateType &state)
Pops a frame from the front.
Definition: tile_animator.hpp:96
bool active
Definition: component.h:24
void onNotifyNow(std::shared_ptr< Events::Event > event) override
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
Definition: tile_animator.hpp:185
void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: tile_animator.hpp:172
virtual void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: component.cpp:16
Definition: event_type.h:30
void setData(const std::string &name, const T &data)
Sets the data.
virtual void notifyNow(std::shared_ptr< Event > event)
notifyNow is used to tell observers of an event as an interrupt.
Definition: subject.cpp:19
void addFrameFront(const StateType &state, const glm::ivec2 &frame_pos, const unsigned int frame_time, bool set_current=false)
Adds a frame to the front.
Definition: tile_animator.hpp:71
std::shared_ptr< Event > getEvent()
Definition: observer.h:17
static std::shared_ptr< TileAnimator< StateType > > create(const unsigned int tileset_width, const unsigned int tileset_height, const unsigned int tile_width, const unsigned int tile_height)
Factory function for TileAnimator.
Definition: tile_animator.hpp:52
bool eventsWaiting() const noexcept
Definition: observer.h:28
virtual void log(el::base::type::ostream_t &os) const
Definition: tile_animator.hpp:193
virtual unsigned long long getValueForSorting() const noexceptoverride
Gets the value for sorting.
Definition: tile_animator.hpp:189
TileAnimator(const unsigned int tileset_width, const unsigned int tileset_height, const unsigned int tile_width_pixels, const unsigned int tile_height_pixels)
TileAnimator Constructor.
Definition: tile_animator.hpp:40
void triggerAnimation(const StateType &state)
Triggers animation of state.
Definition: tile_animator.hpp:116
virtual void onDestroy() override
Called when the engine is shutting down.
Definition: tile_animator.hpp:168
static std::shared_ptr< SetUniformEvent > create(const Uniform &uniform)
Factory function for SetUniformEvent.
Definition: set_uniform_event.h:33
void addFrameBack(const StateType &state, const glm::ivec2 &frame_pos, const unsigned int frame_time, bool set_current=false)
Adds a frame to the back.
Definition: tile_animator.hpp:85