1 #include "BattleState.h"
3 #include "PartyLayout.h"
4 #include "states/SelectMoveAction.h"
5 #include "states/PerformAttacks.h"
6 #include "../app/Application.h"
7 #include "../app/Input.h"
8 #include "../common/GameState.h"
9 #include "../common/Ikari.h"
10 #include "../common/Inventory.h"
11 #include "../common/Item.h"
12 #include "../common/Spell.h"
13 #include "../graphics/Frame.h"
14 #include "../graphics/Sprite.h"
15 #include "../math/Vector.h"
22 using app::Application;
24 using common::Inventory;
36 void BattleState::AddMonster(const Monster &m) {
40 void BattleState::AddHero(const Hero &h) {
44 void BattleState::SetCapsule(const Capsule &c) {
49 void BattleState::OnResize(int w, int h) {
51 (w - background->w) / 2,
52 (h - background->h) / 2);
56 void BattleState::OnEnterState(SDL_Surface *screen) {
57 for (int i(0); i < 4; ++i) {
58 Hero &hero = HeroAt(i);
59 hero.Position() = battle.HeroesLayout().CalculatePosition(i, background->w, background->h);
60 hero.SpellMenu() = *res->spellMenuProperties;
61 hero.UpdateSpellMenu();
62 hero.IkariMenu() = *res->ikariMenuProperties;
63 hero.UpdateIkariMenu(res);
64 heroTags[i] = HeroTag(this, i);
65 smallHeroTags[i] = SmallHeroTag(this, i);
68 battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h);
70 for (int i(0); i < battle.NumMonsters(); ++i) {
71 MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h);
74 int tagHeight(attackTypeMenu.Height());
75 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
76 int xOffset((Width() - 2 * tagWidth) / 2);
77 heroTagPositions[0] = Vector<int>(xOffset, Height() - 2 * tagHeight);
78 heroTagPositions[1] = Vector<int>(xOffset + tagWidth, Height() - 2 * tagHeight);
79 heroTagPositions[2] = Vector<int>(xOffset, Height() - tagHeight);
80 heroTagPositions[3] = Vector<int>(xOffset + tagWidth, Height() - tagHeight);
82 tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2;
83 tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2;
84 xOffset = (Width() - 4 * tagWidth) / 2;
85 int yOffset(Height() - tagHeight);
86 smallHeroTagPositions[0] = Vector<int>(xOffset, yOffset);
87 smallHeroTagPositions[1] = Vector<int>(xOffset + 2 * tagWidth, yOffset);
88 smallHeroTagPositions[2] = Vector<int>(xOffset + tagWidth, yOffset);
89 smallHeroTagPositions[3] = Vector<int>(xOffset + 3 * tagWidth, yOffset);
91 OnResize(screen->w, screen->h);
93 itemMenu = *res->itemMenuProperties;
95 battle.ClearAllAttacks();
98 void BattleState::LoadInventory() {
99 const Inventory &inv(game->state->inventory);
101 itemMenu.Reserve(inv.MaxItems());
102 for (int i(0); i < inv.MaxItems(); ++i) {
103 const Item *item(inv.ItemAt(i));
105 itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
107 itemMenu.AddEmptyEntry();
112 void BattleState::OnExitState(SDL_Surface *screen) {
116 void BattleState::OnResumeState(SDL_Surface *screen) {
118 Ctrl().PopState(); // quit the battle scene
121 if (battle.Victory()) {
125 if (battle.Defeat()) {
129 // TODO: this should not push a state while quitting
130 if (battle.AttackSelectionDone()) {
131 Ctrl().PushState(new PerformAttacks(&battle, this));
133 Ctrl().PushState(new SelectMoveAction(&battle, this));
137 void BattleState::OnPauseState(SDL_Surface *screen) {
141 void BattleState::HandleEvents(const Input &input) {
145 void BattleState::UpdateWorld(Uint32 deltaT) {
149 void BattleState::Render(SDL_Surface *screen) {
151 RenderBackground(screen);
152 RenderMonsters(screen);
155 void BattleState::RenderBackground(SDL_Surface *screen) {
158 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
160 destRect.x = offset.X();
161 destRect.y = offset.Y();
162 destRect.w = background->w;
163 destRect.h = background->h;
164 SDL_BlitSurface(background, 0, screen, &destRect);
167 void BattleState::RenderMonsters(SDL_Surface *screen) {
169 for (vector<Monster>::size_type i(0), end(battle.NumMonsters()); i < end; ++i) {
170 if (battle.MonsterPositionOccupied(i)) {
171 Monster &monster(battle.MonsterAt(i));
172 if (monster.GetAnimation().Running()) {
173 monster.GetAnimation().DrawCenter(screen, monster.Position() + offset);
175 monster.Sprite()->DrawCenter(screen, monster.Position() + offset);
181 void BattleState::RenderHeroes(SDL_Surface *screen) {
183 for (int i(0); i < NumHeroes(); ++i) {
184 Hero &hero(battle.HeroAt(i));
185 if (hero.GetAnimation().Running()) {
186 hero.GetAnimation().DrawCenter(screen, hero.Position() + offset);
188 int row(hero.Health() > 0 ? 0 : 2);
189 hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row);
194 void BattleState::RenderCapsule(SDL_Surface *screen) {
195 const Capsule &capsule(battle.GetCapsule());
196 if (!capsule.Active() || capsule.Health() <= 0) return;
197 if (capsule.GetAnimation().Running()) {
198 capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset);
200 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
204 void BattleState::RenderHeroTags(SDL_Surface *screen) {
206 int tagHeight(attackTypeMenu.Height());
207 int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
209 for (int i(0); i < battle.NumHeroes(); ++i) {
210 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i));
214 void BattleState::RenderSmallHeroTags(SDL_Surface *screen) {
216 int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
217 int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
221 rect.y = offset.Y() + Height() - tagHeight;
224 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0));
225 rect.y += res->normalFont->CharHeight() / 8;
226 rect.h -= res->normalFont->CharHeight() / 4;
227 SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format));
229 for (int i(0); i < battle.NumHeroes(); ++i) {
230 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);