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