72 lines
2.0 KiB
C++
72 lines
2.0 KiB
C++
/* +------------------------------------------------------+
|
|
____/ \____ /| - Open source game framework licensed to freely use, |
|
|
\ / / | copy, modify and sell without restriction |
|
|
+--\ ^__^ /--+ | |
|
|
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
| SPACE ~~~~~ | /
|
|
| ~~~~~~~ BOX |/
|
|
+-------------*/
|
|
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <functional>
|
|
#include <algorithm>
|
|
#include "Timer.hpp"
|
|
|
|
typedef std::function<void()> callback;
|
|
|
|
class Node;
|
|
|
|
class Animation
|
|
{
|
|
|
|
private:
|
|
|
|
bool play_state = false, ending = false, paused = false;
|
|
float delay = 0.0f, overflow = 0.0f, frame_length = 0.0f, previous_step_time = 0.0f;
|
|
callback step = nullptr;
|
|
Node* containing_object = nullptr;
|
|
sb::Timer timer = sb::Timer();
|
|
|
|
public:
|
|
|
|
Animation();
|
|
|
|
/*!
|
|
* Constructor that allows passing a pointer to an object and a pointer to one of its
|
|
* member functions that will be used as the animation function. Frame length can be supplied
|
|
* in seconds, representing how often the animation function will run. If omitted, the
|
|
* animation function will run every time the Animation object is updated.
|
|
*/
|
|
template<typename T>
|
|
Animation(void(T::*member_function)(), T* object, float frame_length = 0) : frame_length(frame_length)
|
|
{
|
|
bind(member_function, object);
|
|
timer.off();
|
|
}
|
|
|
|
/*!
|
|
* Set the animation function by supplying an object and one of its member functions.
|
|
*/
|
|
template<typename T>
|
|
void bind(void(T::*f)(), T* o)
|
|
{
|
|
step = std::bind(f, o);
|
|
containing_object = static_cast<Node*>(o);
|
|
}
|
|
|
|
void set_frame_length(float);
|
|
void play(float = 0, bool = false);
|
|
void play_once(float = 0);
|
|
void pause();
|
|
void unpause();
|
|
void reset();
|
|
bool playing(bool = true) const;
|
|
void update(float timestamp);
|
|
|
|
};
|
|
|
|
#include "Node.hpp"
|