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"
15 using common::Capsule;
16 using common::Inventory;
20 using graphics::Frame;
24 CapsuleFeedMenu::CapsuleFeedMenu(CapsuleMenu *parent)
26 , menu(*parent->Res().capsuleFeedMenuProperties)
27 , itemMenu(*parent->Res().inventoryMenuProperties) {
28 menu.Add(parent->Res().itemMenuSelectText, CHOICE_SELECT);
29 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
34 void CapsuleFeedMenu::OnEnterState(SDL_Surface *) {
39 void CapsuleFeedMenu::LoadInventory() {
40 const Inventory &inv(parent->Game().state->inventory);
42 itemMenu.Reserve(inv.MaxItems());
43 for (int i(0); i < inv.MaxItems(); ++i) {
44 const Item *item(inv.ItemAt(i));
46 // TODO: find out which items are impossible to feed to a capsule
47 itemMenu.Add(item->Name(), item, true, item->MenuIcon(), inv.ItemCountAt(i));
49 itemMenu.AddEmptyEntry();
54 void CapsuleFeedMenu::OnExitState(SDL_Surface *) {
58 void CapsuleFeedMenu::OnResumeState(SDL_Surface *) {
62 void CapsuleFeedMenu::OnPauseState(SDL_Surface *) {
67 void CapsuleFeedMenu::OnResize(int width, int height) {
72 void CapsuleFeedMenu::HandleEvents(const Input &input) {
73 if (menu.IsActive()) {
74 if (input.JustPressed(Input::PAD_LEFT)) {
77 if (input.JustPressed(Input::PAD_RIGHT)) {
81 if (input.JustPressed(Input::PAD_UP)) {
82 itemMenu.PreviousItem();
84 if (input.JustPressed(Input::PAD_DOWN)) {
89 if (input.JustPressed(Input::ACTION_A)) {
90 if (menu.IsActive()) {
91 if (menu.Selected() == CHOICE_SORT) {
92 parent->Game().state->inventory.Sort();
98 } else if (itemMenu.IsActive()) {
99 itemMenu.SetDualSelection();
100 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
101 switch (menu.Selected()) {
104 itemMenu.SetActive();
107 // invalid state, recover
109 itemMenu.SetInactive();
113 parent->Game().state->inventory.SwapEntriesAt(
114 itemMenu.SelectedIndex(),
115 itemMenu.SecondaryIndex());
116 itemMenu.SwapSelected();
117 itemMenu.SetActive();
120 if (input.JustPressed(Input::ACTION_B)) {
121 if (menu.IsActive()) {
123 } else if (itemMenu.IsActive()) {
125 itemMenu.SetInactive();
127 itemMenu.SetActive();
132 void CapsuleFeedMenu::FeedSelected() {
133 if (itemMenu.Selected()) {
134 // TODO: feed and grow animations
135 if (GetCapsule().IsHungry()) {
136 GetCapsule().Feed(itemMenu.Selected());
137 parent->Game().state->inventory.Remove(itemMenu.Selected(), 1);
139 } else if (itemMenu.Selected() == GetCapsule().UpgradeItem()) {
140 GetCapsule().UpgradeSpecial();
141 parent->Game().state->inventory.Remove(itemMenu.Selected(), 1);
152 void CapsuleFeedMenu::UpdateWorld(Uint32 deltaT) {
156 void CapsuleFeedMenu::Render(SDL_Surface *screen) {
157 const Font &font(*parent->Res().statusFont);
158 const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
159 const Vector<int> nameOffset(
161 2 * font.CharHeight() - font.CharHeight() / 8);
162 const Vector<int> spriteOffset(
163 3 * font.CharWidth() + font.CharWidth() * 3 / 4,
164 4 * font.CharHeight() + font.CharHeight() / 4);
165 const Vector<int> growthOffset(
166 13 * font.CharWidth(),
167 7 * font.CharHeight() + font.CharHeight() / 4);
168 const Vector<int> hungerOffset(
169 13 * font.CharWidth(),
170 9 * font.CharHeight() + font.CharHeight() / 8);
171 const Vector<int> menuOffset(
173 13 * font.CharHeight() + font.CharHeight() / 8);
174 const Vector<int> itemsOffset(
176 16 * font.CharHeight() + font.CharHeight() / 8);
178 parent->RenderBackground(screen);
179 RenderName(screen, offset + nameOffset);
180 RenderSprite(screen, offset + spriteOffset);
181 if (GetCapsule().IsHungry()) {
182 RenderGrowth(screen, offset + growthOffset);
184 RenderHunger(screen, offset + hungerOffset);
185 RenderMenu(screen, offset + menuOffset);
186 RenderItems(screen, offset + itemsOffset);
189 void CapsuleFeedMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
190 const Font &font(*parent->Res().statusFont);
191 const Vector<int> separatorOffset(5 * font.CharWidth(), 0);
192 const Vector<int> nameOffset(6 * font.CharWidth(), 0);
194 font.DrawString(parent->Res().capsuleNameLabel, screen, offset, 5);
195 font.DrawChar(':', screen, offset + separatorOffset);
196 font.DrawString(GetCapsule().Name(), screen, offset + nameOffset);
199 void CapsuleFeedMenu::RenderSprite(SDL_Surface *screen, const Vector<int> &offset) const {
200 // TODO: sitting ground
201 GetCapsule().BattleSprite()->Draw(screen, offset);
204 void CapsuleFeedMenu::RenderGrowth(SDL_Surface *screen, const Vector<int> &offset) const {
205 Vector<int> cursor(offset);
206 parent->Res().capsuleGrowthLabel->Draw(screen, offset);
207 cursor.X() += parent->Res().capsuleGrowthLabel->Width();
209 for (int i = 0; i < GetCapsule().HungerFull(); ++i) {
210 parent->Res().capsuleGrowthBarFilled->Draw(screen, cursor);
211 cursor.X() += parent->Res().capsuleGrowthBarFilled->Width();
214 for (int i = 0; i < GetCapsule().HungerEmpty(); ++i) {
215 parent->Res().capsuleGrowthBar->Draw(screen, cursor);
216 cursor.X() += parent->Res().capsuleGrowthBar->Width();
220 void CapsuleFeedMenu::RenderHunger(SDL_Surface *screen, const Vector<int> &offset) const {
221 const Font &font(*parent->Res().normalFont);
222 const Frame &frame(*parent->Res().statusFrame);
223 const Vector<int> textOffset(2 * font.CharWidth(), font.CharHeight());
225 frame.Draw(screen, offset, 18 * font.CharWidth(), 3 * font.CharHeight());
226 font.DrawString(parent->Res().capsuleNotHungryText, screen, offset + textOffset, 15);
229 void CapsuleFeedMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
230 const Font &font(*parent->Res().normalFont);
231 const Frame &frame(*parent->Res().statusFrame);
232 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
233 const Vector<int> menubgOffset(8 * font.CharWidth(), 0);
234 const Vector<int> menuOffset(11 * font.CharWidth(), font.CharHeight());
236 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
237 font.DrawString(parent->Res().capsuleFeedLabel, screen, offset + labelOffset);
238 frame.Draw(screen, offset + menubgOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
239 menu.Draw(screen, offset + menuOffset);
242 void CapsuleFeedMenu::RenderItems(SDL_Surface *screen, const Vector<int> &offset) const {
243 const Font &font(*parent->Res().normalFont);
244 const Frame &frame(*parent->Res().statusFrame);
245 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() * 5 / 4);
247 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
248 itemMenu.Draw(screen, offset + menuOffset);
252 int CapsuleFeedMenu::Width() const {
253 return parent->Width();
256 int CapsuleFeedMenu::Height() const {
257 return parent->Height();
260 Capsule &CapsuleFeedMenu::GetCapsule() {
261 return parent->GetCapsule();
264 const Capsule &CapsuleFeedMenu::GetCapsule() const {
265 return parent->GetCapsule();