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:
91 actionMenu.SetSelected();
92 equipmentMenu.SetActive();
94 case CHOICE_REMOVE_ALL:
98 actionMenu.SetSelected();
99 equipmentMenu.SetActive();
102 } else if (input.JustPressed(Input::ACTION_B)) {
105 } else if (equipmentMenu.IsActive()) {
106 if (input.JustPressed(Input::PAD_UP)) {
107 equipmentMenu.PreviousRow();
109 if (input.JustPressed(Input::PAD_DOWN)) {
110 equipmentMenu.NextRow();
112 if (input.JustPressed(Input::ACTION_B)) {
113 equipmentMenu.SetInactive();
114 actionMenu.SetActive();
115 } else if (input.JustPressed(Input::ACTION_A)) {
116 switch (actionMenu.Selected()) {
120 case CHOICE_STRONGEST:
121 case CHOICE_REMOVE_ALL:
122 // invalid state, recover
123 equipmentMenu.SetInactive();
124 actionMenu.SetActive();
137 void EquipMenu::UpdateWorld(float deltaT) {
141 void EquipMenu::Render(SDL_Surface *screen) {
142 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
143 Vector<int> shoulderNavOffset(
144 5 * parent->Res().statusFont->CharWidth(),
145 parent->Res().statusFont->CharHeight());
146 Vector<int> statsOffset(
147 4 * parent->Res().statusFont->CharWidth(),
148 8 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
149 Vector<int> equipOffset(
150 17 * parent->Res().statusFont->CharWidth(),
151 4 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
152 Vector<int> menuOffset(
153 15 * parent->Res().statusFont->CharWidth(),
154 17 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
156 parent->RenderBackground(screen);
157 parent->Res().shoulderNav->Draw(screen, offset + shoulderNavOffset);
158 RenderStatus(screen, offset + parent->StatusOffset(0));
159 RenderStats(screen, offset + statsOffset);
160 RenderEquipmentMenu(screen, offset + equipOffset);
161 RenderActionMenu(screen, offset + menuOffset);
164 int EquipMenu::Width() const {
165 return parent->Width();
168 int EquipMenu::Height() const {
169 return parent->Height();
172 void EquipMenu::RenderStatus(SDL_Surface *screen, const Vector<int> &offset) const {
173 parent->GetHeroStatus(cursor).Render(screen, offset);
176 void EquipMenu::RenderStats(SDL_Surface *screen, const Vector<int> &offset) const {
177 const Stats &stats(GetHero().GetStats());
178 Vector<int> lineBreak(0, parent->Res().statusFont->CharHeight());
180 Vector<int> position(offset);
181 RenderStatsLine(parent->Res().atpLabel, stats.Attack(), screen, position);
183 position += lineBreak;
184 RenderStatsLine(parent->Res().dfpLabel, stats.Defense(), screen, position);
186 position += lineBreak;
187 RenderStatsLine(parent->Res().strLabel, stats.Strength(), screen, position);
189 position += lineBreak;
190 RenderStatsLine(parent->Res().aglLabel, stats.Agility(), screen, position);
192 position += lineBreak;
193 RenderStatsLine(parent->Res().intLabel, stats.Intelligence(), screen, position);
195 position += lineBreak;
196 RenderStatsLine(parent->Res().gutLabel, stats.Gut(), screen, position);
198 position += lineBreak;
199 RenderStatsLine(parent->Res().mgrLabel, stats.MagicResistance(), screen, position);
202 void EquipMenu::RenderStatsLine(const char *label, int number, SDL_Surface *screen, const Vector<int> &position) const {
203 const Font &font(*parent->Res().statusFont);
204 const Vector<int> numberOffset(4 * font.CharWidth(), 0);
206 font.DrawString(label, screen, position, 3);
207 font.DrawNumber(number, screen, position + numberOffset, 3);
210 void EquipMenu::RenderEquipmentMenu(SDL_Surface *screen, const Vector<int> &offset) const {
211 equipmentMenu.Draw(screen, offset);
214 void EquipMenu::RenderActionMenu(SDL_Surface *screen, const Vector<int> &offset) const {
215 const Font &font(*parent->Res().statusFont);
216 const Frame &frame(*parent->Res().statusFrame);
217 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 2);
219 frame.Draw(screen, offset, 15 * font.CharWidth(), 10 * font.CharHeight());
220 actionMenu.Draw(screen, offset + menuOffset);
223 void EquipMenu::RenderInventoryMenu(SDL_Surface *screen, const Vector<int> &offset) const {
228 void EquipMenu::NextHero() {
229 cursor = (cursor + 1) % parent->Game().state->partySize;
233 void EquipMenu::PreviousHero() {
234 cursor = (cursor + parent->Game().state->partySize - 1) % parent->Game().state->partySize;
238 Hero &EquipMenu::GetHero() {
239 return *parent->Game().state->party[cursor];
242 const Hero &EquipMenu::GetHero() const {
243 return *parent->Game().state->party[cursor];
247 void EquipMenu::LoadEquipment() {
248 equipmentMenu.Clear();
249 if (GetHero().HasWeapon()) {
250 equipmentMenu.Add(GetHero().Weapon()->Name(), GetHero().Weapon(), true, GetHero().Weapon()->MenuIcon());
252 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
254 if (GetHero().HasArmor()) {
255 equipmentMenu.Add(GetHero().Armor()->Name(), GetHero().Armor(), true, GetHero().Armor()->MenuIcon());
257 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
259 if (GetHero().HasShield()) {
260 equipmentMenu.Add(GetHero().Shield()->Name(), GetHero().Shield(), true, GetHero().Shield()->MenuIcon());
262 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
264 if (GetHero().HasHelmet()) {
265 equipmentMenu.Add(GetHero().Helmet()->Name(), GetHero().Helmet(), true, GetHero().Helmet()->MenuIcon());
267 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
269 if (GetHero().HasRing()) {
270 equipmentMenu.Add(GetHero().Ring()->Name(), GetHero().Ring(), true, GetHero().Ring()->MenuIcon());
272 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
274 if (GetHero().HasJewel()) {
275 equipmentMenu.Add(GetHero().Jewel()->Name(), GetHero().Jewel(), true, GetHero().Jewel()->MenuIcon());
277 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
281 void EquipMenu::RemoveAllEquipment() {
282 Inventory &inv(parent->Game().state->inventory);
283 if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
284 GetHero().RemoveWeapon();
286 if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
287 GetHero().RemoveArmor();
289 if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
290 GetHero().RemoveShield();
292 if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
293 GetHero().RemoveHelmet();
295 if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
296 GetHero().RemoveRing();
298 if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
299 GetHero().RemoveJewel();
304 void EquipMenu::RemoveItem() {
305 Inventory &inv(parent->Game().state->inventory);
306 switch (equipmentMenu.SelectedIndex()) {
308 if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
309 GetHero().RemoveWeapon();
313 if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
314 GetHero().RemoveArmor();
318 if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
319 GetHero().RemoveShield();
323 if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
324 GetHero().RemoveHelmet();
328 if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
329 GetHero().RemoveRing();
333 if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
334 GetHero().RemoveJewel();
341 void EquipMenu::DropItem() {
342 switch (equipmentMenu.SelectedIndex()) {
344 GetHero().RemoveWeapon();
347 GetHero().RemoveArmor();
350 GetHero().RemoveShield();
353 GetHero().RemoveHelmet();
356 GetHero().RemoveRing();
359 GetHero().RemoveJewel();