hx3d  1
2D/3D Simple Game Framework
tween.inl.hpp
1 /*
2  Regular tween.
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 namespace hx3d {
22 namespace tweens {
23 
24 template <class T>
25 Tween<T>::Tween(T& mod, const T end, const float duration, const math::Interpolation interp, bool infinite):
26  BaseTween(infinite), mod(&mod), end(end), duration(duration), interp(interp) {
27  currentTime = 0.f;
28  _firstUpdate = false;
29 }
30 
31 template <class T>
33  _ended = false;
34  currentTime = 0;
35 }
36 
37 template <class T>
38 void Tween<T>::update(const float delta) {
39  if (_ended)
40  return;
41 
42  if (!_firstUpdate) {
43  base = *mod;
44  _firstUpdate = true;
45  }
46 
47  currentTime += delta;
48  if (currentTime < duration) {
49  const float normTime = currentTime / duration;
50  *mod = math::interpolate(base, end, normTime, interp);
51  } else {
52  if (_infinite) {
53  reset();
54  } else {
55  _ended = true;
56  }
57  }
58 }
59 
60 } /* tween */
61 } /* hx3d */
virtual void reset() override
Reset the tween.
Definition: tween.inl.hpp:32
virtual void update(const float delta) override
Update the tween.
Definition: tween.inl.hpp:38
bool _infinite
Is the tween infinite ?
Definition: base_tween.hpp:62
T interpolate(T a, T b, float t, Interpolation type)
Interpolate between two values.
hx3d framework namespace
Definition: audio.hpp:26
bool _ended
Has the tween ended ?
Definition: base_tween.hpp:59
Interpolation
Interpolate using functions.
Tween(T &mod, const T end, const float duration, const math::Interpolation interp, bool infinite=false)
Create a tween.
Definition: tween.inl.hpp:25
Base abstract tween.
Definition: base_tween.hpp:34