Sort layouts in the list
[kdegames.git] / kollision / animation.cpp
blobf8c4c5f67b88a5c44cd60eb0ec80ab1ddad53f9f
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 */
10 #include "animation.h"
12 #include "sprite.h"
14 #include <math.h>
15 #include <kdebug.h>
17 Animation::~Animation()
19 emit over();
22 PauseAnimation::PauseAnimation(int time)
23 : m_time(time)
27 void PauseAnimation::start(int t)
29 m_start = t;
32 bool PauseAnimation::step(int t)
34 return t - m_start >= m_time;
37 FadeAnimation::FadeAnimation(const SpritePtr& sprite, double from, double to, int time)
38 : m_sprite(sprite)
39 , m_from(from)
40 , m_to(to)
41 , m_time(time)
42 , m_stopped(false)
46 void FadeAnimation::start(int t)
48 m_start = t;
49 m_sprite->setOpacityF(m_from);
52 bool FadeAnimation::step(int t)
54 if (m_stopped) {
55 return true;
57 else {
58 double val = m_from + (m_to - m_from) * (t - m_start) / m_time;
59 m_sprite->setOpacityF(val);
60 return t - m_start >= m_time;
64 void FadeAnimation::stop()
66 m_sprite->setOpacityF(m_to);
67 m_stopped = true;
71 MovementAnimation::MovementAnimation(const SpritePtr& sprite, const QPointF& from,
72 const QPointF& velocity, int time)
73 : m_sprite(sprite)
74 , m_from(from)
75 , m_velocity(velocity)
76 , m_time(time)
80 void MovementAnimation::start(int t)
82 m_last = t;
83 m_sprite->setPosition(m_from);
86 bool MovementAnimation::step(int t)
88 int delta = t - m_last;
89 m_last = t;
90 m_sprite->setPosition(m_sprite->position() + delta * m_velocity);
91 m_time -= delta;
92 return m_time <= 0;
95 void MovementAnimation::stop()
97 m_sprite->setPosition(m_from + m_velocity * m_time);
101 AnimationGroup::AnimationGroup()
102 : m_last(-1)
106 void AnimationGroup::add(Animation* a)
108 m_animations.append(a);
109 if (m_last != -1) {
110 a->start(m_last);
114 void AnimationGroup::start(int t)
116 m_last = t;
117 foreach (Animation* a, m_animations) {
118 a->start(t);
122 bool AnimationGroup::step(int t)
124 m_last = t;
125 for (List::iterator it = m_animations.begin();
126 it != m_animations.end(); ) {
127 if ((*it)->step(t)) {
128 delete *it;
129 it = m_animations.erase(it);
131 else {
132 ++it;
136 return m_animations.isEmpty();
139 void AnimationGroup::stop()
141 foreach (Animation* a, m_animations) {
142 delete a;
144 m_animations.clear();
148 AnimationSequence::AnimationSequence()
149 : m_last(-1)
154 void AnimationSequence::add(Animation* a)
156 m_animations.enqueue(a);
157 if (m_last != -1) {
158 a->start(m_last);
162 void AnimationSequence::start(int t)
164 m_last = t;
165 if (!m_animations.isEmpty()) {
166 m_animations.head()->start(t);
170 bool AnimationSequence::step(int t)
172 m_last = t;
173 while (!m_animations.isEmpty()) {
174 if (m_animations.head()->step(t)) {
175 delete m_animations.dequeue();
176 if (!m_animations.isEmpty()) {
177 m_animations.head()->start(t);
180 else {
181 return false;
184 return true;
187 void AnimationSequence::stop()
189 while (!m_animations.isEmpty()) {
190 m_animations.head()->stop();
191 delete m_animations.dequeue();
195 #include "animation.moc"