4 * Created on: Aug 10, 2012
8 #include "PerformAttacks.h"
10 #include "../AttackAnimation.h"
11 #include "../BattleState.h"
13 #include "../Monster.h"
14 #include "../../app/Application.h"
15 #include "../../app/Input.h"
16 #include "../../common/Ikari.h"
17 #include "../../common/Item.h"
18 #include "../../common/Spell.h"
19 #include "../../geometry/operators.h"
20 #include "../../geometry/Point.h"
21 #include "../../graphics/Animation.h"
22 #include "../../graphics/Font.h"
23 #include "../../graphics/Frame.h"
27 using app::Application;
29 using geometry::Point;
30 using geometry::Vector;
35 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
37 battle->WriteOrder(order);
38 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
39 numberPosition.reserve(numberAnimation.size());
42 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
46 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
50 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
55 void PerformAttacks::Resize(int width, int height) {
60 void PerformAttacks::HandleEvents(const Input &input) {
61 if (attackAnimation) attackAnimation->Update();
62 CheckNumberAnimation();
63 if (HasAnimationsRunning()) return;
67 battle->ClearAllAttacks();
72 if (order[cursor].isMonster) {
73 const Monster &monster(battle->MonsterAt(order[cursor].index));
74 titleBarText = monster.Name();
77 Hero &hero(battle->HeroAt(order[cursor].index));
78 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
80 switch (ac.GetType()) {
81 case AttackChoice::SWORD:
82 if (hero.HasWeapon()) {
83 titleBarText = hero.Weapon()->Name();
84 moveAnimation = hero.AttackAnimation();
86 titleBarText = "Melee attack!";
87 if (hero.MeleeAnimation()) {
89 attackAnimation = hero.MeleeAnimation();
91 moveAnimation = hero.AttackAnimation();
95 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
96 if (ac.Selection().TargetsEnemies()) {
97 numberPosition.push_back(
98 battle->MonsterPositions()[ac.Selection().SingleSelection()]);
100 numberPosition.push_back(
101 battle->HeroesPositions()[ac.Selection().SingleSelection()]);
104 case AttackChoice::MAGIC:
105 titleBarText = ac.GetSpell()->Name();
106 moveAnimation = hero.SpellAnimation();
108 case AttackChoice::DEFEND:
109 titleBarText = "Defends.";
112 case AttackChoice::IKARI:
113 if (ac.GetItem()->HasIkari()) {
114 titleBarText = ac.GetItem()->GetIkari()->Name();
115 if (ac.GetItem()->GetIkari()->IsMagical()) {
116 moveAnimation = hero.SpellAnimation();
118 moveAnimation = hero.AttackAnimation();
122 case AttackChoice::ITEM:
123 titleBarText = ac.GetItem()->Name();
126 case AttackChoice::UNDECIDED:
127 titleBarText = "UNDECIDED";
133 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
134 if (moveAnimation) moveAnimation->Start(*this);
135 if (attackAnimation) attackAnimation->Start(battle, this);
138 void PerformAttacks::CheckNumberAnimation() {
139 if (moveAnimation && moveAnimation->Running()) return;
140 if (attackAnimation && !attackAnimation->Finished()) return;
141 if (moveAnimation || attackAnimation) {
144 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
148 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
149 i->CheckTimers(*this);
154 bool PerformAttacks::HasAnimationsRunning() const {
155 if (titleBarTimer.Running()) return true;
156 if (moveAnimation && moveAnimation->Running()) return true;
157 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
158 if (i->Running()) return true;
163 void PerformAttacks::ResetAnimation() {
164 if (moveAnimation) moveAnimation->Stop();
165 if (attackAnimation) attackAnimation = 0;
166 titleBarTimer.Clear();
167 numberAnimation.clear();
168 numberPosition.clear();
171 void PerformAttacks::AdvanceCursor() {
173 while (cursor < int(order.size())) {
174 if (order[cursor].isMonster) {
175 if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
177 if (battle->HeroAt(order[cursor].index).Health() > 0) break;
184 void PerformAttacks::UpdateWorld(float deltaT) {
188 void PerformAttacks::Render(SDL_Surface *screen) {
189 Vector<int> offset(battle->CalculateScreenOffset(screen));
190 battle->RenderBackground(screen, offset);
191 battle->RenderMonsters(screen, offset);
192 battle->RenderHeroes(screen, offset);
193 battle->RenderSmallHeroTags(screen, offset);
194 RenderTitleBar(screen, offset);
195 RenderNumbers(screen, offset);
196 if (attackAnimation) attackAnimation->Render(screen, offset);
199 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
200 if (!titleBarText || !titleBarTimer.Running()) return;
202 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
203 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
205 Point<int> textPosition(
206 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
207 battle->Res().titleFrame->BorderHeight());
208 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
211 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
212 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
213 if (numberAnimation[i].Running()) {
214 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
215 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);