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;
30 using graphics::Animation;
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
36 // TODO: push battle animation if enemy is faster
39 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
43 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
44 fakeMoveTimer = GraphicsTimers().StartCountdown(850);
47 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
52 void PerformAttacks::Resize(int width, int height) {
57 void PerformAttacks::HandleEvents(const Input &input) {
58 if (fakeMoveTimer.JustHit()) {
63 while (cursor < battle->MaxMonsters() && !battle->MonsterPositionOccupied(cursor)) {
66 if (cursor >= battle->MaxMonsters()) {
67 battle->ClearAllAttacks();
72 battle->HeroAnimationAt(battle->NumHeroes() - 1).Stop();
74 titleBarText = battle->MonsterAt(cursor).Name();
77 const AttackChoice &ac(battle->AttackChoiceAt(cursor));
81 switch (ac.GetType()) {
82 case AttackChoice::SWORD:
83 battle->HeroAnimationAt(cursor) = Animation(
84 battle->HeroAt(cursor).Sprite(),
85 battle->HeroAt(cursor).AttackFrameTime(),
86 battle->HeroAt(cursor).AttackFrames(), 2);
88 case AttackChoice::MAGIC:
89 battle->HeroAnimationAt(cursor) = Animation(
90 battle->HeroAt(cursor).Sprite(),
91 battle->HeroAt(cursor).SpellFrameTime(),
92 battle->HeroAt(cursor).SpellFrames(), 3);
94 case AttackChoice::DEFEND:
96 case AttackChoice::IKARI:
97 if (ac.GetItem()->HasIkari()) {
98 if (ac.GetItem()->GetIkari()->IsMagical()) {
99 battle->HeroAnimationAt(cursor) = Animation(
100 battle->HeroAt(cursor).Sprite(),
101 battle->HeroAt(cursor).SpellFrameTime(),
102 battle->HeroAt(cursor).SpellFrames(), 3);
104 battle->HeroAnimationAt(cursor) = Animation(
105 battle->HeroAt(cursor).Sprite(),
106 battle->HeroAt(cursor).AttackFrameTime(),
107 battle->HeroAt(cursor).AttackFrames(), 2);
111 case AttackChoice::ITEM:
113 case AttackChoice::UNDECIDED:
116 battle->HeroAnimationAt(cursor).Start(*this);
119 if (cursor == battle->NumHeroes()) {
125 battle->HeroAnimationAt(cursor - 1).Stop();
127 switch (ac.GetType()) {
128 case AttackChoice::SWORD:
129 titleBarText = battle->HeroAt(cursor).HasWeapon() ? battle->HeroAt(cursor).Weapon()->Name() : "Gauntlet";
131 case AttackChoice::MAGIC:
132 titleBarText = ac.GetSpell()->Name();
134 case AttackChoice::DEFEND:
135 titleBarText = "Defends.";
137 case AttackChoice::IKARI:
138 titleBarText = ac.GetItem()->HasIkari() ? ac.GetItem()->GetIkari()->Name() : "No Ikari?";
140 case AttackChoice::ITEM:
141 titleBarText = ac.GetItem()->Name();
143 case AttackChoice::UNDECIDED:
144 titleBarText = "WTF???";
153 void PerformAttacks::UpdateWorld(float deltaT) {
157 void PerformAttacks::Render(SDL_Surface *screen) {
158 Vector<int> offset(battle->CalculateScreenOffset(screen));
159 battle->RenderBackground(screen, offset);
160 battle->RenderMonsters(screen, offset);
161 battle->RenderHeroes(screen, offset);
162 battle->RenderSmallHeroTags(screen, offset);
163 RenderTitleBar(screen, offset);
166 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
167 if (!titleBarText) return;
169 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
170 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->BackgroundWidth(), height);
172 Point<int> textPosition(
173 (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
174 battle->Res().titleFrame->BorderHeight());
175 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);