Fix deserialization of shogi promotions.
[tagua/yd.git] / src / animation.h
blobfa863939b072bb856099549bbe8ec9bb07177ad0
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef ANIMATION_H
12 #define ANIMATION_H
14 #include <list>
15 #include <vector>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18 #include <QPoint>
19 #include "sprite.h"
20 #include "fwd.h"
22 class Movement;
23 class Random;
25 class Animation : public boost::enable_shared_from_this<Animation> {
26 protected:
27 enum State {
28 Active,
29 Inactive,
30 Aborted
32 public:
33 virtual ~Animation() { }
34 virtual bool sticky() const { return false; }
36 virtual State animationAdvance(int msec) = 0;
37 virtual void stop() = 0;
38 virtual void abort() = 0;
41 class ConflictingAnimation {
42 public:
43 virtual ~ConflictingAnimation() { }
44 virtual void setTarget(const SpritePtr&) = 0;
45 virtual void setSource(const SpritePtr&) = 0;
48 class ConcreteAnimation : public Animation {
49 protected:
50 SpritePtr m_piece;
51 public:
52 ConcreteAnimation(const SpritePtr& piece);
53 virtual void stop() { }
56 class OneShotAnimation : public ConcreteAnimation {
57 protected:
58 virtual void shoot() = 0;
59 public:
60 OneShotAnimation(const SpritePtr& piece);
61 virtual State animationAdvance(int);
62 virtual void abort() { }
65 class InstantAnimation : public OneShotAnimation
66 , public ConflictingAnimation {
67 QPoint m_destination;
68 SpritePtr m_source;
69 public:
70 InstantAnimation(const SpritePtr& piece, const QPoint& destination);
71 virtual void shoot();
72 virtual void setTarget(const SpritePtr&) { }
73 virtual void setSource(const SpritePtr& source) { m_source = source; }
77 class MovementAnimation : public ConcreteAnimation
78 , public ConflictingAnimation {
79 protected:
80 SpritePtr m_source;
81 SpritePtr m_target;
82 QPoint m_destination;
83 double m_speed;
84 State m_state;
86 double m_time;
87 int m_start;
88 boost::shared_ptr<Movement> m_movement;
89 bool m_rotate;
91 virtual boost::shared_ptr<Movement> createMovement(const QPoint& from, const QPoint& to) const;
92 void start();
93 public:
94 MovementAnimation(const SpritePtr& piece, const QPoint& destination, bool rotate = false, double speed = 1.0);
95 virtual ~MovementAnimation();
96 virtual State animationAdvance(int msec);
97 virtual void stop();
98 virtual void abort();
100 virtual void setTarget(const SpritePtr& target);
101 virtual void setSource(const SpritePtr& source);
104 class KnightMovementAnimation : public MovementAnimation {
105 protected:
106 virtual boost::shared_ptr<Movement> createMovement(const QPoint& from, const QPoint& to) const;
107 public:
108 KnightMovementAnimation(const SpritePtr& piece, const QPoint& destination,
109 bool rotate, double speed = 1.0);
112 class CaptureAnimation : public OneShotAnimation {
113 protected:
114 virtual void shoot();
115 public:
116 CaptureAnimation(const SpritePtr& piece);
119 class DropAnimation : public OneShotAnimation {
120 bool m_valid_position;
121 QPoint m_position;
122 protected:
123 virtual void shoot();
124 public:
125 DropAnimation(const SpritePtr& piece);
126 DropAnimation(const SpritePtr& piece, const QPoint&);
129 class PromotionAnimation : public OneShotAnimation {
130 protected:
131 SpritePtr m_promoted;
132 virtual void shoot();
133 public:
134 PromotionAnimation(const SpritePtr& piece, const SpritePtr& promoted);
137 class DelayAnimation : public Animation {
138 State m_state;
140 int m_msecs;
141 int m_start;
142 void start();
143 public:
144 DelayAnimation(int msecs);
145 virtual State animationAdvance(int msec);
146 virtual void stop();
147 virtual void abort();
150 class FadeAnimation : public ConcreteAnimation {
151 int m_fadeFrom;
152 int m_fadeTo;
153 QPoint m_to;
154 State m_state;
156 int m_start;
157 void start();
158 public:
159 FadeAnimation(const SpritePtr& sprite, const QPoint& pos, int fadeFrom, int fadeTo);
160 FadeAnimation(const SpritePtr& sprite, int fadeFrom, int fadeTo);
161 virtual State animationAdvance(int msec);
162 virtual void stop();
163 virtual void abort();
166 class GrowAnimation : public ConcreteAnimation {
167 State m_state;
168 int m_start;
170 void start();
171 public:
172 GrowAnimation(const SpritePtr& sprite);
173 virtual State animationAdvance(int msec);
174 virtual void stop();
175 virtual void abort();
178 class ExplodeAnimation : public ConcreteAnimation {
179 State m_state;
180 Random& m_random;
182 int m_start;
183 void start();
184 public:
185 ExplodeAnimation(const SpritePtr& sprite, Random& random);
186 virtual State animationAdvance(int msec);
187 virtual void stop();
188 virtual void abort();
192 * @brief Two groups of animations, to be executed one after the other.
194 * A double list of animations: pre-animations and post-animations.
195 * When an AnimationGroup is advanced, pre-animations are handled first,
196 * and removed from the list as they finish animating.
197 * When all pre-animations are gone, post-animations are handled.
199 class AnimationGroup : public Animation {
200 private:
201 typedef std::list<AnimationPtr> AnimationList;
202 typedef AnimationList::iterator Iterator;
203 AnimationList pre;
204 AnimationList post;
206 protected:
207 bool m_active;
208 bool m_persistent;
209 bool m_chain_abortions;
211 virtual void start();
212 bool empty() const;
213 public:
214 AnimationGroup(bool persistent = false);
216 virtual bool sticky() const { return m_persistent; }
217 void setPersistent(bool value) { m_persistent = value; }
218 void setChainAbortions(bool value) { m_chain_abortions = value; }
220 void addPreAnimation(AnimationPtr);
221 void addPostAnimation(AnimationPtr);
223 virtual State animationAdvance(int msec);
224 virtual void stop();
225 virtual void abort() { stop(); }
228 class TeleportAnimation : public AnimationGroup {
229 public:
230 TeleportAnimation(const SpritePtr& sprite, const QPoint& from, const QPoint& to);
231 TeleportAnimation(const SpritePtr& sprite, const QPoint& to);
234 class CrossFadingAnimation : public AnimationGroup {
235 SpritePtr m_piece;
236 protected:
237 virtual void start();
238 public:
239 CrossFadingAnimation(const SpritePtr& piece, const SpritePtr& promoted);
242 class DelayedAnimationSet : public Animation {
243 class Event {
244 int m_index;
245 int m_time;
246 public:
247 Event(int index, int time)
248 : m_index(index), m_time(time) { }
249 int index() const { return m_index; }
250 int time() const { return m_time; }
252 bool operator<(const Event& other) const { return m_time < other.m_time; }
255 int m_time;
256 int m_start;
257 Random& m_random;
258 State m_state;
259 std::vector<boost::shared_ptr<Animation> > m_animations;
260 AnimationGroup m_group;
261 std::vector<Event> m_events;
262 std::vector<Event>::iterator m_next_event;
264 void execute(int);
265 public:
266 DelayedAnimationSet(Random& random);
267 void addAnimation(const boost::shared_ptr<Animation>& animation);
268 void start();
269 virtual State animationAdvance(int msec);
270 virtual void stop();
271 virtual void abort();
274 #endif // ANIMATION_H