]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.cpp
preliminary solution for loading inventory in item select
[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 "../geometry/operators.h"
17 #include "../graphics/Sprite.h"
18
19 #include <iomanip>
20 #include <stdexcept>
21 #include <sstream>
22
23 using app::Application;
24 using app::Input;
25 using common::Inventory;
26 using common::Item;
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         attackChoices.resize(heroes.size());
59         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
60                 spellMenus.push_back(res->spellMenuPrototype);
61                 // TODO: insert spell menu entries
62                 ikariMenus.push_back(res->ikariMenuPrototype);
63                 // TODO: insert ikari menu entries
64                 heroTags.push_back(HeroTag(&heroes[i], &attackChoices[i], res, HeroTag::Alignment((i + 1) % 2)));
65         }
66         // TODO: insert item menu entries
67         itemMenu = res->itemMenuPrototype;
68         LoadInventory();
69 }
70
71 void BattleState::LoadInventory() {
72         const Inventory &inv(*res->inventory);
73         itemMenu.Clear();
74         itemMenu.Reserve(inv.MaxItems());
75         itemMenuStrings.clear();
76         itemMenuStrings.resize(inv.MaxItems());
77         int itemNameLength(itemMenu.CharsPerEntry() - 3);
78         // TODO: better (maybe intrusive) solution for menus with counts
79         for (int i(0); i < inv.MaxItems(); ++i) {
80                 const Item *item(inv.ItemAt(i));
81                 if (item) {
82                         std::stringstream s;
83                         s << std::setw(itemNameLength) << std::left << std::setfill(' ') << item->Name();
84                         s << ':';
85                         s << inv.ItemCountAt(i);
86                         itemMenuStrings[i] = s.str();
87                         itemMenu.Add(itemMenuStrings[i].c_str(), item, item->CanUseInBattle(), item->MenuIcon());
88                 } else {
89                         itemMenu.Add("", 0, false);
90                 }
91         }
92 }
93
94 void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) {
95
96 }
97
98 void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) {
99         // TODO: check for victory, defeat or run
100         // reset attack choices
101         activeHero = -1;
102         attackChoices.clear();
103         attackChoices.resize(heroes.size());
104         ctrl.PushState(new SelectMoveAction(this));
105 }
106
107 void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) {
108
109 }
110
111
112 void BattleState::HandleInput(const Input &input) {
113
114 }
115
116 void BattleState::UpdateWorld(float deltaT) {
117
118 }
119
120 void BattleState::Render(SDL_Surface *screen) {
121         Vector<int> offset(CalculateScreenOffset(screen));
122
123         RenderBackground(screen, offset);
124         RenderMonsters(screen, offset);
125 //      RenderHeroes(screen, offset);
126         RenderHeroTags(screen, offset);
127 }
128
129 void BattleState::RenderBackground(SDL_Surface *screen, const Vector<int> &offset) {
130         // black for now
131         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
132         SDL_Rect destRect;
133         destRect.x = offset.X();
134         destRect.y = offset.Y();
135         destRect.w = background->w;
136         destRect.h = background->h;
137         SDL_BlitSurface(background, 0, screen, &destRect);
138 }
139
140 void BattleState::RenderMonsters(SDL_Surface *screen, const Vector<int> &offset) {
141         for (vector<Monster>::size_type i(0), end(monsters.size()); i < end; ++i) {
142                 monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset);
143         }
144 }
145
146 void BattleState::RenderHeroes(SDL_Surface *screen, const Vector<int> &offset) {
147         for (vector<Hero>::size_type i(0), end(heroes.size()); i < end; ++i) {
148                 heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset);
149         }
150 }
151
152 void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector<int> &offset) {
153         int tagHeight(attackTypeMenu.Height());
154         int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2);
155         int xOffset((BackgroundWidth() - 2 * tagWidth) / 2);
156
157         Point<int> tagPosition[4];
158         tagPosition[0] = Point<int>(xOffset, BackgroundHeight() - 2 * tagHeight);
159         tagPosition[1] = Point<int>(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight);
160         tagPosition[2] = Point<int>(xOffset, BackgroundHeight() - tagHeight);
161         tagPosition[3] = Point<int>(xOffset + tagWidth, BackgroundHeight() - tagHeight);
162
163         for (vector<HeroTag>::size_type i(0), end(heroTags.size()); i < end; ++i) {
164                 heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, (int)i == activeHero);
165         }
166 }
167
168 }