]> git.localhorst.tv Git - l2e.git/blob - src/battle/AttackAnimation.cpp
added AttackAnimation
[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::Wait(int ms) {
31         text.push_back(WAIT);
32         text.push_back(ms);
33 }
34
35 void AttackAnimation::WaitForAnimation() {
36         text.push_back(WAIT_ANIMATION);
37 }
38
39 void AttackAnimation::PlayAttackAnimation() {
40         text.push_back(ATTACK_ANIMATION);
41 }
42
43 void AttackAnimation::PlaySpellAnimation() {
44         text.push_back(SPELL_ANIMATION);
45 }
46
47 void AttackAnimation::PlayTargetAnimation(graphics::Animation *a) {
48         text.push_back(TARGET_ANIMATION);
49         text.push_back(a);
50 }
51
52 void AttackAnimation::PlayFullscreenAnimation(graphics::Animation *a) {
53         text.push_back(FULLSCREEN_ANIMATION);
54         text.push_back(a);
55 }
56
57
58 void AttackAnimation::Start(BattleState *b, app::State *s) {
59         battle = b;
60         state = s;
61         cursor = 0;
62         Update();
63 }
64
65 void AttackAnimation::Update() {
66         while (!Finished()) {
67                 if (ExecuteCommand()) break;
68         }
69 }
70
71 bool AttackAnimation::ExecuteCommand() {
72         switch (text[cursor].command) {
73                 case WAIT:
74                         return ExecuteWait();
75                 case WAIT_ANIMATION:
76                         return ExecuteWaitAnimation();
77                 case ATTACK_ANIMATION:
78                         return ExecuteAttackAnimation();
79                 case SPELL_ANIMATION:
80                         return ExecuteSpellAnimation();
81                 case TARGET_ANIMATION:
82                         return ExecuteTargetAnimation();
83                 case FULLSCREEN_ANIMATION:
84                         return ExecuteFullscreenAnimation();
85         }
86         ++cursor; // skip unknown command (which should never happen anyway)
87         return false;
88 }
89
90 bool AttackAnimation::ExecuteWait() {
91         if (timer.Started()) {
92                 if (timer.Running()) {
93                         return true;
94                 } else {
95                         cursor += 2;
96                         return false;
97                 }
98         } else {
99                 timer = state->GraphicsTimers().StartCountdown(text[cursor + 1].number);
100                 return true;
101         }
102 }
103
104
105 bool AttackAnimation::ExecuteWaitAnimation() {
106         for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
107                 if (i->animation->Running()) return true;
108         }
109         for (vector<Animation *>::const_iterator i(foreignAnimations.begin()), end(foreignAnimations.end()); i != end; ++i) {
110                 if ((*i)->Running()) return true;
111         }
112         ++cursor;
113         return false;
114 }
115
116 bool AttackAnimation::ExecuteAttackAnimation() {
117         foreignAnimations.push_back(battle->HeroAt(0).AttackAnimation());
118         battle->HeroAt(0).AttackAnimation()->Start(*state);
119         ++cursor;
120         return false;
121 }
122
123 bool AttackAnimation::ExecuteSpellAnimation() {
124         foreignAnimations.push_back(battle->HeroAt(0).SpellAnimation());
125         battle->HeroAt(0).SpellAnimation()->Start(*state);
126         ++cursor;
127         return false;
128 }
129
130 bool AttackAnimation::ExecuteTargetAnimation() {
131         AnimationMemo am;
132         am.animation = (Animation *) text[cursor + 1].ptr;
133         am.animation->Start(*state);
134         am.position = Point<int>(100, 100);
135         animations.push_back(am);
136         cursor += 2;
137         return true;
138 }
139
140 bool AttackAnimation::ExecuteFullscreenAnimation() {
141         AnimationMemo am;
142         am.animation = (Animation *) text[cursor + 1].ptr;
143         am.animation->Start(*state);
144         am.position = Point<int>(100, 100);
145         animations.push_back(am);
146         cursor += 2;
147         return true;
148 }
149
150
151 bool AttackAnimation::Finished() const {
152         return cursor >= int(text.size());
153 }
154
155 void AttackAnimation::Render(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
156         for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
157                 if (i->animation->Running()) {
158                         i->animation->Draw(screen, i->position + offset);
159                 }
160         }
161 }
162
163 }