4 * Created on: Nov 18, 2012
10 #include "HeroStatus.h"
11 #include "PartyMenu.h"
12 #include "Resources.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/GameConfig.h"
16 #include "../common/GameState.h"
17 #include "../common/Hero.h"
18 #include "../common/Inventory.h"
19 #include "../common/Item.h"
20 #include "../common/Stats.h"
21 #include "../graphics/Font.h"
22 #include "../graphics/Frame.h"
26 using common::Inventory;
29 using geometry::Vector;
31 using graphics::Frame;
35 EquipMenu::EquipMenu(PartyMenu *parent, int cursor)
38 , actionMenu(*parent->Res().equipmentActionMenuProperties)
39 , equipmentMenu(*parent->Res().equipmentMenuProperties)
40 , inventoryMenu(*parent->Res().inventoryMenuProperties) {
41 actionMenu.Add(parent->Res().equipMenuEquipLabel, CHOICE_EQUIP);
42 actionMenu.Add(parent->Res().equipMenuStrongestLabel, CHOICE_STRONGEST);
43 actionMenu.Add(parent->Res().equipMenuRemoveLabel, CHOICE_REMOVE);
44 actionMenu.Add(parent->Res().equipMenuRemoveAllLabel, CHOICE_REMOVE_ALL);
45 actionMenu.Add(parent->Res().equipMenuDropLabel, CHOICE_DROP);
51 void EquipMenu::OnEnterState(SDL_Surface *) {
52 equipmentMenu.SetInactive();
55 void EquipMenu::OnExitState(SDL_Surface *) {
59 void EquipMenu::OnResumeState(SDL_Surface *) {
63 void EquipMenu::OnPauseState(SDL_Surface *) {
68 void EquipMenu::OnResize(int width, int height) {
73 void EquipMenu::HandleEvents(const Input &input) {
74 if (actionMenu.IsActive()) {
75 if (input.JustPressed(Input::PAD_UP)) {
76 actionMenu.PreviousRow();
78 if (input.JustPressed(Input::PAD_DOWN)) {
81 if (input.JustPressed(Input::ACTION_A)) {
82 switch (actionMenu.Selected()) {
84 actionMenu.SetSelected();
85 equipmentMenu.SetActive();
87 case CHOICE_STRONGEST:
90 actionMenu.SetSelected();
91 equipmentMenu.SetActive();
93 case CHOICE_REMOVE_ALL:
97 actionMenu.SetSelected();
98 equipmentMenu.SetActive();
101 } else if (input.JustPressed(Input::ACTION_B)) {
107 void EquipMenu::UpdateWorld(float deltaT) {
111 void EquipMenu::Render(SDL_Surface *screen) {
112 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
113 Vector<int> shoulderNavOffset(
114 5 * parent->Res().statusFont->CharWidth(),
115 parent->Res().statusFont->CharHeight());
116 Vector<int> statsOffset(
117 4 * parent->Res().statusFont->CharWidth(),
118 8 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
119 Vector<int> equipOffset(
120 17 * parent->Res().statusFont->CharWidth(),
121 4 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
122 Vector<int> menuOffset(
123 15 * parent->Res().statusFont->CharWidth(),
124 17 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
126 parent->RenderBackground(screen);
127 parent->Res().shoulderNav->Draw(screen, offset + shoulderNavOffset);
128 RenderStatus(screen, offset + parent->StatusOffset(0));
129 RenderStats(screen, offset + statsOffset);
130 RenderEquipmentMenu(screen, offset + equipOffset);
131 RenderActionMenu(screen, offset + menuOffset);
134 int EquipMenu::Width() const {
135 return parent->Width();
138 int EquipMenu::Height() const {
139 return parent->Height();
142 void EquipMenu::RenderStatus(SDL_Surface *screen, const Vector<int> &offset) const {
143 parent->GetHeroStatus(cursor).Render(screen, offset);
146 void EquipMenu::RenderStats(SDL_Surface *screen, const Vector<int> &offset) const {
147 const Stats &stats(GetHero().GetStats());
148 Vector<int> lineBreak(0, parent->Res().statusFont->CharHeight());
150 Vector<int> position(offset);
151 RenderStatsLine(parent->Res().atpLabel, stats.Attack(), screen, position);
153 position += lineBreak;
154 RenderStatsLine(parent->Res().dfpLabel, stats.Defense(), screen, position);
156 position += lineBreak;
157 RenderStatsLine(parent->Res().strLabel, stats.Strength(), screen, position);
159 position += lineBreak;
160 RenderStatsLine(parent->Res().aglLabel, stats.Agility(), screen, position);
162 position += lineBreak;
163 RenderStatsLine(parent->Res().intLabel, stats.Intelligence(), screen, position);
165 position += lineBreak;
166 RenderStatsLine(parent->Res().gutLabel, stats.Gut(), screen, position);
168 position += lineBreak;
169 RenderStatsLine(parent->Res().mgrLabel, stats.MagicResistance(), screen, position);
172 void EquipMenu::RenderStatsLine(const char *label, int number, SDL_Surface *screen, const Vector<int> &position) const {
173 const Font &font(*parent->Res().statusFont);
174 const Vector<int> numberOffset(4 * font.CharWidth(), 0);
176 font.DrawString(label, screen, position, 3);
177 font.DrawNumber(number, screen, position + numberOffset, 3);
180 void EquipMenu::RenderEquipmentMenu(SDL_Surface *screen, const Vector<int> &offset) const {
181 equipmentMenu.Draw(screen, offset);
184 void EquipMenu::RenderActionMenu(SDL_Surface *screen, const Vector<int> &offset) const {
185 const Font &font(*parent->Res().statusFont);
186 const Frame &frame(*parent->Res().statusFrame);
187 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 2);
189 frame.Draw(screen, offset, 15 * font.CharWidth(), 10 * font.CharHeight());
190 actionMenu.Draw(screen, offset + menuOffset);
193 void EquipMenu::RenderInventoryMenu(SDL_Surface *screen, const Vector<int> &offset) const {
198 void EquipMenu::NextHero() {
199 cursor = (cursor + 1) % parent->Game().state->partySize;
203 void EquipMenu::PreviousHero() {
204 cursor = (cursor + parent->Game().state->partySize - 1) % parent->Game().state->partySize;
208 Hero &EquipMenu::GetHero() {
209 return *parent->Game().state->party[cursor];
212 const Hero &EquipMenu::GetHero() const {
213 return *parent->Game().state->party[cursor];
217 void EquipMenu::LoadEquipment() {
218 equipmentMenu.Clear();
219 if (GetHero().HasWeapon()) {
220 equipmentMenu.Add(GetHero().Weapon()->Name(), GetHero().Weapon(), true, GetHero().Weapon()->MenuIcon());
222 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
224 if (GetHero().HasArmor()) {
225 equipmentMenu.Add(GetHero().Armor()->Name(), GetHero().Armor(), true, GetHero().Armor()->MenuIcon());
227 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
229 if (GetHero().HasShield()) {
230 equipmentMenu.Add(GetHero().Shield()->Name(), GetHero().Shield(), true, GetHero().Shield()->MenuIcon());
232 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
234 if (GetHero().HasHelmet()) {
235 equipmentMenu.Add(GetHero().Helmet()->Name(), GetHero().Helmet(), true, GetHero().Helmet()->MenuIcon());
237 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
239 if (GetHero().HasRing()) {
240 equipmentMenu.Add(GetHero().Ring()->Name(), GetHero().Ring(), true, GetHero().Ring()->MenuIcon());
242 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
244 if (GetHero().HasJewel()) {
245 equipmentMenu.Add(GetHero().Jewel()->Name(), GetHero().Jewel(), true, GetHero().Jewel()->MenuIcon());
247 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
251 void EquipMenu::RemoveAllEquipment() {
252 Inventory &inv(parent->Game().state->inventory);
253 if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
254 GetHero().RemoveWeapon();
256 if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
257 GetHero().RemoveArmor();
259 if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
260 GetHero().RemoveShield();
262 if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
263 GetHero().RemoveHelmet();
265 if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
266 GetHero().RemoveRing();
268 if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
269 GetHero().RemoveJewel();