]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added basic defeat state
[l2e.git] / src / battle / BattleState.cpp
1 #include "BattleState.h"
2
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"
18
19 #include <algorithm>
20 #include <cassert>
21 #include <cstdlib>
22 #include <stdexcept>
23
24 using app::Application;
25 using app::Input;
26 using common::Inventory;
27 using common::Item;
28 using common::Spell;
29 using common::Stats;
30 using math::Vector;
31 using graphics::Menu;
32
33 using std::rand;
34 using std::vector;
35
36 namespace battle {
37
38 void BattleState::AddMonster(const Monster &m) {
39         battle.AddMonster(m);
40 }
41
42 void BattleState::AddHero(const Hero &h) {
43         battle.AddHero(h);
44 }
45
46 void BattleState::SetCapsule(const Capsule &c) {
47         battle.SetCapsule(c);
48 }
49
50
51 void BattleState::OnResize(int w, int h) {
52         offset = Vector<int>(
53                                 (w - background->w) / 2,
54                                 (h - background->h) / 2);
55 }
56
57
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);
68         }
69
70         battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h);
71
72         for (int i(0); i < battle.NumMonsters(); ++i) {
73                 MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h);
74         }
75
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);
83
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);
92
93         OnResize(screen->w, screen->h);
94
95         itemMenu = *res->itemMenuProperties;
96         LoadInventory();
97         battle.ClearAllAttacks();
98 }
99
100 void BattleState::LoadInventory() {
101         const Inventory &inv(game->state->inventory);
102         itemMenu.Clear();
103         itemMenu.Reserve(inv.MaxItems());
104         for (int i(0); i < inv.MaxItems(); ++i) {
105                 const Item *item(inv.ItemAt(i));
106                 if (item) {
107                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
108                 } else {
109                         itemMenu.AddEmptyEntry();
110                 }
111         }
112 }
113
114 void BattleState::OnExitState(SDL_Surface *screen) {
115
116 }
117
118 void BattleState::OnResumeState(SDL_Surface *screen) {
119         if (ranAway) {
120                 Ctrl().PopState(); // quit the battle scene
121                 return;
122         }
123         if (battle.Victory()) {
124                 if (alreadyPushed) {
125                         Ctrl().PopState();
126                 } else {
127                         Ctrl().PushState(new VictoryState(&battle, this));
128                         alreadyPushed = true;
129                 }
130                 return;
131         }
132         if (battle.Defeat()) {
133                 if (alreadyPushed) {
134                         Ctrl().PopState();
135                 } else {
136                         Ctrl().PushState(new DefeatState(&battle, this));
137                         alreadyPushed = true;
138                 }
139                 return;
140         }
141         // TODO: this should not push a state while quitting
142         if (battle.AttackSelectionDone()) {
143                 Ctrl().PushState(new PerformAttacks(&battle, this));
144         } else {
145                 Ctrl().PushState(new SelectMoveAction(&battle, this));
146         }
147 }
148
149 void BattleState::OnPauseState(SDL_Surface *screen) {
150
151 }
152
153 void BattleState::HandleEvents(const Input &input) {
154
155 }
156
157 void BattleState::UpdateWorld(Uint32 deltaT) {
158
159 }
160
161 void BattleState::Render(SDL_Surface *screen) {
162         assert(screen);
163         RenderBackground(screen);
164         RenderMonsters(screen);
165 }
166
167 void BattleState::RenderBackground(SDL_Surface *screen) {
168         assert(screen);
169         // black for now
170         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
171         SDL_Rect destRect;
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);
177 }
178
179 void BattleState::RenderMonsters(SDL_Surface *screen) {
180         assert(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);
186                         } else {
187                                 monster.Sprite()->DrawCenter(screen, monster.Position() + offset);
188                         }
189                 }
190         }
191 }
192
193 void BattleState::RenderHeroes(SDL_Surface *screen) {
194         assert(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);
199                 } else {
200                         int row(hero.Health() > 0 ? 0 : 2);
201                         hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row);
202                 }
203         }
204 }
205
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);
211         } else {
212                 capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset);
213         }
214 }
215
216 void BattleState::RenderHeroTags(SDL_Surface *screen) {
217         assert(screen);
218         int tagHeight(attackTypeMenu.Height());
219         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
220
221         for (int i(0); i < battle.NumHeroes(); ++i) {
222                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i));
223         }
224 }
225
226 void BattleState::RenderSmallHeroTags(SDL_Surface *screen) {
227         assert(screen);
228         int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2);
229         int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);
230
231         SDL_Rect rect;
232         rect.x = offset.X();
233         rect.y = offset.Y() + Height() - tagHeight;
234         rect.w = Width();
235         rect.h = 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));
240
241         for (int i(0); i < battle.NumHeroes(); ++i) {
242                 smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset);
243         }
244 }
245
246 }