1 #include "BattleState.h"
3 #include "PartyLayout.h"
4 #include "states/SelectMoveAction.h"
5 #include "states/PerformAttacks.h"
6 #include "states/VictoryState.h"
7 #include "../app/Application.h"
8 #include "../app/Input.h"
9 #include "../common/GameState.h"
10 #include "../common/Ikari.h"
11 #include "../common/Inventory.h"
12 #include "../common/Item.h"
13 #include "../common/Spell.h"
14 #include "../graphics/Frame.h"
15 #include "../graphics/Sprite.h"
16 #include "../math/Vector.h"
23 using app::Application;
25 using common::Inventory;
37 void BattleState::AddMonster(const Monster &m) {
41 void BattleState::AddHero(const Hero &h) {
45 void BattleState::SetCapsule(const Capsule &c) {
50 void BattleState::OnResize(int w, int h) {
52 (w - background->w) / 2,
53 (h - background->h) / 2);
57 void BattleState::OnEnterState(SDL_Surface *screen) {
58 for (int i(0); i < 4; ++i) {
59 Hero &hero = HeroAt(i);
60 hero.Position() = battle.HeroesLayout().CalculatePosition(i, background->w, background->h);
61 hero.SpellMenu() = *res->spellMenuProperties;
62 hero.UpdateSpellMenu();
63 hero.IkariMenu() = *res->ikariMenuProperties;
64 hero.UpdateIkariMenu(res);
65 heroTags[i] = HeroTag(this, i);
66 smallHeroTags[i] = SmallHeroTag(this, i);
69 battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h);
71 for (int i(0); i < battle.NumMonsters(); ++i) {
72 MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h);
75 int tagHeight(attackTypeMenu.Height());
76 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
77 int xOffset((Width() - 2 * tagWidth) / 2);
78 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
79 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
80 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
81 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
83 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
84 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
85 xOffset = (Width() - 4 * tagWidth) / 2;
86 int yOffset(Height() - tagHeight);
87 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
88 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
89 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
90 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
92 OnResize(screen->w, screen->h);
94 itemMenu = *res->itemMenuProperties;
96 battle.ClearAllAttacks();
99 void BattleState::LoadInventory() {
100 const Inventory &inv(game->state->inventory);
102 itemMenu.Reserve(inv.MaxItems());
103 for (int i(0); i < inv.MaxItems(); ++i) {
104 const Item *item(inv.ItemAt(i));
106 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
108 itemMenu.AddEmptyEntry();
113 void BattleState::OnExitState(SDL_Surface *screen) {
117 void BattleState::OnResumeState(SDL_Surface *screen) {
119 Ctrl().PopState(); // quit the battle scene
122 if (battle.Victory()) {
126 Ctrl().PushState(new VictoryState(&battle, this));
127 alreadyPushed = true;
131 if (battle.Defeat()) {
135 // TODO: this should not push a state while quitting
136 if (battle.AttackSelectionDone()) {
137 Ctrl().PushState(new PerformAttacks(&battle, this));
139 Ctrl().PushState(new SelectMoveAction(&battle, this));
143 void BattleState::OnPauseState(SDL_Surface *screen) {
147 void BattleState::HandleEvents(const Input &input) {
151 void BattleState::UpdateWorld(Uint32 deltaT) {
155 void BattleState::Render(SDL_Surface *screen) {
157 RenderBackground(screen);
158 RenderMonsters(screen);
161 void BattleState::RenderBackground(SDL_Surface *screen) {
164 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
166 destRect.x = offset.X();
167 destRect.y = offset.Y();
168 destRect.w = background->w;
169 destRect.h = background->h;
170 SDL_BlitSurface(background, 0, screen, &destRect);
173 void BattleState::RenderMonsters(SDL_Surface *screen) {
175 for (vector<Monster>::size_type i(0), end(battle.NumMonsters()); i < end; ++i) {
176 if (battle.MonsterPositionOccupied(i)) {
177 Monster &monster(battle.MonsterAt(i));
178 if (monster.GetAnimation().Running()) {
179 monster.GetAnimation().DrawCenter(screen, monster.Position() + offset);
181 monster.Sprite()->DrawCenter(screen, monster.Position() + offset);
187 void BattleState::RenderHeroes(SDL_Surface *screen) {
189 for (int i(0); i < NumHeroes(); ++i) {
190 Hero &hero(battle.HeroAt(i));
191 if (hero.GetAnimation().Running()) {
192 hero.GetAnimation().DrawCenter(screen, hero.Position() + offset);
194 int row(hero.Health() > 0 ? 0 : 2);
195 hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row);
200 void BattleState::RenderCapsule(SDL_Surface *screen) {
201 const Capsule &capsule(battle.GetCapsule());
202 if (!capsule.Active() || capsule.Health() <= 0) return;
203 if (capsule.GetAnimation().Running()) {
204 capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset);
206 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
210 void BattleState::RenderHeroTags(SDL_Surface *screen) {
212 int tagHeight(attackTypeMenu.Height());
213 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
215 for (int i(0); i < battle.NumHeroes(); ++i) {
216 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i));
220 void BattleState::RenderSmallHeroTags(SDL_Surface *screen) {
222 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
223 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
227 rect.y = offset.Y() + Height() - tagHeight;
230 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
231 rect.y += res->normalFont->CharHeight() / 8;
232 rect.h -= res->normalFont->CharHeight() / 4;
233 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
235 for (int i(0); i < battle.NumHeroes(); ++i) {
236 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);