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;
33 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
35 battle->WriteOrder(order);
38 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
42 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
46 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
51 void PerformAttacks::Resize(int width, int height) {
56 void PerformAttacks::HandleEvents(const Input &input) {
57 if (titleBarTimer.Running() || (moveAnimation && moveAnimation->Running())) return;
59 if (moveAnimation) moveAnimation->Stop();
60 titleBarTimer.Clear();
64 while (cursor < int(order.size())) {
65 if (order[cursor].isMonster) {
66 if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
68 if (battle->HeroAt(order[cursor].index).Health() > 0) break;
73 if (cursor >= int(order.size())) {
74 battle->ClearAllAttacks();
79 if (order[cursor].isMonster) {
80 const Monster &monster(battle->MonsterAt(order[cursor].index));
81 titleBarText = monster.Name();
84 Hero &hero(battle->HeroAt(order[cursor].index));
85 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
87 switch (ac.GetType()) {
88 case AttackChoice::SWORD:
89 titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
90 moveAnimation = hero.AttackAnimation();
92 case AttackChoice::MAGIC:
93 titleBarText = ac.GetSpell()->Name();
94 moveAnimation = hero.SpellAnimation();
96 case AttackChoice::DEFEND:
97 titleBarText = "Defends.";
100 case AttackChoice::IKARI:
101 if (ac.GetItem()->HasIkari()) {
102 titleBarText = ac.GetItem()->GetIkari()->Name();
103 if (ac.GetItem()->GetIkari()->IsMagical()) {
104 moveAnimation = hero.SpellAnimation();
106 moveAnimation = hero.AttackAnimation();
110 case AttackChoice::ITEM:
111 titleBarText = ac.GetItem()->Name();
114 case AttackChoice::UNDECIDED:
115 titleBarText = "UNDECIDED";
121 titleBarTimer = GraphicsTimers().StartCountdown(1500);
122 if (moveAnimation) moveAnimation->Start(*this);
126 void PerformAttacks::UpdateWorld(float deltaT) {
130 void PerformAttacks::Render(SDL_Surface *screen) {
131 Vector<int> offset(battle->CalculateScreenOffset(screen));
132 battle->RenderBackground(screen, offset);
133 battle->RenderMonsters(screen, offset);
134 battle->RenderHeroes(screen, offset);
135 battle->RenderSmallHeroTags(screen, offset);
136 RenderTitleBar(screen, offset);
139 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
140 if (!titleBarText || !titleBarTimer.Running()) return;
142 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
143 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->BackgroundWidth(), height);
145 Point<int> textPosition(
146 (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
147 battle->Res().titleFrame->BorderHeight());
148 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);