Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::animator Class Referencefinal

Public Member Functions

 animator (std::vector< std::pair< std::shared_ptr< texture >, uint16_t > > const &frames={}, std::vector< std::weak_ptr< animatable > > const &targets={})
 
std::vector< std::pair< std::shared_ptr< texture >, uint16_t > > const & frames () const
 
std::vector< std::pair< std::shared_ptr< texture >, uint16_t > > & frames ()
 
std::vector< std::weak_ptr< animatable > > const & targets () const
 
std::vector< std::weak_ptr< animatable > > & targets ()
 
size_t current_frame () const
 
void set_current_frame (size_t current_frame)
 
bool play ()
 
void reset ()
 

Detailed Description

Definition at line 35 of file animator.hpp.

Constructor & Destructor Documentation

◆ animator()

wze::animator::animator ( std::vector< std::pair< std::shared_ptr< texture >, uint16_t > > const & frames = {},
std::vector< std::weak_ptr< animatable > > const & targets = {} )
explicit

Definition at line 28 of file animator.cpp.

30 {
31 this->frames() = frames;
32 this->targets() = targets;
33 set_current_frame(0);
34 _remaining_time = 0;
35}

Member Function Documentation

◆ frames() [1/2]

std::vector< std::pair< std::shared_ptr< wze::texture >, uint16_t > > const & wze::animator::frames ( ) const
nodiscard

Definition at line 38 of file animator.cpp.

38 {
39 return _frames;
40}

◆ frames() [2/2]

std::vector< std::pair< std::shared_ptr< wze::texture >, uint16_t > > & wze::animator::frames ( )
nodiscard

Definition at line 43 of file animator.cpp.

43 {
44 return _frames;
45}

◆ targets() [1/2]

std::vector< std::weak_ptr< wze::animatable > > const & wze::animator::targets ( ) const
nodiscard

Definition at line 48 of file animator.cpp.

48 {
49 return _targets;
50}

◆ targets() [2/2]

std::vector< std::weak_ptr< wze::animatable > > & wze::animator::targets ( )
nodiscard

Definition at line 52 of file animator.cpp.

52 {
53 return _targets;
54}

◆ current_frame()

size_t wze::animator::current_frame ( ) const
nodiscard

Definition at line 56 of file animator.cpp.

56 {
57 return _current_frame;
58}

◆ set_current_frame()

void wze::animator::set_current_frame ( size_t current_frame)

Definition at line 60 of file animator.cpp.

60 {
61 _current_frame = current_frame;
62}

◆ play()

bool wze::animator::play ( )

Definition at line 64 of file animator.cpp.

64 {
65 uint32_t elapsed_time;
66 bool looped;
67
68 if (frames().empty()) {
69 return false;
70 }
71
72 if ((bool)frames().at(current_frame()).second) {
73 elapsed_time = (uint32_t)timer::delta_time() + _remaining_time;
74 set_current_frame(current_frame() +
75 elapsed_time / frames().at(current_frame()).second);
76 _remaining_time = elapsed_time % frames().at(current_frame()).second;
77 } else {
78 set_current_frame(current_frame() + 1);
79 _remaining_time = 0;
80 }
81
82 looped = frames().size() <= current_frame();
83 if (looped) {
84 set_current_frame(current_frame() % frames().size());
85 }
86
87 targets().erase(
88 std::remove_if(targets().begin(), targets().end(),
89 [&](std::weak_ptr<animatable> const& target) -> bool {
90 std::shared_ptr<animatable> locked;
91
92 locked = target.lock();
93 if (locked) {
94 if (locked->animated()) {
95 locked->set_texture(
96 frames().at(current_frame()).first);
97 }
98 return false;
99 }
100
101 return true;
102 }),
103 targets().end());
104
105 return looped;
106}
static float delta_time()
Gets the current delta time in milliseconds.
Definition timer.cpp:41

◆ reset()

void wze::animator::reset ( )

Definition at line 108 of file animator.cpp.

108 {
109 set_current_frame(0);
110 _remaining_time = 0;
111}

The documentation for this class was generated from the following files: