From b7a90738b9ae701cfc86bf74a11ba59d7fcb17ba Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 10 Aug 2012 12:46:41 +0200 Subject: [PATCH] added item target selection --- src/battle/AttackChoice.cpp | 6 +++++ src/battle/AttackChoice.h | 11 ++++++-- src/battle/BattleState.h | 2 ++ src/battle/states/SelectAttackType.cpp | 5 ++-- src/battle/states/SelectItem.cpp | 35 +++++++++++++++++++++---- src/battle/states/SelectTarget.h | 2 +- src/common/Item.h | 3 ++- src/main.cpp | 14 +++++++++- test-data/targeting-icons.png | Bin 414 -> 744 bytes 9 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/battle/AttackChoice.cpp b/src/battle/AttackChoice.cpp index c966b53..5d3359b 100644 --- a/src/battle/AttackChoice.cpp +++ b/src/battle/AttackChoice.cpp @@ -9,4 +9,10 @@ namespace battle { +void AttackChoice::Reset() { + thing = 0; + selection.Reset(); + type = UNDECIDED; +} + } diff --git a/src/battle/AttackChoice.h b/src/battle/AttackChoice.h index 0068e6f..42eb0d3 100644 --- a/src/battle/AttackChoice.h +++ b/src/battle/AttackChoice.h @@ -10,6 +10,8 @@ #include "TargetSelection.h" +namespace common { class Item; } + namespace battle { class AttackChoice { @@ -25,18 +27,23 @@ public: }; public: - explicit AttackChoice(BattleState *b = 0) : type(UNDECIDED), selection(b) { } + explicit AttackChoice(BattleState *b = 0) : thing(0), selection(b), type(UNDECIDED) { } ~AttackChoice() { } public: Type GetType() const { return type; } void SetType(Type t) { type = t; } + const common::Item *GetItem() const { return (const common::Item *)thing; } + void SetItem(const common::Item *i) { thing = i; } TargetSelection &Selection() { return selection; } const TargetSelection &Selection() const { return selection; } + void Reset(); + private: - Type type; + const void *thing; TargetSelection selection; + Type type; }; diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 3b29354..1a0586c 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -86,6 +86,8 @@ public: const geometry::Point &HeroTagPositionAt(std::vector::size_type index) const { return heroTagPositions[index]; } bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; } void SetAttackType(AttackChoice::Type t) { attackChoices[activeHero].SetType(t); } + AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; } + const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; } TargetSelection &ActiveHeroTargets() { return attackChoices[activeHero].Selection(); } const TargetSelection &ActiveHeroTargets() const { return attackChoices[activeHero].Selection(); } bool AttackSelectionDone() const { return activeHero >= (int) heroes.size(); } diff --git a/src/battle/states/SelectAttackType.cpp b/src/battle/states/SelectAttackType.cpp index 1039e51..90d483c 100644 --- a/src/battle/states/SelectAttackType.cpp +++ b/src/battle/states/SelectAttackType.cpp @@ -75,7 +75,7 @@ void SelectAttackType::HandleInput(const Input &input) { // TODO: detect single/multiple/all attack mode battle->ActiveHeroTargets().SetSingle(); battle->ActiveHeroTargets().Reset(); - ctrl->PushState(new SelectTarget(battle, this, &battle->ActiveHeroTargets(), battle->Res().weaponTargetCursor, true)); + ctrl->PushState(new SelectTarget(battle, this, &battle->ActiveHeroTargets(), battle->Res().weaponTargetCursor)); break; case AttackChoice::MAGIC: if (battle->ActiveHero().CanUseMagic()) { @@ -96,11 +96,12 @@ void SelectAttackType::HandleInput(const Input &input) { throw std::logic_error("selected invalid attack type"); } } else if (input.JustPressed(Input::ACTION_B)) { + battle->ActiveHeroAttackChoice().Reset(); battle->PreviousHero(); if (battle->BeforeFirstHero()) { ctrl->ChangeState(new SelectMoveAction(battle)); } else { - battle->SetAttackType(AttackChoice::UNDECIDED); + battle->ActiveHeroAttackChoice().Reset(); } } diff --git a/src/battle/states/SelectItem.cpp b/src/battle/states/SelectItem.cpp index 4f6c7af..a2d178e 100644 --- a/src/battle/states/SelectItem.cpp +++ b/src/battle/states/SelectItem.cpp @@ -8,15 +8,18 @@ #include "SelectItem.h" #include "SelectAttackType.h" +#include "SelectTarget.h" #include "../BattleState.h" #include "../../app/Application.h" #include "../../app/Input.h" +#include "../../common/Item.h" #include "../../geometry/Point.h" #include "../../geometry/operators.h" #include "../../graphics/Frame.h" using app::Application; using app::Input; +using common::Item; using geometry::Point; using geometry::Vector; using graphics::Frame; @@ -32,7 +35,9 @@ void SelectItem::ExitState(Application &c, SDL_Surface *screen) { } void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) { - + if (battle->ActiveHeroTargets().HasSelected()) { + ctrl.PopState(); + } } void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) { @@ -47,11 +52,31 @@ void SelectItem::Resize(int width, int height) { void SelectItem::HandleInput(const Input &input) { if (input.JustPressed(Input::ACTION_A)) { - // TODO: switch to target select if (battle->GetItemMenu().SelectedIsEnabled()) { - battle->SetAttackType(AttackChoice::ITEM); - battle->NextHero(); - ctrl->PopState(); + const Item *item(battle->GetItemMenu().Selected()); + battle->ActiveHeroTargets().Reset(); + if (item->TargetAlly()) { + battle->ActiveHeroTargets().SelectHeroes(); + } else { + battle->ActiveHeroTargets().SelectEnemies(); + } + if (item->TargetAll()) { + battle->SetAttackType(AttackChoice::ITEM); + // TODO: remove item from inventory + battle->ActiveHeroAttackChoice().SetItem(item); + battle->NextHero(); + ctrl->PopState(); + } else { + if (item->TargetOne()) { + battle->ActiveHeroTargets().SetSingle(); + } else { + battle->ActiveHeroTargets().SetMultiple(); + } + ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().itemTargetCursor)); + } +// battle->SetAttackType(AttackChoice::ITEM); +// battle->NextHero(); +// ctrl->PopState(); } } if (input.JustPressed(Input::ACTION_B)) { diff --git a/src/battle/states/SelectTarget.h b/src/battle/states/SelectTarget.h index b9085a5..f84dd31 100644 --- a/src/battle/states/SelectTarget.h +++ b/src/battle/states/SelectTarget.h @@ -24,7 +24,7 @@ class SelectTarget : public app::State { public: - SelectTarget(BattleState *battle, SelectAttackType *parent, TargetSelection *selection, const graphics::Sprite *cursorIcon, bool startWithEnemy) + SelectTarget(BattleState *battle, SelectAttackType *parent, TargetSelection *selection, const graphics::Sprite *cursorIcon) : ctrl(0), battle(battle), parent(parent), selection(selection), cursorIcon(cursorIcon), flipFlop(true) { } public: diff --git a/src/common/Item.h b/src/common/Item.h index 2f66207..1dde887 100644 --- a/src/common/Item.h +++ b/src/common/Item.h @@ -79,8 +79,9 @@ public: void SetName(const char *n) { name = n; } void SetMenuIcon(const graphics::Sprite *icon) { menuIcon = icon; } void SetUsableInBattle() { usability |= USABILITY_BATTLE; } + void SetTargettingMode(int m) { targettingMode = m; } -private: +public: enum Usability { USABILITY_MOST_USEFUL = 1, USABILITY_EQUIPABLE = 2, diff --git a/src/main.cpp b/src/main.cpp index 5e14001..503c426 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -197,11 +197,12 @@ int main(int argc, char **argv) { SDL_Surface *targetingIconsImg(IMG_Load("test-data/targeting-icons.png")); Sprite weaponTargetCursor(targetingIconsImg, 32, 32); + Sprite itemTargetCursor(targetingIconsImg, 32, 32, 0, 64); battleRes.weaponTargetCursor = &weaponTargetCursor; // TODO: add image for magic targeting cursor battleRes.magicTargetCursor = &weaponTargetCursor; // TODO: add image for item targeting cursor - battleRes.itemTargetCursor = &weaponTargetCursor; + battleRes.itemTargetCursor = &itemTargetCursor; battleRes.spellMenuHeadline = "Please choose a spell."; battleRes.spellMenuPrototype = Menu(&normalFont, &disabledFont, &handCursorSprite, 12, 6, 8, 0, 2, 32); @@ -244,16 +245,19 @@ int main(int argc, char **argv) { antidote.SetName("Antidote"); antidote.SetMenuIcon(&potionIcon); antidote.SetUsableInBattle(); + antidote.SetTargettingMode(Item::TARGETTING_MODE_ALLY | Item::TARGETTING_MODE_ONE); inventory.Add(&antidote, 9); Item magicJar; magicJar.SetName("Magic jar"); magicJar.SetMenuIcon(&potionIcon); magicJar.SetUsableInBattle(); + magicJar.SetTargettingMode(Item::TARGETTING_MODE_ALLY | Item::TARGETTING_MODE_ONE); inventory.Add(&magicJar, 4); Item hiPotion; hiPotion.SetName("Hi-Potion"); hiPotion.SetMenuIcon(&potionIcon); hiPotion.SetUsableInBattle(); + hiPotion.SetTargettingMode(Item::TARGETTING_MODE_ALLY | Item::TARGETTING_MODE_ONE); inventory.Add(&hiPotion, 4); Item powerPotion; powerPotion.SetName("Power potion"); @@ -266,10 +270,18 @@ int main(int argc, char **argv) { sleepBall.SetName("Sleep ball"); sleepBall.SetMenuIcon(&ballIcon); sleepBall.SetUsableInBattle(); + sleepBall.SetTargettingMode(Item::TARGETTING_MODE_ENEMY | Item::TARGETTING_MODE_ONE); inventory.Add(&sleepBall, 1); + Item multiBall; + multiBall.SetName("Multi-ball!"); + multiBall.SetMenuIcon(&ballIcon); + multiBall.SetUsableInBattle(); + multiBall.SetTargettingMode(Item::TARGETTING_MODE_ENEMY | Item::TARGETTING_MODE_MULTIPLE); + inventory.Add(&multiBall, 1); Item figgoru; figgoru.SetName("Figgoru"); figgoru.SetMenuIcon(&crankIcon); + figgoru.SetTargettingMode(Item::TARGETTING_MODE_ENEMY | Item::TARGETTING_MODE_ALL); inventory.Add(&figgoru, 1); battleRes.inventory = &inventory; diff --git a/test-data/targeting-icons.png b/test-data/targeting-icons.png index 64b32bf9f98a043edfd6c7a16f084938b3329b38..c0e56fa83adda1b1386bbf9564296876c24d65af 100644 GIT binary patch delta 733 zcmV<30wVpM1Ly^i7k^*~1^@s6d?Ij|00087NklHV%9DS)Wv>V-;0f}s+ zTQlKG4E4%JRV0YirE7#lXqWy0EI8S!PAo`fmg2Yd09q&-4z5 zYc7#`SKV7IJ8_ivejoSou7L$%dF|eBllPWz%V;c$@y3Hq0Dr*n?K=QK@Mx{}z4Y4z z01)nU>`k8YK1KnU50FV#10RT@A3IM>_*DrI$ zoXqW?4=@*!L4RAs{8a`3=)W8S0D3QaMvSLbm3>vfY>)F$%v|oT0st=0f7L4S`e1J+ z#`cc|y;d%&p(C!HiRcR}r4soGt7EUWuHBmzS6P23Vj4W$000~v4{KjwpJ)eNaozcJ zM-;exABhh4V=O7r|9nwf-|Q1vdR2}cg^?2WbV5}L=6?X0sr9X{jNHuu-I~vonKKG_ zczAetczAetczAetczAedcRy~rV`93qV(<9@)kESAX*kLyhPsmzIwh8z$pI>{JrI5Z z_lWYq+Vp=}uIUp)TuSsP}sE*9a?mp~RK!utM1apF}8vO=r3FXq` zzEJElF9{*Y{Zi@Ssz# P00000NkvXXu0mjfBBNa4 delta 401 zcmV;C0dD^21)c+t7k?lK1^@s6b9#F80004HNklA2#^^nXBQF%GkgVBNl+M)VitnJ){v~IR)Q-DVs*@I4#e#UYYxV4OSp38Qt@l@ z{=GYk6|GWv<6UQNjXz%2su0(<_W_rf0`ciwSd@ vFwcbafJBzhcm8EmTo259^XVch3