1 #include "CapsuleFeedMenu.h"
3 #include "CapsuleMenu.h"
5 #include "../app/Application.h"
6 #include "../app/Input.h"
7 #include "../common/Inventory.h"
8 #include "../common/Item.h"
9 #include "../common/GameConfig.h"
10 #include "../common/GameState.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
13 #include "../math/Vector.h"
16 using common::Capsule;
17 using common::Inventory;
21 using graphics::Frame;
25 CapsuleFeedMenu::CapsuleFeedMenu(CapsuleMenu *parent)
27 , menu(*parent->Res().capsuleFeedMenuProperties)
28 , itemMenu(*parent->Res().inventoryMenuProperties) {
29 menu.Add(parent->Res().itemMenuSelectText, CHOICE_SELECT);
30 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
35 void CapsuleFeedMenu::OnEnterState(SDL_Surface *) {
40 void CapsuleFeedMenu::LoadInventory() {
41 const Inventory &inv(parent->Game().state->inventory);
43 itemMenu.Reserve(inv.MaxItems());
44 for (int i(0); i < inv.MaxItems(); ++i) {
45 const Item *item(inv.ItemAt(i));
47 // TODO: find out which items are impossible to feed to a capsule
48 itemMenu.Add(item->Name(), item, true, item->MenuIcon(), inv.ItemCountAt(i));
50 itemMenu.AddEmptyEntry();
55 void CapsuleFeedMenu::OnExitState(SDL_Surface *) {
59 void CapsuleFeedMenu::OnResumeState(SDL_Surface *) {
63 void CapsuleFeedMenu::OnPauseState(SDL_Surface *) {
68 void CapsuleFeedMenu::OnResize(int width, int height) {
73 void CapsuleFeedMenu::HandleEvents(const Input &input) {
74 if (menu.IsActive()) {
75 if (input.JustPressed(Input::PAD_LEFT)) {
78 if (input.JustPressed(Input::PAD_RIGHT)) {
82 if (input.JustPressed(Input::PAD_UP)) {
83 itemMenu.PreviousItem();
85 if (input.JustPressed(Input::PAD_DOWN)) {
90 if (input.JustPressed(Input::ACTION_A)) {
91 if (menu.IsActive()) {
92 if (menu.Selected() == CHOICE_SORT) {
93 parent->Game().state->inventory.Sort();
99 } else if (itemMenu.IsActive()) {
100 itemMenu.SetDualSelection();
101 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
102 switch (menu.Selected()) {
105 itemMenu.SetActive();
108 // invalid state, recover
110 itemMenu.SetInactive();
114 parent->Game().state->inventory.SwapEntriesAt(
115 itemMenu.SelectedIndex(),
116 itemMenu.SecondaryIndex());
117 itemMenu.SwapSelected();
118 itemMenu.SetActive();
121 if (input.JustPressed(Input::ACTION_B)) {
122 if (menu.IsActive()) {
124 } else if (itemMenu.IsActive()) {
126 itemMenu.SetInactive();
128 itemMenu.SetActive();
133 void CapsuleFeedMenu::FeedSelected() {
134 if (itemMenu.Selected()) {
135 // TODO: feed and grow animations
136 if (GetCapsule().IsHungry()) {
137 GetCapsule().Feed(itemMenu.Selected());
138 parent->Game().state->inventory.Remove(itemMenu.Selected(), 1);
140 } else if (itemMenu.Selected() == GetCapsule().UpgradeItem()) {
141 GetCapsule().UpgradeSpecial();
142 parent->Game().state->inventory.Remove(itemMenu.Selected(), 1);
153 void CapsuleFeedMenu::UpdateWorld(Uint32 deltaT) {
157 void CapsuleFeedMenu::Render(SDL_Surface *screen) {
158 const Font &font(*parent->Res().statusFont);
159 const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
160 const Vector<int> nameOffset(
162 2 * font.CharHeight() - font.CharHeight() / 8);
163 const Vector<int> spriteOffset(
164 3 * font.CharWidth() + font.CharWidth() * 3 / 4,
165 4 * font.CharHeight() + font.CharHeight() / 4);
166 const Vector<int> growthOffset(
167 13 * font.CharWidth(),
168 7 * font.CharHeight() + font.CharHeight() / 4);
169 const Vector<int> hungerOffset(
170 13 * font.CharWidth(),
171 9 * font.CharHeight() + font.CharHeight() / 8);
172 const Vector<int> menuOffset(
174 13 * font.CharHeight() + font.CharHeight() / 8);
175 const Vector<int> itemsOffset(
177 16 * font.CharHeight() + font.CharHeight() / 8);
179 parent->RenderBackground(screen);
180 RenderName(screen, offset + nameOffset);
181 RenderSprite(screen, offset + spriteOffset);
182 if (GetCapsule().IsHungry()) {
183 RenderGrowth(screen, offset + growthOffset);
185 RenderHunger(screen, offset + hungerOffset);
186 RenderMenu(screen, offset + menuOffset);
187 RenderItems(screen, offset + itemsOffset);
190 void CapsuleFeedMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
191 const Font &font(*parent->Res().statusFont);
192 const Vector<int> separatorOffset(5 * font.CharWidth(), 0);
193 const Vector<int> nameOffset(6 * font.CharWidth(), 0);
195 font.DrawString(parent->Res().capsuleNameLabel, screen, offset, 5);
196 font.DrawChar(':', screen, offset + separatorOffset);
197 font.DrawString(GetCapsule().Name(), screen, offset + nameOffset);
200 void CapsuleFeedMenu::RenderSprite(SDL_Surface *screen, const Vector<int> &offset) const {
201 // TODO: sitting ground
202 GetCapsule().BattleSprite()->Draw(screen, offset);
205 void CapsuleFeedMenu::RenderGrowth(SDL_Surface *screen, const Vector<int> &offset) const {
206 Vector<int> cursor(offset);
207 parent->Res().capsuleGrowthLabel->Draw(screen, offset);
208 cursor.X() += parent->Res().capsuleGrowthLabel->Width();
210 for (int i = 0; i < GetCapsule().HungerFull(); ++i) {
211 parent->Res().capsuleGrowthBarFilled->Draw(screen, cursor);
212 cursor.X() += parent->Res().capsuleGrowthBarFilled->Width();
215 for (int i = 0; i < GetCapsule().HungerEmpty(); ++i) {
216 parent->Res().capsuleGrowthBar->Draw(screen, cursor);
217 cursor.X() += parent->Res().capsuleGrowthBar->Width();
221 void CapsuleFeedMenu::RenderHunger(SDL_Surface *screen, const Vector<int> &offset) const {
222 const Font &font(*parent->Res().normalFont);
223 const Frame &frame(*parent->Res().statusFrame);
224 const Vector<int> textOffset(2 * font.CharWidth(), font.CharHeight());
226 frame.Draw(screen, offset, 18 * font.CharWidth(), 3 * font.CharHeight());
227 font.DrawString(parent->Res().capsuleNotHungryText, screen, offset + textOffset, 15);
230 void CapsuleFeedMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
231 const Font &font(*parent->Res().normalFont);
232 const Frame &frame(*parent->Res().statusFrame);
233 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
234 const Vector<int> menubgOffset(8 * font.CharWidth(), 0);
235 const Vector<int> menuOffset(11 * font.CharWidth(), font.CharHeight());
237 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
238 font.DrawString(parent->Res().capsuleFeedLabel, screen, offset + labelOffset);
239 frame.Draw(screen, offset + menubgOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
240 menu.Draw(screen, offset + menuOffset);
243 void CapsuleFeedMenu::RenderItems(SDL_Surface *screen, const Vector<int> &offset) const {
244 const Font &font(*parent->Res().normalFont);
245 const Frame &frame(*parent->Res().statusFrame);
246 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() * 5 / 4);
248 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
249 itemMenu.Draw(screen, offset + menuOffset);
253 int CapsuleFeedMenu::Width() const {
254 return parent->Width();
257 int CapsuleFeedMenu::Height() const {
258 return parent->Height();
261 Capsule &CapsuleFeedMenu::GetCapsule() {
262 return parent->GetCapsule();
265 const Capsule &CapsuleFeedMenu::GetCapsule() const {
266 return parent->GetCapsule();