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