Changed my email in the copyright statements.
[tagua/yd.git] / src / animationfactory.cpp
blob619c45a87135daa5ff9a99401bb26f8f167a026c
1 #include "animationfactory.h"
3 #include "animation.h"
4 #include "namedsprite.h"
5 #include "pointconverter.h"
6 #include "indexconverter.h"
7 #include "graphicalapi.h"
8 #include "mastersettings.h"
10 namespace Common {
11 AnimationPtr appear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) {
12 if (!s.fading)
13 type = Animate::Instant;
15 switch (type) {
16 case Animate::Normal:
17 return AnimationPtr(new FadeAnimation(sprite.sprite(), 0, 255));
18 case Animate::Instant:
19 default:
20 return AnimationPtr(new DropAnimation(sprite.sprite()));
24 AnimationPtr disappear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) {
25 if (!s.fading)
26 type = Animate::Instant;
28 switch (type) {
29 case Animate::Normal:
30 return AnimationPtr(new FadeAnimation(sprite.sprite(), 255, 0));
31 case Animate::Instant:
32 default:
33 return AnimationPtr(new CaptureAnimation(sprite.sprite()));
38 AnimationSettings::AnimationSettings() {
39 reload();
42 void AnimationSettings::reload() {
43 Settings s = settings().group("animations");
45 enabled = s.flag("enabled", true);
46 maxSequence =
47 s.group("sequence").flag("enabled", true)
48 ? s.group("sequence")["max"].value<int>()
49 : 0;
50 movement = s["movement"].flag("enabled", true);
51 explode = s["explode"].flag("enabled", true);
52 fading = s["fading"].flag("enabled", true);
53 transform = s["transform"].flag("enabled", true);
56 AnimationFactory::AnimationFactory(GraphicalAPI* api)
57 : m_api(api) {
58 m_group = AnimationGroupPtr(new AnimationGroup);
61 AnimationGroupPtr AnimationFactory::group() const {
62 return m_group;
65 void AnimationFactory::setGroup(const AnimationGroupPtr& group) {
66 m_group = group;
69 void AnimationFactory::addPreAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) {
70 m_group->addPreAnimation(m_api->animate(scheme, type));
73 void AnimationFactory::addPostAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) {
74 m_group->addPostAnimation(m_api->animate(scheme, type));
77 AnimationFactory::operator AnimationGroupPtr() const {
78 return group();
81 namespace Animate {
83 Scheme::~Scheme() { }
85 move::move(const NamedSprite& sprite, const Point& to, int type)
86 : m_sprite(sprite)
87 , m_to(to)
88 , m_type(type) { }
90 AnimationPtr move::run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const {
91 int mov_type = m_type;
92 if (!s.movement)
93 type = Instant;
94 else if (!s.transform) {
95 mov_type &= ~Rotating;
98 switch (type) {
99 case Normal: {
100 MovementAnimation* mov;
101 QPoint destination = converter->toReal(m_to);
102 Point origin = converter->toLogical(m_sprite.sprite()->pos() +
103 Point(converter->squareSize(), converter->squareSize()) / 2);
104 if ((mov_type & LShaped) && origin != m_to) {
105 mov = new KnightMovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating);
107 else {
108 mov = new MovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating);
110 return AnimationPtr(mov);
112 case Instant:
113 default:
114 return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to)));
118 appear::appear(const NamedSprite& sprite)
119 : m_sprite(sprite) { }
121 AnimationPtr appear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
122 return Common::appear(s, m_sprite, type);
125 disappear::disappear(const NamedSprite& sprite)
126 : m_sprite(sprite) { }
128 AnimationPtr disappear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
129 return Common::disappear(s, m_sprite, type);
132 destroy::destroy(const NamedSprite& sprite)
133 : m_sprite(sprite) { }
135 AnimationPtr destroy::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
136 if (!s.explode) {
137 return Common::disappear(s, m_sprite, type);
140 switch (type) {
141 case Normal:
142 return AnimationPtr(new ExplodeAnimation(m_sprite.sprite(), Random::instance()));
143 case Instant:
144 default:
145 return AnimationPtr(new CaptureAnimation(m_sprite.sprite()));
149 morph::morph(const NamedSprite& sprite, const NamedSprite& new_sprite)
150 : m_sprite(sprite)
151 , m_new_sprite(new_sprite) { }
153 AnimationPtr morph::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
154 if (!s.fading)
155 type = Instant;
157 switch (type) {
158 case Normal:
159 return AnimationPtr(new CrossFadingAnimation(m_sprite.sprite(), m_new_sprite.sprite()));
160 case Instant:
161 default:
162 return AnimationPtr(new PromotionAnimation(m_sprite.sprite(), m_new_sprite.sprite()));
166 namespace Pool {
168 move::move(const NamedSprite& sprite, int to)
169 : m_sprite(sprite)
170 , m_to(to) { }
172 AnimationPtr move::run(const AnimationSettings& s, const IndexConverter* converter, AnimationType type) const {
173 if (!s.movement)
174 type = Instant;
176 switch (type) {
177 case Normal:
178 return AnimationPtr(new MovementAnimation(m_sprite.sprite(), converter->toReal(m_to)));
179 case Instant:
180 default:
181 return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to)));
185 appear::appear(const NamedSprite& sprite)
186 : m_sprite(sprite) { }
188 AnimationPtr appear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const {
189 return Common::appear(s, m_sprite, type);
192 disappear::disappear(const NamedSprite& sprite)
193 : m_sprite(sprite) { }
195 AnimationPtr disappear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const {
196 return Common::disappear(s, m_sprite, type);