From 628b3a7276d0b330719e05504b23bafcf88f8fca Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 7 Aug 2012 12:23:10 +0200 Subject: [PATCH] added battle move menu --- Debug/src/battle/subdir.mk | 3 +++ Release/src/battle/subdir.mk | 3 +++ src/battle/AttackTypeMenu.cpp | 16 ------------ src/battle/AttackTypeMenu.h | 4 +-- src/battle/BattleState.cpp | 45 ++++++++++++++++++++++++++++--- src/battle/BattleState.h | 12 +++++++-- src/battle/Hero.h | 16 ++++++++++-- src/battle/HeroTag.cpp | 15 +++++++++-- src/battle/HeroTag.h | 2 +- src/battle/Monster.h | 4 +-- src/battle/MoveMenu.cpp | 29 ++++++++++++++++++++ src/battle/MoveMenu.h | 48 ++++++++++++++++++++++++++++++++++ src/main.cpp | 11 +++++++- test-data/move-icons.png | Bin 0 -> 2795 bytes 14 files changed, 176 insertions(+), 32 deletions(-) create mode 100644 src/battle/MoveMenu.cpp create mode 100644 src/battle/MoveMenu.h create mode 100644 test-data/move-icons.png diff --git a/Debug/src/battle/subdir.mk b/Debug/src/battle/subdir.mk index 0ef38a1..d967ca2 100644 --- a/Debug/src/battle/subdir.mk +++ b/Debug/src/battle/subdir.mk @@ -9,6 +9,7 @@ CPP_SRCS += \ ../src/battle/Hero.cpp \ ../src/battle/HeroTag.cpp \ ../src/battle/Monster.cpp \ +../src/battle/MoveMenu.cpp \ ../src/battle/PartyLayout.cpp OBJS += \ @@ -17,6 +18,7 @@ OBJS += \ ./src/battle/Hero.o \ ./src/battle/HeroTag.o \ ./src/battle/Monster.o \ +./src/battle/MoveMenu.o \ ./src/battle/PartyLayout.o CPP_DEPS += \ @@ -25,6 +27,7 @@ CPP_DEPS += \ ./src/battle/Hero.d \ ./src/battle/HeroTag.d \ ./src/battle/Monster.d \ +./src/battle/MoveMenu.d \ ./src/battle/PartyLayout.d diff --git a/Release/src/battle/subdir.mk b/Release/src/battle/subdir.mk index e59a859..2ae5c59 100644 --- a/Release/src/battle/subdir.mk +++ b/Release/src/battle/subdir.mk @@ -9,6 +9,7 @@ CPP_SRCS += \ ../src/battle/Hero.cpp \ ../src/battle/HeroTag.cpp \ ../src/battle/Monster.cpp \ +../src/battle/MoveMenu.cpp \ ../src/battle/PartyLayout.cpp OBJS += \ @@ -17,6 +18,7 @@ OBJS += \ ./src/battle/Hero.o \ ./src/battle/HeroTag.o \ ./src/battle/Monster.o \ +./src/battle/MoveMenu.o \ ./src/battle/PartyLayout.o CPP_DEPS += \ @@ -25,6 +27,7 @@ CPP_DEPS += \ ./src/battle/Hero.d \ ./src/battle/HeroTag.d \ ./src/battle/Monster.d \ +./src/battle/MoveMenu.d \ ./src/battle/PartyLayout.d diff --git a/src/battle/AttackTypeMenu.cpp b/src/battle/AttackTypeMenu.cpp index b881d8b..bee794f 100644 --- a/src/battle/AttackTypeMenu.cpp +++ b/src/battle/AttackTypeMenu.cpp @@ -7,31 +7,15 @@ #include "AttackTypeMenu.h" -#include "../app/Input.h" #include "../geometry/operators.h" #include "../geometry/Vector.h" #include "../graphics/Sprite.h" -using app::Input; using geometry::Point; using geometry::Vector; namespace battle { -void AttackTypeMenu::ReadInput(const Input &input) { - if (input.IsDown(Input::PAD_UP)) { - selected = MAGIC; - } else if (input.IsDown(Input::PAD_RIGHT)) { - selected = DEFEND; - } else if (input.IsDown(Input::PAD_DOWN)) { - selected = IKARI; - } else if (input.IsDown(Input::PAD_LEFT)) { - selected = ITEM; - } else { - selected = SWORD; - } -} - void AttackTypeMenu::Render(SDL_Surface *screen, const geometry::Point &position) { Vector swordOffset(IconWidth(), IconHeight()); Vector magicOffset(IconWidth(), 0); diff --git a/src/battle/AttackTypeMenu.h b/src/battle/AttackTypeMenu.h index 08caf7f..26a9dd9 100644 --- a/src/battle/AttackTypeMenu.h +++ b/src/battle/AttackTypeMenu.h @@ -8,8 +8,6 @@ #ifndef BATTLE_ATTACKTYPEMENU_H_ #define BATTLE_ATTACKTYPEMENU_H_ -namespace app { class Input; } - #include "../geometry/Point.h" #include "../graphics/Sprite.h" @@ -33,7 +31,7 @@ public: : icons(icons), selected(SWORD) { } public: - void ReadInput(const app::Input &); + void Select(Icon i) { selected = i; } Icon Selected() const { return selected; } void Render(SDL_Surface *screen, const geometry::Point &position); diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 758d9d8..a2c306a 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -58,7 +58,35 @@ void BattleState::ExitState() { void BattleState::HandleInput(const Input &input) { - attackTypeMenu.ReadInput(input); + if (moveChoice == -1) { + if (input.IsDown(Input::PAD_UP)) { + moveMenu.Select(MoveMenu::CHANGE); + } else if (input.IsDown(Input::PAD_DOWN)) { + moveMenu.Select(MoveMenu::RUN); + } else { + moveMenu.Select(MoveMenu::ATTACK); + } + + if (input.JustPressed(Input::ACTION_A)) { + moveChoice = moveMenu.Selected(); + } + } else { + if (input.IsDown(Input::PAD_UP)) { + attackTypeMenu.Select(AttackTypeMenu::MAGIC); + } else if (input.IsDown(Input::PAD_RIGHT)) { + attackTypeMenu.Select(AttackTypeMenu::DEFEND); + } else if (input.IsDown(Input::PAD_DOWN)) { + attackTypeMenu.Select(AttackTypeMenu::IKARI); + } else if (input.IsDown(Input::PAD_LEFT)) { + attackTypeMenu.Select(AttackTypeMenu::ITEM); + } else { + attackTypeMenu.Select(AttackTypeMenu::SWORD); + } + + if (input.JustPressed(Input::ACTION_A)) { + activeHero = (activeHero + 1) % 4; + } + } } void BattleState::UpdateWorld(float deltaT) { @@ -74,7 +102,11 @@ void BattleState::Render(SDL_Surface *screen) { RenderMonsters(screen, offset); // RenderHeroes(screen, offset); RenderHeroTags(screen, offset); - RenderAttackTypeMenu(screen, offset); + if (moveChoice == -1) { + RenderMoveMenu(screen, offset); + } else { + RenderAttackTypeMenu(screen, offset); + } } void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { @@ -112,7 +144,7 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) tagPosition[3] = Point(tagWidth + attackTypeMenu.IconWidth(), uiOffset + tagHeight + attackTypeMenu.IconHeight()); for (vector::size_type i(0), end(heroTags.size()); i < end; ++i) { - heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset); + heroTags[i].Render(screen, tagWidth, tagHeight, tagPosition[i] + offset, i == activeHero); } } @@ -123,4 +155,11 @@ void BattleState::RenderAttackTypeMenu(SDL_Surface *screen, const Vector &o attackTypeMenu.Render(screen, position + offset); } +void BattleState::RenderMoveMenu(SDL_Surface *screen, const Vector &offset) { + Point position( + (background->w - moveMenu.Width()) / 2, + (background->h * 3 / 4) - (moveMenu.Height() / 2)); + moveMenu.Render(screen, position + offset); +} + } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 8fa7d95..daf1cae 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -12,6 +12,7 @@ #include "Hero.h" #include "HeroTag.h" #include "Monster.h" +#include "MoveMenu.h" #include "../app/State.h" #include "../geometry/Point.h" #include "../geometry/Vector.h" @@ -31,11 +32,14 @@ class BattleState : public app::State { public: - BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const graphics::Sprite *attackIcons) + BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const graphics::Sprite *attackIcons, const graphics::Sprite *moveIcons) : background(background) , monstersLayout(&monstersLayout) , heroesLayout(&heroesLayout) - , attackTypeMenu(attackIcons) { } + , attackTypeMenu(attackIcons) + , moveMenu(moveIcons) + , moveChoice(-1) + , activeHero(0) { } public: void AddMonster(const Monster &); @@ -57,17 +61,21 @@ private: void RenderHeroes(SDL_Surface *screen, const geometry::Vector &offset); void RenderHeroTags(SDL_Surface *screen, const geometry::Vector &offset); void RenderAttackTypeMenu(SDL_Surface *screen, const geometry::Vector &offset); + void RenderMoveMenu(SDL_Surface *screen, const geometry::Vector &offset); private: SDL_Surface *background; const PartyLayout *monstersLayout; const PartyLayout *heroesLayout; AttackTypeMenu attackTypeMenu; + MoveMenu moveMenu; std::vector > monsterPositions; std::vector > heroesPositions; std::vector monsters; std::vector heroes; std::vector heroTags; + int moveChoice; + int activeHero; }; diff --git a/src/battle/Hero.h b/src/battle/Hero.h index ec87f49..59512b6 100644 --- a/src/battle/Hero.h +++ b/src/battle/Hero.h @@ -27,11 +27,14 @@ public: Uint16 MaxHealth() const { return maxHealth; } Uint16 Health() const { return health; } - int RelativeHealth(int max) { return health * max / maxHealth; } + int RelativeHealth(int max) const { return health * max / maxHealth; } Uint16 MaxMana() const { return maxMana; } Uint16 Mana() const { return mana; } - int RelativeMana(int max) { return mana * max / maxMana; } + int RelativeMana(int max) const { return mana * max / maxMana; } + + Uint8 IP() const { return ip; } + int RelativeIP(int max) const { return ip * max / 256; } Uint16 Attack() const { return attack; } Uint16 Defense() const { return defense; } @@ -42,8 +45,16 @@ public: // temporary setters until loader is implemented public: + void SetName(const char *n) { name = n; } + void SetLevel(Uint8 l) { level = l; } void SetSprite(graphics::Sprite *s) { sprite = s; } + void SetMaxHealth(Uint16 h) { maxHealth = h; } + void SetHealth(Uint16 h) { health = h; } + void SetMaxMana(Uint16 m) { maxMana = m; } + void SetMana(Uint16 m) { mana = m; } + void SetIP(Uint8 i) { ip = i; } + private: const char *name; graphics::Sprite *sprite; @@ -60,6 +71,7 @@ private: Uint16 magicResistance; Uint8 level; + Uint8 ip; }; diff --git a/src/battle/HeroTag.cpp b/src/battle/HeroTag.cpp index 2461e4d..d67368f 100644 --- a/src/battle/HeroTag.cpp +++ b/src/battle/HeroTag.cpp @@ -7,11 +7,17 @@ #include "HeroTag.h" +#include "Hero.h" +#include "../geometry/operators.h" +#include "../geometry/Vector.h" +#include "../graphics/Sprite.h" + using geometry::Point; +using geometry::Vector; namespace battle { -void HeroTag::Render(SDL_Surface *screen, int width, int height, Point position) const { +void HeroTag::Render(SDL_Surface *screen, int width, int height, Point position, bool active) const { SDL_Rect destRect; destRect.x = position.X(); destRect.y = position.Y(); @@ -22,13 +28,18 @@ void HeroTag::Render(SDL_Surface *screen, int width, int height, Point posi destRect.y += 1; destRect.w -= 2; destRect.h -= 2; - SDL_FillRect(screen, &destRect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); + SDL_FillRect(screen, &destRect, SDL_MapRGB(screen->format, 0xFF, active ? 0 : 0xFF, active ? 0 : 0xFF)); destRect.x += 1; destRect.y += 1; destRect.w -= 2; destRect.h -= 2; SDL_FillRect(screen, &destRect, SDL_MapRGB(screen->format, 0, 0, 0)); + + Vector heroOffset( + (align == LEFT) ? 3 : width - hero->Sprite()->Width() - 3, + height - hero->Sprite()->Height() - 3); + hero->Sprite()->Draw(screen, position + heroOffset); } } diff --git a/src/battle/HeroTag.h b/src/battle/HeroTag.h index e0d3a5a..c606348 100644 --- a/src/battle/HeroTag.h +++ b/src/battle/HeroTag.h @@ -29,7 +29,7 @@ public: ~HeroTag() { } public: - void Render(SDL_Surface *screen, int width, int height, geometry::Point position) const; + void Render(SDL_Surface *screen, int width, int height, geometry::Point position, bool active) const; private: const Hero *hero; diff --git a/src/battle/Monster.h b/src/battle/Monster.h index e77a14b..1c92184 100644 --- a/src/battle/Monster.h +++ b/src/battle/Monster.h @@ -27,11 +27,11 @@ public: Uint16 MaxHealth() const { return maxHealth; } Uint16 Health() const { return health; } - int RelativeHealth(int max) { return health * max / maxHealth; } + int RelativeHealth(int max) const { return health * max / maxHealth; } Uint16 MaxMana() const { return maxMana; } Uint16 Mana() const { return mana; } - int RelativeMana(int max) { return mana * max / maxMana; } + int RelativeMana(int max) const { return mana * max / maxMana; } Uint16 Attack() const { return attack; } Uint16 Defense() const { return defense; } diff --git a/src/battle/MoveMenu.cpp b/src/battle/MoveMenu.cpp new file mode 100644 index 0000000..4d5ff79 --- /dev/null +++ b/src/battle/MoveMenu.cpp @@ -0,0 +1,29 @@ +/* + * MoveMenu.cpp + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#include "MoveMenu.h" + +#include "../geometry/operators.h" +#include "../geometry/Vector.h" +#include "../graphics/Sprite.h" + +using geometry::Point; +using geometry::Vector; + +namespace battle { + +void MoveMenu::Render(SDL_Surface *screen, const geometry::Point &position) { + Vector attackOffset(0, IconHeight()); + Vector changeOffset(0, 0); + Vector runOffset(0, 2 * IconHeight()); + + icons->Draw(screen, position + attackOffset, ATTACK, (selected == ATTACK) ? 1 : 0); + icons->Draw(screen, position + changeOffset, CHANGE, (selected == CHANGE) ? 1 : 0); + icons->Draw(screen, position + runOffset, RUN, (selected == RUN) ? 1 : 0); +} + +} diff --git a/src/battle/MoveMenu.h b/src/battle/MoveMenu.h new file mode 100644 index 0000000..771fcb8 --- /dev/null +++ b/src/battle/MoveMenu.h @@ -0,0 +1,48 @@ +/* + * MoveMenu.h + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#ifndef BATTLE_MOVEMENU_H_ +#define BATTLE_MOVEMENU_H_ + +#include "../geometry/Point.h" +#include "../graphics/Sprite.h" + +namespace battle { + +class MoveMenu { + +public: + enum Icon { + ATTACK, + CHANGE, + RUN + }; + +public: + explicit MoveMenu(const graphics::Sprite *icons) + : icons(icons), selected(ATTACK) { } + ~MoveMenu() { } + +public: + void Select(Icon i) { selected = i; } + Icon Selected() const { return selected; } + void Render(SDL_Surface *screen, const geometry::Point &position); + + int Width() const { return IconWidth(); } + int Height() const { return 3 * IconHeight(); } + int IconWidth() const { return icons->Width(); } + int IconHeight() const { return icons->Height(); } + +private: + const graphics::Sprite *icons; + Icon selected; + +}; + +} + +#endif /* BATTLE_MOVEMENU_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 4ea0e0e..bd8ff4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,12 +73,21 @@ int main(int argc, char **argv) { Monster monster; monster.SetSprite(&dummySprite); Hero hero; + hero.SetName("Name"); + hero.SetLevel(34); hero.SetSprite(&dummySprite); + hero.SetMaxHealth(100); + hero.SetHealth(50); + hero.SetMaxMana(100); + hero.SetMana(66); + hero.SetIP(160); SDL_Surface *attackIcons(IMG_Load("test-data/attack-type-icons.png")); Sprite attackIconsSprite(attackIcons, 32, 32); + SDL_Surface *moveIcons(IMG_Load("test-data/move-icons.png")); + Sprite moveIconsSprite(moveIcons, 32, 32); - BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite)); + BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &attackIconsSprite, &moveIconsSprite)); battleState->AddMonster(monster); battleState->AddMonster(monster); battleState->AddMonster(monster); diff --git a/test-data/move-icons.png b/test-data/move-icons.png new file mode 100644 index 0000000000000000000000000000000000000000..93a6b10b739ecc6f54906cde619d615f35902cce GIT binary patch literal 2795 zcmVqi$%B)%*FG$#OI7**qiX^atT)~vDH?I?==*%*`tfb#P4o<4n=mzS5W4S?T*qNv4U!EyW#0t7gY0|1D- zKPxLsr_)teR}%y=F)kWtZ*LCpwi^XCg2qH3IN7avE*yG2KSFc`;AP9=07=~Fa7I9%j zjEi9yR#H;3e*OB58#n&=@k7K@i;9W@fdI?0aef;BKz)6^$KyG1;>5mv`&gD0ZtN_} zdcEGypFhLDMMqeXE*2+e33oA*$wZQ5eDkr{Y$%F~OY<=Osnu$gN~Kn-=g!4)a&oR- zy}Dt;27#O~&ISmAP^nb$F-oNtgC43@t8F&h*|TSb+YtaDBO^l$K*^$$a0AI2e{pef zadEM59t0nOF@7n7={nbhYug_>FF657~n6?F@xW_ zbt{4(gM))fJ;Lwz2Lge^hYyd9jlt{P-QBUCo{^EkFboWN32L!LixyqFbO}}uSy@?H ztrkI$j*boylqX&C<;$0^U%!gDF%Sss-@hM*v*pW|*Vos_GAQ`lhr<_>Nn;qMR4Uur z+HT#t#c>=>(<0DG$fT1J9LKAxtDBpf`3(rsPm-ieCL0|c4F-c|vsosSRaI4aJRXO` z5lLTu{rVM-PU0A}bm`KUFJId2_T1duS#4+u0TBRj_Uu`iOeS1E0DvF}wOS3?5HunT z!zhZHkskp72M!#_%*;%#VKzBAX|-Bkzkc1;))rZ;r!FwpXfzt5(U>k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