Fix debian/rules to build in BUILD/ dir.
[tagua/yd.git] / src / animationfactory.cpp
blob746115dd11d85c87d1b6c4cb0a6fff9172ae3da9
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 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 #include "animationfactory.h"
13 #include "animation.h"
14 #include "namedsprite.h"
15 #include "pointconverter.h"
16 #include "indexconverter.h"
17 #include "graphicalapi.h"
18 #include "mastersettings.h"
20 namespace Common {
21 AnimationPtr appear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) {
22 if (!s.fading)
23 type = Animate::Instant;
25 switch (type) {
26 case Animate::Normal:
27 return AnimationPtr(new FadeAnimation(sprite.sprite(), 0, 255));
28 case Animate::Instant:
29 default:
30 return AnimationPtr(new DropAnimation(sprite.sprite()));
34 AnimationPtr disappear(const AnimationSettings& s, const NamedSprite& sprite, Animate::AnimationType type) {
35 if (!s.fading)
36 type = Animate::Instant;
38 switch (type) {
39 case Animate::Normal:
40 return AnimationPtr(new FadeAnimation(sprite.sprite(), 255, 0));
41 case Animate::Instant:
42 default:
43 return AnimationPtr(new CaptureAnimation(sprite.sprite()));
48 AnimationSettings::AnimationSettings() {
49 reload();
52 void AnimationSettings::reload() {
53 Settings s = settings().group("animations");
55 enabled = s.flag("enabled", true);
56 maxSequence =
57 s.group("sequence").flag("enabled", true)
58 ? s.group("sequence")["max"].value<int>()
59 : 0;
60 movement = s["movement"].flag("enabled", true);
61 explode = s["explode"].flag("enabled", true);
62 fading = s["fading"].flag("enabled", true);
63 transform = s["transform"].flag("enabled", true);
66 AnimationFactory::AnimationFactory(GraphicalAPI* api)
67 : m_api(api) {
68 m_group = AnimationGroupPtr(new AnimationGroup);
71 AnimationGroupPtr AnimationFactory::group() const {
72 return m_group;
75 void AnimationFactory::setGroup(const AnimationGroupPtr& group) {
76 m_group = group;
79 void AnimationFactory::addPreAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) {
80 m_group->addPreAnimation(m_api->animate(scheme, type));
83 void AnimationFactory::addPostAnimation(const Animate::Scheme& scheme, Animate::AnimationType type) {
84 m_group->addPostAnimation(m_api->animate(scheme, type));
87 AnimationFactory::operator AnimationGroupPtr() const {
88 return group();
91 namespace Animate {
93 Scheme::~Scheme() { }
95 move::move(const NamedSprite& sprite, const Point& to, int type)
96 : m_sprite(sprite)
97 , m_to(to)
98 , m_type(type) { }
100 AnimationPtr move::run(const AnimationSettings& s, const PointConverter* converter, AnimationType type) const {
101 int mov_type = m_type;
102 if (!s.movement)
103 type = Instant;
104 else if (!s.transform) {
105 mov_type &= ~Rotating;
108 switch (type) {
109 case Normal: {
110 MovementAnimation* mov;
111 QPoint destination = converter->toReal(m_to);
112 Point origin = converter->toLogical(m_sprite.sprite()->pos() +
113 Point(converter->squareSize(), converter->squareSize()) / 2);
114 if ((mov_type & LShaped) && origin != m_to) {
115 mov = new KnightMovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating);
117 else {
118 mov = new MovementAnimation(m_sprite.sprite(), destination, mov_type & Rotating);
120 return AnimationPtr(mov);
122 case Instant:
123 default:
124 return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to)));
128 appear::appear(const NamedSprite& sprite)
129 : m_sprite(sprite) { }
131 AnimationPtr appear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
132 return Common::appear(s, m_sprite, type);
135 disappear::disappear(const NamedSprite& sprite)
136 : m_sprite(sprite) { }
138 AnimationPtr disappear::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
139 return Common::disappear(s, m_sprite, type);
142 destroy::destroy(const NamedSprite& sprite)
143 : m_sprite(sprite) { }
145 AnimationPtr destroy::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
146 if (!s.explode) {
147 return Common::disappear(s, m_sprite, type);
150 switch (type) {
151 case Normal:
152 return AnimationPtr(new ExplodeAnimation(m_sprite.sprite(), Random::instance()));
153 case Instant:
154 default:
155 return AnimationPtr(new CaptureAnimation(m_sprite.sprite()));
159 morph::morph(const NamedSprite& sprite, const NamedSprite& new_sprite)
160 : m_sprite(sprite)
161 , m_new_sprite(new_sprite) { }
163 AnimationPtr morph::run(const AnimationSettings& s, const PointConverter*, AnimationType type) const {
164 if (!s.fading)
165 type = Instant;
167 switch (type) {
168 case Normal:
169 return AnimationPtr(new CrossFadingAnimation(m_sprite.sprite(), m_new_sprite.sprite()));
170 case Instant:
171 default:
172 return AnimationPtr(new PromotionAnimation(m_sprite.sprite(), m_new_sprite.sprite()));
176 namespace Pool {
178 move::move(const NamedSprite& sprite, int to)
179 : m_sprite(sprite)
180 , m_to(to) { }
182 AnimationPtr move::run(const AnimationSettings& s, const IndexConverter* converter, AnimationType type) const {
183 if (!s.movement)
184 type = Instant;
186 switch (type) {
187 case Normal:
188 return AnimationPtr(new MovementAnimation(m_sprite.sprite(), converter->toReal(m_to)));
189 case Instant:
190 default:
191 return AnimationPtr(new InstantAnimation(m_sprite.sprite(), converter->toReal(m_to)));
195 appear::appear(const NamedSprite& sprite)
196 : m_sprite(sprite) { }
198 AnimationPtr appear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const {
199 return Common::appear(s, m_sprite, type);
202 disappear::disappear(const NamedSprite& sprite)
203 : m_sprite(sprite) { }
205 AnimationPtr disappear::run(const AnimationSettings& s, const IndexConverter*, AnimationType type) const {
206 return Common::disappear(s, m_sprite, type);