]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
added spells
[l2e.git] / src / battle / BattleState.cpp
1 /*
2  * BattleState.cpp
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #include "BattleState.h"
9
10 #include "PartyLayout.h"
11 #include "states/SelectMoveAction.h"
12 #include "../app/Application.h"
13 #include "../app/Input.h"
14 #include "../common/Inventory.h"
15 #include "../common/Item.h"
16 #include "../common/Spell.h"
17 #include "../geometry/operators.h"
18 #include "../graphics/Sprite.h"
19
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 geometry::Point;
28 using geometry::Vector;
29 using graphics::Menu;
30
31 using std::vector;
32
33 namespace battle {
34
35 void BattleState::AddMonster(const Monster &m) {
36         if (monsters.size() >= monstersLayout->NumPositions()) {
37                 throw std::overflow_error("too many monsters for layout");
38         }
39         monsters.push_back(m);
40 }
41
42 void BattleState::AddHero(const Hero &h) {
43         if (heroes.size() >= heroesLayout->NumPositions()) {
44                 throw std::overflow_error("too many heroes for layout");
45         }
46         heroes.push_back(h);
47 }
48
49
50 void BattleState::Resize(int w, int h) {
51
52 }
53
54
55 void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
56         monstersLayout->CalculatePositions(background->w, background->h, monsterPositions);
57         heroesLayout->CalculatePositions(background->w, background->h, heroesPositions);
58         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
59                 spellMenus.push_back(res->spellMenuPrototype);
60                 LoadSpellMenu(i);
61                 ikariMenus.push_back(res->ikariMenuPrototype);
62                 // TODO: insert ikari menu entries
63                 heroTags[i] = HeroTag(&heroes[i], attackChoices + i, res, HeroTag::Alignment((i + 1) % 2));
64         }
65
66         int tagHeight(attackTypeMenu.Height());
67         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
68         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
69         heroTagPositions[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
70         heroTagPositions[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
71         heroTagPositions[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
72         heroTagPositions[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
73
74         // TODO: insert item menu entries
75         itemMenu = res->itemMenuPrototype;
76         LoadInventory();
77 }
78
79 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
80         spellMenus[index].Clear();
81         spellMenus[index].Reserve(HeroAt(index).Spells().size());
82         for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
83                 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
84                 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
85         }
86 }
87
88 void BattleState::LoadInventory() {
89         const Inventory &inv(*res->inventory);
90         itemMenu.Clear();
91         itemMenu.Reserve(inv.MaxItems());
92         for (int i(0); i < inv.MaxItems(); ++i) {
93                 const Item *item(inv.ItemAt(i));
94                 if (item) {
95                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
96                 } else {
97                         itemMenu.AddEmptyEntry();
98                 }
99         }
100 }
101
102 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
103
104 }
105
106 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
107         // TODO: check for victory, defeat or run
108         // reset attack choices
109         activeHero = -1;
110         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
111                 attackChoices[i] = AttackChoice(this);
112         }
113         ctrl.PushState(new SelectMoveAction(this));
114 }
115
116 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
117
118 }
119
120
121 void BattleState::HandleInput(const Input &input) {
122
123 }
124
125 void BattleState::UpdateWorld(float deltaT) {
126
127 }
128
129 void BattleState::Render(SDL_Surface *screen) {
130         Vector<int> offset(CalculateScreenOffset(screen));
131
132         RenderBackground(screen, offset);
133         RenderMonsters(screen, offset);
134 //      RenderHeroes(screen, offset);
135         RenderHeroTags(screen, offset);
136 }
137
138 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
139         // black for now
140         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
141         SDL_Rect destRect;
142         destRect.x = offset.X();
143         destRect.y = offset.Y();
144         destRect.w = background->w;
145         destRect.h = background->h;
146         SDL_BlitSurface(background, 0, screen, &destRect);
147 }
148
149 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
150         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
151                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
152         }
153 }
154
155 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
156         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
157                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
158         }
159 }
160
161 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
162         int tagHeight(attackTypeMenu.Height());
163         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
164
165         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
166                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
167         }
168 }
169
170 }