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) {
60 CheckNumberAnimation();
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 titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
81 moveAnimation = hero.AttackAnimation();
82 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
83 if (ac.Selection().TargetsEnemies()) {
84 numberPosition.push_back(
85 battle->MonsterPositions()[ac.Selection().SingleSelection()]);
87 numberPosition.push_back(
88 battle->HeroesPositions()[ac.Selection().SingleSelection()]);
91 case AttackChoice::MAGIC:
92 titleBarText = ac.GetSpell()->Name();
93 moveAnimation = hero.SpellAnimation();
95 case AttackChoice::DEFEND:
96 titleBarText = "Defends.";
99 case AttackChoice::IKARI:
100 if (ac.GetItem()->HasIkari()) {
101 titleBarText = ac.GetItem()->GetIkari()->Name();
102 if (ac.GetItem()->GetIkari()->IsMagical()) {
103 moveAnimation = hero.SpellAnimation();
105 moveAnimation = hero.AttackAnimation();
109 case AttackChoice::ITEM:
110 titleBarText = ac.GetItem()->Name();
113 case AttackChoice::UNDECIDED:
114 titleBarText = "UNDECIDED";
120 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
121 if (moveAnimation) moveAnimation->Start(*this);
124 void PerformAttacks::CheckNumberAnimation() {
125 if (moveAnimation && moveAnimation->Running()) return;
126 if (!moveAnimation || moveAnimation->JustFinished()) {
127 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
131 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
132 i->CheckTimers(*this);
137 bool PerformAttacks::HasAnimationsRunning() const {
138 if (titleBarTimer.Running()) return true;
139 if (moveAnimation && moveAnimation->Running()) return true;
140 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
141 if (i->Running()) return true;
146 void PerformAttacks::ResetAnimation() {
147 if (moveAnimation) moveAnimation->Stop();
148 titleBarTimer.Clear();
149 numberAnimation.clear();
150 numberPosition.clear();
153 void PerformAttacks::AdvanceCursor() {
155 while (cursor < int(order.size())) {
156 if (order[cursor].isMonster) {
157 if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
159 if (battle->HeroAt(order[cursor].index).Health() > 0) break;
166 void PerformAttacks::UpdateWorld(float deltaT) {
170 void PerformAttacks::Render(SDL_Surface *screen) {
171 Vector<int> offset(battle->CalculateScreenOffset(screen));
172 battle->RenderBackground(screen, offset);
173 battle->RenderMonsters(screen, offset);
174 battle->RenderHeroes(screen, offset);
175 battle->RenderSmallHeroTags(screen, offset);
176 RenderTitleBar(screen, offset);
177 RenderNumbers(screen, offset);
180 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
181 if (!titleBarText || !titleBarTimer.Running()) return;
183 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
184 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
186 Point<int> textPosition(
187 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
188 battle->Res().titleFrame->BorderHeight());
189 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
192 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
193 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
194 if (numberAnimation[i].Running()) {
195 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
196 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);