2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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>
20 #include <core/animation.h>
21 #include <core/namedsprite.h>
27 class ConflictingAnimation
{
29 virtual ~ConflictingAnimation() { }
30 virtual void setTarget(const SpritePtr
&) = 0;
31 virtual void setSource(const SpritePtr
&) = 0;
34 class ConcreteAnimation
: public Animation
{
38 ConcreteAnimation(const SpritePtr
& piece
);
39 virtual void stop() { }
42 class OneShotAnimation
: public ConcreteAnimation
{
44 virtual void shoot() = 0;
46 OneShotAnimation(const SpritePtr
& piece
);
47 virtual State
animationAdvance(int);
48 virtual void abort() { }
51 class InstantAnimation
: public OneShotAnimation
52 , public ConflictingAnimation
{
56 InstantAnimation(const SpritePtr
& piece
, const QPoint
& destination
);
58 virtual void setTarget(const SpritePtr
&) { }
59 virtual void setSource(const SpritePtr
& source
) { m_source
= source
; }
63 class MovementAnimation
: public ConcreteAnimation
64 , public ConflictingAnimation
{
74 boost::shared_ptr
<Movement
> m_movement
;
77 virtual boost::shared_ptr
<Movement
> createMovement(const QPoint
& from
, const QPoint
& to
) const;
80 MovementAnimation(const SpritePtr
& piece
, const QPoint
& destination
, bool rotate
= false, double speed
= 1.0);
81 virtual ~MovementAnimation();
82 virtual State
animationAdvance(int msec
);
86 virtual void setTarget(const SpritePtr
& target
);
87 virtual void setSource(const SpritePtr
& source
);
90 class KnightMovementAnimation
: public MovementAnimation
{
92 virtual boost::shared_ptr
<Movement
> createMovement(const QPoint
& from
, const QPoint
& to
) const;
94 KnightMovementAnimation(const SpritePtr
& piece
, const QPoint
& destination
,
95 bool rotate
, double speed
= 1.0);
98 class CaptureAnimation
: public OneShotAnimation
{
100 virtual void shoot();
102 CaptureAnimation(const SpritePtr
& piece
);
105 class DropAnimation
: public OneShotAnimation
{
106 bool m_valid_position
;
109 virtual void shoot();
111 DropAnimation(const SpritePtr
& piece
);
112 DropAnimation(const SpritePtr
& piece
, const QPoint
&);
115 class PromotionAnimation
: public OneShotAnimation
{
117 SpritePtr m_promoted
;
118 virtual void shoot();
120 PromotionAnimation(const SpritePtr
& piece
, const SpritePtr
& promoted
);
123 class DelayAnimation
: public Animation
{
130 DelayAnimation(int msecs
);
131 virtual State
animationAdvance(int msec
);
133 virtual void abort();
136 class FadeAnimation
: public ConcreteAnimation
{
145 FadeAnimation(const SpritePtr
& sprite
, const QPoint
& pos
, int fadeFrom
, int fadeTo
);
146 FadeAnimation(const SpritePtr
& sprite
, int fadeFrom
, int fadeTo
);
147 virtual State
animationAdvance(int msec
);
149 virtual void abort();
152 class GrowAnimation
: public ConcreteAnimation
{
158 GrowAnimation(const SpritePtr
& sprite
);
159 virtual State
animationAdvance(int msec
);
161 virtual void abort();
164 class ExplodeAnimation
: public ConcreteAnimation
{
171 ExplodeAnimation(const SpritePtr
& sprite
, Random
& random
);
172 virtual State
animationAdvance(int msec
);
174 virtual void abort();
178 * @brief Two groups of animations, to be executed one after the other.
180 * A double list of animations: pre-animations and post-animations.
181 * When an AnimationGroup is advanced, pre-animations are handled first,
182 * and removed from the list as they finish animating.
183 * When all pre-animations are gone, post-animations are handled.
185 class AnimationGroup
: public Animation
{
187 typedef std::list
<AnimationPtr
> AnimationList
;
188 typedef AnimationList::iterator Iterator
;
195 bool m_chain_abortions
;
197 virtual void start();
200 AnimationGroup(bool persistent
= false);
202 virtual bool sticky() const { return m_persistent
; }
203 void setPersistent(bool value
) { m_persistent
= value
; }
204 void setChainAbortions(bool value
) { m_chain_abortions
= value
; }
206 virtual void addPreAnimation(AnimationPtr
);
207 virtual void addPostAnimation(AnimationPtr
);
209 virtual State
animationAdvance(int msec
);
211 virtual void abort() { stop(); }
214 class TeleportAnimation
: public AnimationGroup
{
216 TeleportAnimation(const SpritePtr
& sprite
, const QPoint
& from
, const QPoint
& to
);
217 TeleportAnimation(const SpritePtr
& sprite
, const QPoint
& to
);
220 class CrossFadingAnimation
: public AnimationGroup
{
223 virtual void start();
225 CrossFadingAnimation(const SpritePtr
& piece
, const SpritePtr
& promoted
);
228 class DelayedAnimationSet
: public Animation
{
233 Event(int index
, int time
)
234 : m_index(index
), m_time(time
) { }
235 int index() const { return m_index
; }
236 int time() const { return m_time
; }
238 bool operator<(const Event
& other
) const { return m_time
< other
.m_time
; }
245 std::vector
<boost::shared_ptr
<Animation
> > m_animations
;
246 AnimationGroup m_group
;
247 std::vector
<Event
> m_events
;
248 std::vector
<Event
>::iterator m_next_event
;
252 DelayedAnimationSet(Random
& random
);
253 void addAnimation(const boost::shared_ptr
<Animation
>& animation
);
255 virtual State
animationAdvance(int msec
);
257 virtual void abort();
260 #endif // ANIMATION_H