hx3d  1
2D/3D Simple Game Framework
parallel.cpp
1 /*
2  Parallel 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/parallel.hpp"
22 
23 #include "hx3d/tweens/callback.hpp"
24 
25 namespace hx3d {
26 namespace tweens {
27 
28 Parallel::Parallel(bool infinite): BaseTween(infinite) {}
29 
30 void Parallel::add(const Ptr<BaseTween>& tween) {
31  tweens.push_back(tween);
32 }
33 
34 void Parallel::addRepeatingCallback(std::function<void()> func, const float duration) {
35  Ptr<BaseTween> tween = Make<Callback>(func, duration);
36  tweens.push_back(tween);
37 }
38 
40  for (auto i = tweens.begin(); i != tweens.end();) {
41  const Ptr<BaseTween>& tween = *i;
42  tween->reset();
43 
44  ++i;
45  }
46 }
47 
48 void Parallel::update(const float delta) {
49  if (_ended)
50  return;
51 
52  bool all_ended = true;
53  for (auto i = tweens.begin(); i != tweens.end();) {
54  const Ptr<BaseTween>& tween = *i;
55 
56  if (!tween->hasEnded()) {
57  all_ended = false;
58  tween->update(delta);
59  }
60 
61  ++i;
62  }
63 
64  if (all_ended) {
65  if (_infinite) {
66  reset();
67  } else {
68  _ended = true;
69  }
70  }
71 }
72 
73 } /* tweens */
74 } /* hx3d */
bool _infinite
Is the tween infinite ?
Definition: base_tween.hpp:62
Parallel(bool infinite=false)
Definition: parallel.cpp:28
virtual void reset() override
Reset the tween.
Definition: parallel.cpp:39
hx3d framework namespace
Definition: audio.hpp:26
bool _ended
Has the tween ended ?
Definition: base_tween.hpp:59
void addRepeatingCallback(std::function< void()> func, const float duration)
Create a repeating callback.
Definition: parallel.cpp:34
void add(const Ptr< BaseTween > &tween)
Add an existing tween.
Definition: parallel.cpp:30
virtual void update(const float delta) override
Update the tween.
Definition: parallel.cpp:48
Base abstract tween.
Definition: base_tween.hpp:34
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34