ScummVM API documentation
motor.h
1
2/* ScummVM - Graphic Adventure Engine
3 *
4 * ScummVM is the legal property of its developers, whose names
5 * are too numerous to list here. Please refer to the COPYRIGHT
6 * file distributed with this source distribution.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef TWP_MOTOR_H
24#define TWP_MOTOR_H
25
26#include "math/vector2d.h"
27#include "twp/camera.h"
28#include "twp/util.h"
29#include "twp/lip.h"
30
31namespace Twp {
32
33template<typename T>
34struct Tween {
35public:
36 Tween(T f, T t, float d, InterpolationMethod im)
37 : frm(f), to(t), delta(t - f), duration(d), value(f), easing_f(easing(im.kind)), swing(im.swing), loop(im.loop) {
38 }
39
40 bool running() {
41 if (swing || loop) {
42 return true;
43 }
44 return elapsed < duration;
45 }
46
47 void update(float el) {
48 if (enabled && running()) {
49 elapsed += el;
50 float f = CLIP(elapsed / duration, 0.0f, 1.0f);
51 if (!dir_forward)
52 f = 1.0f - f;
53 if ((elapsed > duration) && (swing || loop)) {
54 elapsed = elapsed - duration;
55 if (swing)
56 dir_forward = !dir_forward;
57 }
58 if (easing_f.func) {
59 f = easing_f.func(f);
60 value = frm + delta * f;
61 }
62 } else {
63 value = to;
64 }
65 }
66
67 T current() const { return value; }
68
69public:
70 T frm, to, delta;
71 float elapsed = 0.f;
72 float duration = 0.f; // duration in ms
73 T value;
74 EasingFunc_t easing_f;
75 bool enabled = true;
76 bool dir_forward = true;
77 bool swing = false;
78 bool loop = false;
79};
80
81class Motor {
82public:
83 virtual ~Motor() {}
84 virtual void disable() {
85 _enabled = false;
86 }
87 virtual bool isEnabled() const { return _enabled; }
88 void update(float elapsed);
89
90protected:
91 virtual void onUpdate(float elapsed) = 0;
92
93protected:
94 bool _enabled = true;
95};
96
97class Object;
98class OffsetTo : public Motor {
99public:
100 virtual ~OffsetTo();
101 OffsetTo(float duration, Common::SharedPtr<Object> obj, const Math::Vector2d &pos, InterpolationMethod im);
102
103private:
104 virtual void onUpdate(float elasped) override;
105
106private:
109};
110
111class MoveTo : public Motor {
112public:
113 virtual ~MoveTo();
114 MoveTo(float duration, Common::SharedPtr<Object> obj, const Math::Vector2d &pos, InterpolationMethod im);
115
116private:
117 virtual void onUpdate(float elasped) override;
118
119private:
122};
123
124class AlphaTo : public Motor {
125public:
126 virtual ~AlphaTo();
127 AlphaTo(float duration, Common::SharedPtr<Object> obj, float to, InterpolationMethod im);
128
129private:
130 virtual void onUpdate(float elasped) override;
131
132private:
134 Tween<float> _tween;
135};
136
137class Node;
138class RotateTo : public Motor {
139public:
140 virtual ~RotateTo();
141 RotateTo(float duration, Node *obj, float to, InterpolationMethod im);
142
143private:
144 virtual void onUpdate(float elasped) override;
145
146private:
147 Node *_node = nullptr;
148 Tween<float> _tween;
149};
150
151class RoomRotateTo : public Motor {
152public:
153 virtual ~RoomRotateTo();
155
156private:
157 virtual void onUpdate(float elasped) override;
158
159private:
161 Tween<float> _tween;
162};
163
164class ScaleTo : public Motor {
165public:
166 virtual ~ScaleTo();
167 ScaleTo(float duration, Node *node, float to, InterpolationMethod im);
168
169private:
170 virtual void onUpdate(float elasped) override;
171
172private:
173 Node *_node = nullptr;
174 Tween<float> _tween;
175};
176
177class Shake : public Motor {
178public:
179 virtual ~Shake();
180 Shake(Node *node, float amount);
181
182private:
183 virtual void onUpdate(float elasped) override;
184
185private:
186 Node *_node = nullptr;
187 float _amount = 0.f;
188 float _shakeTime = 0.f;
189 float _elapsed = 0.f;
190};
191
192class OverlayTo : public Motor {
193public:
194 virtual ~OverlayTo();
195 OverlayTo(float duration, Common::SharedPtr<Room> room, const Color &to);
196
197 virtual void onUpdate(float elapsed) override;
198
199private:
201 Color _to;
202 Tween<Color> _tween;
203};
204
205class ReachAnim : public Motor {
206public:
207 virtual ~ReachAnim();
209
210 virtual void onUpdate(float elasped) override;
211
212private:
213 void playReachAnim();
214
215private:
218 int _state = 0;
219 float _elapsed = 0.f;
220};
221
222enum WalkToState {
223 kWalking,
224 kArrived,
225 kReach
226};
227
228class WalkTo : public Motor {
229public:
230 WalkTo(Common::SharedPtr<Object> obj, const Math::Vector2d &dest, int facing = 0);
231 void disable() override;
232
233 const Common::Array<Math::Vector2d> &getPath() const { return _path; }
234
235private:
236 void actorArrived();
237 void onUpdate(float elapsed) override;
238
239private:
242 int _facing = 0;
243 float _wsd;
244 WalkToState _state = kWalking;
245};
246
247class TextNode;
248
249class TalkingBase : public Motor {
250protected:
251 TalkingBase(Common::SharedPtr<Object> actor, float duration);
252
253public:
254 virtual ~TalkingBase() {}
255
256protected:
257 Common::String talkieKey();
258 int onTalkieId(int id);
259 int loadActorSpeech(const Common::String &name);
260 void setDuration(const Common::String &text);
261 float getTalkSpeed() const;
262
263protected:
265 float _duration = 0.f;
266 float _elapsed = 0.f;
267};
268
269// Creates a talking animation for a specified object.
270class Talking : public TalkingBase {
271public:
272 Talking(Common::SharedPtr<Object> obj, const Common::StringArray &texts, const Color &color);
273 virtual ~Talking() {}
274
275 void append(const Common::StringArray &texts, const Color &color);
276
277 virtual void onUpdate(float elapsed) override;
278 virtual void disable() override;
279
280private:
281 void say(const Common::String &text);
282
283private:
285 Lip _lip;
286 Color _color;
287 Common::StringArray _texts;
288};
289
290class SayLineAt : public TalkingBase {
291public:
292 SayLineAt(const Math::Vector2d &pos, const Color &color, Common::SharedPtr<Object> actor, float duration, const Common::String &text);
293 virtual ~SayLineAt() {}
294
295 virtual void onUpdate(float elapsed) override;
296 virtual void disable() override;
297
298private:
299 void say(const Common::String &text);
300
301private:
302 const Math::Vector2d _pos;
303 Color _color;
304 Common::String _text;
306};
307
308class Jiggle : public Motor {
309public:
310 Jiggle(Node *node, float amount);
311 virtual ~Jiggle();
312
313private:
314 virtual void onUpdate(float elapsed) override;
315
316private:
317 Node *_node = nullptr;
318 float _amount = 0.f;
319 float _jiggleTime = 0.f;
320};
321
322class MoveCursorTo : public Motor {
323public:
324 MoveCursorTo(const Math::Vector2d &pos, float time);
325 virtual ~MoveCursorTo() {}
326
327private:
328 virtual void onUpdate(float elapsed) override;
329
330private:
332 Math::Vector2d _pos;
333};
334
335} // namespace Twp
336
337#endif
Definition: str.h:59
Definition: motor.h:124
Definition: motor.h:308
Definition: lip.h:41
Definition: motor.h:81
Definition: motor.h:322
Definition: motor.h:111
Definition: scenegraph.h:41
Definition: object.h:115
Definition: motor.h:98
Definition: motor.h:192
Definition: motor.h:205
Definition: motor.h:151
Definition: motor.h:138
Definition: motor.h:290
Definition: motor.h:164
Definition: motor.h:177
Definition: motor.h:249
Definition: motor.h:270
Definition: scenegraph.h:200
Definition: motor.h:228
T CLIP(T v, T amin, T amax)
Definition: util.h:69
Definition: gfx.h:35
Definition: easing.h:33
Definition: easing.h:46
Definition: motor.h:34