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.
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
25 class Animation
: public boost::enable_shared_from_this
<Animation
> {
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
{
43 virtual ~ConflictingAnimation() { }
44 virtual void setTarget(const SpritePtr
&) = 0;
45 virtual void setSource(const SpritePtr
&) = 0;
48 class ConcreteAnimation
: public Animation
{
52 ConcreteAnimation(const SpritePtr
& piece
);
53 virtual void stop() { }
56 class OneShotAnimation
: public ConcreteAnimation
{
58 virtual void shoot() = 0;
60 OneShotAnimation(const SpritePtr
& piece
);
61 virtual State
animationAdvance(int);
62 virtual void abort() { }
65 class InstantAnimation
: public OneShotAnimation
66 , public ConflictingAnimation
{
70 InstantAnimation(const SpritePtr
& piece
, const QPoint
& destination
);
72 virtual void setTarget(const SpritePtr
&) { }
73 virtual void setSource(const SpritePtr
& source
) { m_source
= source
; }
77 class MovementAnimation
: public ConcreteAnimation
78 , public ConflictingAnimation
{
88 boost::shared_ptr
<Movement
> m_movement
;
91 virtual boost::shared_ptr
<Movement
> createMovement(const QPoint
& from
, const QPoint
& to
) const;
94 MovementAnimation(const SpritePtr
& piece
, const QPoint
& destination
, bool rotate
= false, double speed
= 1.0);
95 virtual ~MovementAnimation();
96 virtual State
animationAdvance(int msec
);
100 virtual void setTarget(const SpritePtr
& target
);
101 virtual void setSource(const SpritePtr
& source
);
104 class KnightMovementAnimation
: public MovementAnimation
{
106 virtual boost::shared_ptr
<Movement
> createMovement(const QPoint
& from
, const QPoint
& to
) const;
108 KnightMovementAnimation(const SpritePtr
& piece
, const QPoint
& destination
,
109 bool rotate
, double speed
= 1.0);
112 class CaptureAnimation
: public OneShotAnimation
{
114 virtual void shoot();
116 CaptureAnimation(const SpritePtr
& piece
);
119 class DropAnimation
: public OneShotAnimation
{
120 bool m_valid_position
;
123 virtual void shoot();
125 DropAnimation(const SpritePtr
& piece
);
126 DropAnimation(const SpritePtr
& piece
, const QPoint
&);
129 class PromotionAnimation
: public OneShotAnimation
{
131 SpritePtr m_promoted
;
132 virtual void shoot();
134 PromotionAnimation(const SpritePtr
& piece
, const SpritePtr
& promoted
);
137 class DelayAnimation
: public Animation
{
144 DelayAnimation(int msecs
);
145 virtual State
animationAdvance(int msec
);
147 virtual void abort();
150 class FadeAnimation
: public ConcreteAnimation
{
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
);
163 virtual void abort();
166 class GrowAnimation
: public ConcreteAnimation
{
172 GrowAnimation(const SpritePtr
& sprite
);
173 virtual State
animationAdvance(int msec
);
175 virtual void abort();
178 class ExplodeAnimation
: public ConcreteAnimation
{
185 ExplodeAnimation(const SpritePtr
& sprite
, Random
& random
);
186 virtual State
animationAdvance(int msec
);
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
{
201 typedef std::list
<AnimationPtr
> AnimationList
;
202 typedef AnimationList::iterator Iterator
;
209 bool m_chain_abortions
;
211 virtual void start();
213 AnimationGroup(bool persistent
= false);
215 virtual bool sticky() const { return m_persistent
; }
216 void setPersistent(bool value
) { m_persistent
= value
; }
217 void setChainAbortions(bool value
) { m_chain_abortions
= value
; }
219 void addPreAnimation(AnimationPtr
);
220 void addPostAnimation(AnimationPtr
);
222 virtual State
animationAdvance(int msec
);
224 virtual void abort() { stop(); }
227 class TeleportAnimation
: public AnimationGroup
{
229 TeleportAnimation(const SpritePtr
& sprite
, const QPoint
& from
, const QPoint
& to
);
230 TeleportAnimation(const SpritePtr
& sprite
, const QPoint
& to
);
233 class CrossFadingAnimation
: public AnimationGroup
{
236 virtual void start();
238 CrossFadingAnimation(const SpritePtr
& piece
, const SpritePtr
& promoted
);
241 class DelayedAnimationSet
: public Animation
{
246 Event(int index
, int time
)
247 : m_index(index
), m_time(time
) { }
248 int index() const { return m_index
; }
249 int time() const { return m_time
; }
251 bool operator<(const Event
& other
) const { return m_time
< other
.m_time
; }
258 std::vector
<boost::shared_ptr
<Animation
> > m_animations
;
259 AnimationGroup m_group
;
260 std::vector
<Event
> m_events
;
261 std::vector
<Event
>::iterator m_next_event
;
265 DelayedAnimationSet(Random
& random
);
266 void addAnimation(const boost::shared_ptr
<Animation
>& animation
);
268 virtual State
animationAdvance(int msec
);
270 virtual void abort();
273 #endif // ANIMATION_H