hx3d  1
2D/3D Simple Game Framework
sequence.cpp
1 /*
2  Tween sequence.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/tweens/sequence.hpp"
22 
23 namespace hx3d {
24 namespace tweens {
25 
26 Sequence::Sequence(bool infinite): BaseTween(infinite) {}
27 
28 void Sequence::addDelay(const float delay) {
29  Ptr<BaseTween> tween = Make<Delay>(delay);
30  tweens.push(tween);
31 }
32 
33 void Sequence::addCallback(std::function<void()> func) {
34  Ptr<BaseTween> tween = Make<Callback>(func);
35  tweens.push(tween);
36 }
37 
38 void Sequence::add(const Ptr<BaseTween>& tween) {
39  tweens.push(tween);
40 }
41 
43  while (!doneTweens.empty()) {
44  auto& tween = doneTweens.top();
45 
46  tween.reset();
47  tweens.push(tween);
48 
49  doneTweens.pop();
50  }
51 }
52 
53 void Sequence::update(const float delta) {
54  if (_ended)
55  return;
56 
57  const Ptr<BaseTween>& tween = tweens.front();
58  if (tween->hasEnded()) {
59  doneTweens.push(tween);
60  tweens.pop();
61  } else {
62  tween->update(delta);
63  }
64 
65  if (tweens.empty()) {
66  if (_infinite) {
67  reset();
68  } else {
69  _ended = true;
70  }
71  }
72 }
73 
74 } /* tweens */
75 } /* hx3d */
virtual void update(const float delta) override
Update the tween.
Definition: sequence.cpp:53
void addCallback(std::function< void()> func)
Create a callback tween.
Definition: sequence.cpp:33
Sequence(bool infinite=false)
Definition: sequence.cpp:26
bool _infinite
Is the tween infinite ?
Definition: base_tween.hpp:62
void addDelay(const float delay)
Create a delay tween.
Definition: sequence.cpp:28
virtual void reset() override
Reset the tween.
Definition: sequence.cpp:42
hx3d framework namespace
Definition: audio.hpp:26
bool _ended
Has the tween ended ?
Definition: base_tween.hpp:59
Base abstract tween.
Definition: base_tween.hpp:34
void add(const Ptr< BaseTween > &tween)
Add an existing tween.
Definition: sequence.cpp:38
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34