1 #include "BattleState.h"
3 #include "PartyLayout.h"
4 #include "states/DefeatState.h"
5 #include "states/SelectMoveAction.h"
6 #include "states/PerformAttacks.h"
7 #include "states/VictoryState.h"
8 #include "../app/Application.h"
9 #include "../app/Input.h"
10 #include "../common/GameState.h"
11 #include "../common/Ikari.h"
12 #include "../common/Inventory.h"
13 #include "../common/Item.h"
14 #include "../common/Spell.h"
15 #include "../graphics/Frame.h"
16 #include "../graphics/Sprite.h"
17 #include "../math/Vector.h"
24 using app::Application;
26 using common::Inventory;
38 void BattleState::AddMonster(const Monster &m) {
42 void BattleState::AddHero(const Hero &h) {
46 void BattleState::SetCapsule(const Capsule &c) {
51 void BattleState::OnResize(int w, int h) {
53 (w - background->w) / 2,
54 (h - background->h) / 2);
58 void BattleState::OnEnterState(SDL_Surface *screen) {
59 for (int i(0); i < 4; ++i) {
60 Hero &hero = HeroAt(i);
61 hero.Position() = battle.HeroesLayout().CalculatePosition(i, background->w, background->h);
62 hero.SpellMenu() = *res->spellMenuProperties;
63 hero.UpdateSpellMenu();
64 hero.IkariMenu() = *res->ikariMenuProperties;
65 hero.UpdateIkariMenu(res);
66 heroTags[i] = HeroTag(this, i);
67 smallHeroTags[i] = SmallHeroTag(this, i);
70 battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h);
72 for (int i(0); i < battle.NumMonsters(); ++i) {
73 MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h);
76 int tagHeight(attackTypeMenu.Height());
77 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
78 int xOffset((Width() - 2 * tagWidth) / 2);
79 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
80 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
81 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
82 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
84 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
85 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
86 xOffset = (Width() - 4 * tagWidth) / 2;
87 int yOffset(Height() - tagHeight);
88 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
89 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
90 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
91 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
93 OnResize(screen->w, screen->h);
95 itemMenu = *res->itemMenuProperties;
97 battle.ClearAllAttacks();
100 void BattleState::LoadInventory() {
101 const Inventory &inv(game->state->inventory);
103 itemMenu.Reserve(inv.MaxItems());
104 for (int i(0); i < inv.MaxItems(); ++i) {
105 const Item *item(inv.ItemAt(i));
107 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
109 itemMenu.AddEmptyEntry();
114 void BattleState::OnExitState(SDL_Surface *screen) {
118 void BattleState::OnResumeState(SDL_Surface *screen) {
120 Ctrl().PopState(); // quit the battle scene
123 if (battle.Victory()) {
127 Ctrl().PushState(new VictoryState(&battle, this));
128 alreadyPushed = true;
132 if (battle.Defeat()) {
136 Ctrl().PushState(new DefeatState(&battle, this));
137 alreadyPushed = true;
141 // TODO: this should not push a state while quitting
142 if (battle.AttackSelectionDone()) {
143 Ctrl().PushState(new PerformAttacks(&battle, this));
145 Ctrl().PushState(new SelectMoveAction(&battle, this));
149 void BattleState::OnPauseState(SDL_Surface *screen) {
153 void BattleState::HandleEvents(const Input &input) {
157 void BattleState::UpdateWorld(Uint32 deltaT) {
161 void BattleState::Render(SDL_Surface *screen) {
163 RenderBackground(screen);
164 RenderMonsters(screen);
167 void BattleState::RenderBackground(SDL_Surface *screen) {
170 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
172 destRect.x = offset.X();
173 destRect.y = offset.Y();
174 destRect.w = background->w;
175 destRect.h = background->h;
176 SDL_BlitSurface(background, 0, screen, &destRect);
179 void BattleState::RenderMonsters(SDL_Surface *screen) {
181 for (vector<Monster>::size_type i(0), end(battle.NumMonsters()); i < end; ++i) {
182 if (battle.MonsterPositionOccupied(i)) {
183 Monster &monster(battle.MonsterAt(i));
184 if (monster.GetAnimation().Running()) {
185 monster.GetAnimation().DrawCenter(screen, monster.Position() + offset);
187 monster.Sprite()->DrawCenter(screen, monster.Position() + offset);
193 void BattleState::RenderHeroes(SDL_Surface *screen) {
195 for (int i(0); i < NumHeroes(); ++i) {
196 Hero &hero(battle.HeroAt(i));
197 if (hero.GetAnimation().Running()) {
198 hero.GetAnimation().DrawCenter(screen, hero.Position() + offset);
200 int row(hero.Health() > 0 ? 0 : 2);
201 hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row);
206 void BattleState::RenderCapsule(SDL_Surface *screen) {
207 const Capsule &capsule(battle.GetCapsule());
208 if (!capsule.Active() || capsule.Health() <= 0) return;
209 if (capsule.GetAnimation().Running()) {
210 capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset);
212 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
216 void BattleState::RenderHeroTags(SDL_Surface *screen) {
218 int tagHeight(attackTypeMenu.Height());
219 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
221 for (int i(0); i < battle.NumHeroes(); ++i) {
222 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i));
226 void BattleState::RenderSmallHeroTags(SDL_Surface *screen) {
228 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
229 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
233 rect.y = offset.Y() + Height() - tagHeight;
236 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
237 rect.y += res->normalFont->CharHeight() / 8;
238 rect.h -= res->normalFont->CharHeight() / 4;
239 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
241 for (int i(0); i < battle.NumHeroes(); ++i) {
242 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);