]> git.localhorst.tv Git - l2e.git/blob - src/battle/AttackAnimation.cpp
revised attack animation codes
[l2e.git] / src / battle / AttackAnimation.cpp
1 /*
2  * AttackAnimation.cpp
3  *
4  *  Created on: Aug 13, 2012
5  *      Author: holy
6  */
7
8 #include "AttackAnimation.h"
9
10 #include "BattleState.h"
11 #include "../app/State.h"
12 #include "../graphics/Animation.h"
13
14 using geometry::Point;
15 using graphics::Animation;
16 using std::vector;
17
18 namespace battle {
19
20 AttackAnimation::~AttackAnimation() {
21         for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
22                 i->animation->Stop();
23         }
24         for (vector<Animation *>::const_iterator i(foreignAnimations.begin()), end(foreignAnimations.end()); i != end; ++i) {
25                 (*i)->Stop();
26         }
27 }
28
29
30 void AttackAnimation::StartTimer(int ms) {
31         text.push_back(START_TIMER);
32         text.push_back(ms);
33 }
34
35 void AttackAnimation::PlayAttackAnimation() {
36         text.push_back(ATTACK_ANIMATION);
37 }
38
39 void AttackAnimation::PlaySpellAnimation() {
40         text.push_back(SPELL_ANIMATION);
41 }
42
43 void AttackAnimation::PlayTargetAnimation(graphics::Animation *a) {
44         text.push_back(TARGET_ANIMATION);
45         text.push_back(a);
46 }
47
48 void AttackAnimation::PlayFullscreenAnimation(graphics::Animation *a) {
49         text.push_back(FULLSCREEN_ANIMATION);
50         text.push_back(a);
51 }
52
53 void AttackAnimation::WaitForTimer() {
54         text.push_back(WAIT_TIMER);
55 }
56
57 void AttackAnimation::WaitForAnimations() {
58         text.push_back(WAIT_ANIMATIONS);
59 }
60
61
62 void AttackAnimation::Start(BattleState *b, app::State *s) {
63         battle = b;
64         state = s;
65         cursor = 0;
66         Update();
67 }
68
69 void AttackAnimation::Update() {
70         while (!Finished()) {
71                 if (ExecuteCommand()) break;
72         }
73 }
74
75 bool AttackAnimation::ExecuteCommand() {
76         switch (text[cursor].command) {
77                 case NOOP:
78                         break;
79                 case START_TIMER:
80                         return ExecuteStartTimer();
81                 case ATTACK_ANIMATION:
82                         return ExecuteAttackAnimation();
83                 case SPELL_ANIMATION:
84                         return ExecuteSpellAnimation();
85                 case TARGET_ANIMATION:
86                         return ExecuteTargetAnimation();
87                 case FULLSCREEN_ANIMATION:
88                         return ExecuteFullscreenAnimation();
89                 case WAIT_TIMER:
90                         return ExecuteWaitTimer();
91                 case WAIT_ANIMATIONS:
92                         return ExecuteWaitAnimations();
93         }
94         ++cursor; // skip unknown command (which should never happen anyway)
95         return false;
96 }
97
98
99 bool AttackAnimation::ExecuteStartTimer() {
100         timer = state->GraphicsTimers().StartCountdown(text[cursor + 1].number);
101         cursor += 2;
102         return false;
103 }
104
105 bool AttackAnimation::ExecuteAttackAnimation() {
106         // TODO: get real hero/monster
107         foreignAnimations.push_back(battle->HeroAt(0).AttackAnimation());
108         battle->HeroAt(0).AttackAnimation()->Start(*state);
109         ++cursor;
110         return false;
111 }
112
113 bool AttackAnimation::ExecuteSpellAnimation() {
114         // TODO: get real hero/monster
115         foreignAnimations.push_back(battle->HeroAt(0).SpellAnimation());
116         battle->HeroAt(0).SpellAnimation()->Start(*state);
117         ++cursor;
118         return false;
119 }
120
121 bool AttackAnimation::ExecuteTargetAnimation() {
122          // TODO: get real positions
123         AnimationMemo am;
124         am.animation = (Animation *) text[cursor + 1].ptr;
125         am.animation->Start(*state);
126         am.position = Point<int>(100, 100);
127         animations.push_back(am);
128         cursor += 2;
129         return false;
130 }
131
132 bool AttackAnimation::ExecuteFullscreenAnimation() {
133         AnimationMemo am;
134         am.animation = (Animation *) text[cursor + 1].ptr;
135         am.animation->Start(*state);
136         am.position = Point<int>(0, 0);
137         animations.push_back(am);
138         cursor += 2;
139         return false;
140 }
141
142 bool AttackAnimation::ExecuteWaitTimer() {
143         if (timer.Running()) {
144                 return true;
145         } else {
146                 ++cursor;
147                 return false;
148         }
149 }
150
151 bool AttackAnimation::ExecuteWaitAnimations() {
152         for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
153                 if (i->animation->Running()) return true;
154         }
155         for (vector<Animation *>::const_iterator i(foreignAnimations.begin()), end(foreignAnimations.end()); i != end; ++i) {
156                 if ((*i)->Running()) return true;
157         }
158         ++cursor;
159         return false;
160 }
161
162
163 bool AttackAnimation::Finished() const {
164         return cursor >= int(text.size());
165 }
166
167 void AttackAnimation::Render(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
168         for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
169                 if (i->animation->Running()) {
170                         i->animation->Draw(screen, i->position + offset);
171                 }
172         }
173 }
174
175 }