1 #ifndef TILE_ANIMATOR_H
2 #define TILE_ANIMATOR_H
16 template<
typename StateType>
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;
27 std::map<StateType, std::list<std::pair<glm::ivec2, unsigned int>>> triggerable_animations;
28 StateType current_state;
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) {
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);
61 current_state = state;
71 void addFrameFront(
const StateType& state,
const glm::ivec2& frame_pos,
const unsigned int frame_time,
bool set_current =
false) {
73 current_state = state;
74 triggerable_animations[state].push_front(std::pair<glm::ivec2, unsigned int>(frame_pos, frame_time));
85 void addFrameBack(
const StateType& state,
const glm::ivec2& frame_pos,
const unsigned int frame_time,
bool set_current =
false){
87 current_state = state;
88 triggerable_animations[state].push_back(std::pair<glm::ivec2, unsigned int>(frame_pos, frame_time));
97 if(triggerable_animations[state].size() > 0)
98 triggerable_animations[state].pop_front();
107 if(triggerable_animations[state].size() > 0)
108 triggerable_animations[state].pop_back();
117 current_state = state;
120 virtual std::string
className() const noexcept
override {
121 return "Graphics::TileAnimator";
125 frame_time_accumulator = 0.0;
127 if(triggerable_animations[current_state].size() > 0) {
129 tile_coord.
setData(
"tile_coord", triggerable_animations[current_state].front().first);
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);
137 tile_coord_multiplier.
setData(
"tile_coord_multiplier", multiplier);
142 virtual bool onUpdate(
const double delta)
override {
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;
157 tile_coord.
setData(std::string(
"tile_coord"), triggerable_animations[current_state].front().first);
160 tile_coord_multiplier.
setData(std::string(
"tile_coord_multiplier"), multiplier);
169 triggerable_animations.clear();
173 switch(event->getEventType()) {
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;
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 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
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
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