4 * Created on: Aug 10, 2012
8 #include "PerformAttacks.h"
10 #include "../BattleState.h"
12 #include "../Monster.h"
13 #include "../../app/Application.h"
14 #include "../../app/Input.h"
15 #include "../../common/Ikari.h"
16 #include "../../common/Item.h"
17 #include "../../common/Spell.h"
18 #include "../../geometry/operators.h"
19 #include "../../geometry/Point.h"
20 #include "../../graphics/Animation.h"
21 #include "../../graphics/Font.h"
22 #include "../../graphics/Frame.h"
26 using app::Application;
28 using geometry::Point;
29 using geometry::Vector;
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
36 battle->WriteOrder(order);
37 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
38 numberPosition.reserve(numberAnimation.size());
41 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
45 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
49 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
54 void PerformAttacks::Resize(int width, int height) {
59 void PerformAttacks::HandleEvents(const Input &input) {
61 if (HasAnimationsRunning()) return;
65 battle->ClearAllAttacks();
70 if (order[cursor].isMonster) {
71 const Monster &monster(battle->MonsterAt(order[cursor].index));
72 titleBarText = monster.Name();
75 Hero &hero(battle->HeroAt(order[cursor].index));
76 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
78 switch (ac.GetType()) {
79 case AttackChoice::SWORD:
80 if (hero.HasWeapon()) {
81 titleBarText = hero.Weapon()->Name();
82 targetAnimation = hero.Weapon()->AttackAnimation();
84 titleBarText = "Melee attack!";
85 targetAnimation = hero.MeleeAnimation();
87 moveAnimation = hero.AttackAnimation();
89 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
90 if (ac.Selection().TargetsEnemies()) {
91 numberPosition.push_back(
92 battle->MonsterPositions()[ac.Selection().SingleSelection()]);
94 numberPosition.push_back(
95 battle->HeroesPositions()[ac.Selection().SingleSelection()]);
98 case AttackChoice::MAGIC:
99 titleBarText = ac.GetSpell()->Name();
100 moveAnimation = hero.SpellAnimation();
102 case AttackChoice::DEFEND:
103 titleBarText = "Defends.";
106 case AttackChoice::IKARI:
107 if (ac.GetItem()->HasIkari()) {
108 titleBarText = ac.GetItem()->GetIkari()->Name();
109 if (ac.GetItem()->GetIkari()->IsMagical()) {
110 moveAnimation = hero.SpellAnimation();
112 moveAnimation = hero.AttackAnimation();
116 case AttackChoice::ITEM:
117 titleBarText = ac.GetItem()->Name();
120 case AttackChoice::UNDECIDED:
121 titleBarText = "UNDECIDED";
127 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
128 if (moveAnimation) moveAnimation->Start(*this);
129 if (targetAnimation) {
130 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
132 targetAnimationTimer.Clear();
136 void PerformAttacks::CheckAnimations() {
137 if (targetAnimation && targetAnimationTimer.JustHit()) {
138 targetAnimation->Start(*this);
140 if (moveAnimation && !moveAnimation->Finished()) return;
141 if (targetAnimation && !targetAnimation->Finished()) return;
142 if (moveAnimation || targetAnimation) {
145 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
149 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
150 i->CheckTimers(*this);
155 bool PerformAttacks::HasAnimationsRunning() const {
156 if (titleBarTimer.Running()) return true;
157 if (moveAnimation && moveAnimation->Running()) return true;
158 if (targetAnimation && targetAnimation->Running()) return true;
159 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
160 if (i->Running()) return true;
165 void PerformAttacks::ResetAnimation() {
167 moveAnimation->Stop();
170 if (targetAnimation) {
171 targetAnimation->Stop();
174 titleBarTimer.Clear();
175 numberAnimation.clear();
176 numberPosition.clear();
179 void PerformAttacks::AdvanceCursor() {
181 while (cursor < int(order.size())) {
182 if (order[cursor].isMonster) {
183 if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
185 if (battle->HeroAt(order[cursor].index).Health() > 0) break;
192 void PerformAttacks::UpdateWorld(float deltaT) {
196 void PerformAttacks::Render(SDL_Surface *screen) {
197 Vector<int> offset(battle->CalculateScreenOffset(screen));
198 battle->RenderBackground(screen, offset);
199 battle->RenderMonsters(screen, offset);
200 battle->RenderHeroes(screen, offset);
201 battle->RenderSmallHeroTags(screen, offset);
202 RenderTitleBar(screen, offset);
203 RenderNumbers(screen, offset);
204 RenderTargetAnimation(screen, offset);
207 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
208 if (!titleBarText || !titleBarTimer.Running()) return;
210 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
211 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
213 Point<int> textPosition(
214 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
215 battle->Res().titleFrame->BorderHeight());
216 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
219 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
220 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
221 if (numberAnimation[i].Running()) {
222 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
223 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
228 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
229 if (!targetAnimation || !targetAnimation->Running()) return;
230 if (order[cursor].isMonster) return; // no monsters for now
231 const TargetSelection &ts(battle->AttackChoiceAt(order[cursor].index).Selection());
232 const vector<Point<int> > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions());
233 for (vector<Point<int> >::size_type i(0), end(positions.size()); i < end; ++i) {
234 if (ts.IsSelected(i)) {
235 targetAnimation->DrawCenter(screen, positions[i] + offset);