]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
set ikari menu to point to items
[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         itemMenu = res->itemMenuPrototype;
75         LoadInventory();
76 }
77
78 void BattleState::LoadSpellMenu(vector<Hero>::size_type index) {
79         spellMenus[index].Clear();
80         spellMenus[index].Reserve(HeroAt(index).Spells().size());
81         for (vector<const Spell *>::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) {
82                 bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana());
83                 spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost());
84         }
85 }
86
87 void BattleState::LoadInventory() {
88         const Inventory &inv(*res->inventory);
89         itemMenu.Clear();
90         itemMenu.Reserve(inv.MaxItems());
91         for (int i(0); i < inv.MaxItems(); ++i) {
92                 const Item *item(inv.ItemAt(i));
93                 if (item) {
94                         itemMenu.Add(item->Name(), item, item->CanUseInBattle(), item->MenuIcon(), inv.ItemCountAt(i));
95                 } else {
96                         itemMenu.AddEmptyEntry();
97                 }
98         }
99 }
100
101 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
102
103 }
104
105 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
106         // TODO: check for victory, defeat or run
107         // reset attack choices
108         activeHero = -1;
109         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
110                 attackChoices[i] = AttackChoice(this);
111         }
112         ctrl.PushState(new SelectMoveAction(this));
113 }
114
115 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
116
117 }
118
119
120 void BattleState::HandleInput(const Input &input) {
121
122 }
123
124 void BattleState::UpdateWorld(float deltaT) {
125
126 }
127
128 void BattleState::Render(SDL_Surface *screen) {
129         Vector<int> offset(CalculateScreenOffset(screen));
130
131         RenderBackground(screen, offset);
132         RenderMonsters(screen, offset);
133 //      RenderHeroes(screen, offset);
134         RenderHeroTags(screen, offset);
135 }
136
137 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
138         // black for now
139         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
140         SDL_Rect destRect;
141         destRect.x = offset.X();
142         destRect.y = offset.Y();
143         destRect.w = background->w;
144         destRect.h = background->h;
145         SDL_BlitSurface(background, 0, screen, &destRect);
146 }
147
148 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
149         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
150                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
151         }
152 }
153
154 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
155         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
156                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
157         }
158 }
159
160 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
161         int tagHeight(attackTypeMenu.Height());
162         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
163
164         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
165                 heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero);
166         }
167 }
168
169 }