From 558fd3d14ad1a9dc347998691a0b300fd334a16a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 7 Aug 2012 13:52:31 +0200 Subject: [PATCH] split battle state into (so far) 2 states --- Debug/makefile | 1 + Debug/sources.mk | 1 + Debug/src/battle/states/subdir.mk | 27 ++++++++ Release/makefile | 1 + Release/sources.mk | 1 + Release/src/battle/states/subdir.mk | 27 ++++++++ src/battle/BattleState.cpp | 57 +++-------------- src/battle/BattleState.h | 15 ++++- src/battle/states/SelectAttackType.cpp | 87 ++++++++++++++++++++++++++ src/battle/states/SelectAttackType.h | 47 ++++++++++++++ src/battle/states/SelectMoveAction.cpp | 80 +++++++++++++++++++++++ src/battle/states/SelectMoveAction.h | 48 ++++++++++++++ 12 files changed, 342 insertions(+), 50 deletions(-) create mode 100644 Debug/src/battle/states/subdir.mk create mode 100644 Release/src/battle/states/subdir.mk create mode 100644 src/battle/states/SelectAttackType.cpp create mode 100644 src/battle/states/SelectAttackType.h create mode 100644 src/battle/states/SelectMoveAction.cpp create mode 100644 src/battle/states/SelectMoveAction.h diff --git a/Debug/makefile b/Debug/makefile index 507437a..2000ebc 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -10,6 +10,7 @@ RM := rm -rf -include sources.mk -include src/sdl/subdir.mk -include src/graphics/subdir.mk +-include src/battle/states/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index 0fdadd3..cd96090 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -26,6 +26,7 @@ SUBDIRS := \ src/sdl \ src \ src/graphics \ +src/battle/states \ src/battle \ src/app \ diff --git a/Debug/src/battle/states/subdir.mk b/Debug/src/battle/states/subdir.mk new file mode 100644 index 0000000..8a61792 --- /dev/null +++ b/Debug/src/battle/states/subdir.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/battle/states/SelectAttackType.cpp \ +../src/battle/states/SelectMoveAction.cpp + +OBJS += \ +./src/battle/states/SelectAttackType.o \ +./src/battle/states/SelectMoveAction.o + +CPP_DEPS += \ +./src/battle/states/SelectAttackType.d \ +./src/battle/states/SelectMoveAction.d + + +# Each subdirectory must supply rules for building sources it contributes +src/battle/states/%.o: ../src/battle/states/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/usr/include/SDL -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Release/makefile b/Release/makefile index 507437a..2000ebc 100644 --- a/Release/makefile +++ b/Release/makefile @@ -10,6 +10,7 @@ RM := rm -rf -include sources.mk -include src/sdl/subdir.mk -include src/graphics/subdir.mk +-include src/battle/states/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Release/sources.mk b/Release/sources.mk index 0fdadd3..cd96090 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -26,6 +26,7 @@ SUBDIRS := \ src/sdl \ src \ src/graphics \ +src/battle/states \ src/battle \ src/app \ diff --git a/Release/src/battle/states/subdir.mk b/Release/src/battle/states/subdir.mk new file mode 100644 index 0000000..05f8177 --- /dev/null +++ b/Release/src/battle/states/subdir.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/battle/states/SelectAttackType.cpp \ +../src/battle/states/SelectMoveAction.cpp + +OBJS += \ +./src/battle/states/SelectAttackType.o \ +./src/battle/states/SelectMoveAction.o + +CPP_DEPS += \ +./src/battle/states/SelectAttackType.d \ +./src/battle/states/SelectMoveAction.d + + +# Each subdirectory must supply rules for building sources it contributes +src/battle/states/%.o: ../src/battle/states/%.cpp + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -I/usr/include/SDL -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index a2c306a..4e12f79 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -8,6 +8,7 @@ #include "BattleState.h" #include "PartyLayout.h" +#include "states/SelectMoveAction.h" #include "../app/Application.h" #include "../app/Input.h" #include "../geometry/operators.h" @@ -50,6 +51,7 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { heroTags.push_back(HeroTag(&heroes[i], HeroTag::Alignment((i + 1) % 2))); } + ctrl.PushState(new SelectMoveAction(this)); } void BattleState::ExitState() { @@ -58,35 +60,7 @@ void BattleState::ExitState() { void BattleState::HandleInput(const Input &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) { @@ -94,19 +68,13 @@ void BattleState::UpdateWorld(float deltaT) { } void BattleState::Render(SDL_Surface *screen) { - Vector offset( - (screen->w - background->w) / 2, - (screen->h - background->h) / 2); + Vector offset(CalculateScreenOffset(screen)); RenderBackground(screen, offset); RenderMonsters(screen, offset); // RenderHeroes(screen, offset); RenderHeroTags(screen, offset); - if (moveChoice == -1) { - RenderMoveMenu(screen, offset); - } else { - RenderAttackTypeMenu(screen, offset); - } + RenderAttackTypeMenu(screen, offset); } void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { @@ -133,9 +101,9 @@ void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { } void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { - int uiHeight(background->h / 2), uiOffset(uiHeight); + int uiHeight(BackgroundHeight() / 2), uiOffset(uiHeight); int tagHeight((uiHeight - attackTypeMenu.IconHeight()) / 2); - int tagWidth((background->w - attackTypeMenu.IconWidth()) / 2); + int tagWidth((BackgroundWidth() - attackTypeMenu.IconWidth()) / 2); Point tagPosition[4]; tagPosition[0] = Point(0, uiOffset); @@ -150,16 +118,9 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) void BattleState::RenderAttackTypeMenu(SDL_Surface *screen, const Vector &offset) { Point position( - (background->w - attackTypeMenu.Width()) / 2, - (background->h * 3 / 4) - (attackTypeMenu.Height() / 2)); + (BackgroundWidth() - attackTypeMenu.Width()) / 2, + (BackgroundHeight() * 3 / 4) - (attackTypeMenu.Height() / 2)); 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 daf1cae..a3c0879 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -55,13 +55,24 @@ public: virtual void UpdateWorld(float deltaT); virtual void Render(SDL_Surface *); -private: +public: + AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; } + MoveMenu &GetMoveMenu() { return moveMenu; } + +public: + geometry::Vector CalculateScreenOffset(SDL_Surface *screen) const { + return geometry::Vector( + (screen->w - background->w) / 2, + (screen->h - background->h) / 2); + } + int BackgroundWidth() const { return background->w; } + int BackgroundHeight() const { return background->h; } + void RenderBackground(SDL_Surface *screen, const geometry::Vector &offset); void RenderMonsters(SDL_Surface *screen, const geometry::Vector &offset); 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; diff --git a/src/battle/states/SelectAttackType.cpp b/src/battle/states/SelectAttackType.cpp new file mode 100644 index 0000000..1d76b6c --- /dev/null +++ b/src/battle/states/SelectAttackType.cpp @@ -0,0 +1,87 @@ +/* + * SelectAttackType.cpp + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#include "SelectAttackType.h" + +#include "../BattleState.h" +#include "../../app/Input.h" +#include "../../geometry/operators.h" + +using app::Input; +using geometry::Point; +using geometry::Vector; + +namespace battle { + +void SelectAttackType::EnterState(app::Application &c, SDL_Surface *screen) { + ctrl = &c; +} + +void SelectAttackType::ExitState() { + ctrl = 0; +} + + +void SelectAttackType::Resize(int width, int height) { + +} + + +void SelectAttackType::HandleInput(const Input &input) { + if (input.IsDown(Input::PAD_UP)) { + battle->GetAttackTypeMenu().Select(AttackTypeMenu::MAGIC); + } else if (input.IsDown(Input::PAD_RIGHT)) { + battle->GetAttackTypeMenu().Select(AttackTypeMenu::DEFEND); + } else if (input.IsDown(Input::PAD_DOWN)) { + battle->GetAttackTypeMenu().Select(AttackTypeMenu::IKARI); + } else if (input.IsDown(Input::PAD_LEFT)) { + battle->GetAttackTypeMenu().Select(AttackTypeMenu::ITEM); + } else { + battle->GetAttackTypeMenu().Select(AttackTypeMenu::SWORD); + } + + if (input.JustPressed(Input::ACTION_A)) { + switch (battle->GetAttackTypeMenu().Selected()) { + case AttackTypeMenu::SWORD: + // TODO: switch to next character + break; + case AttackTypeMenu::MAGIC: + // TODO: switch to spell select + break; + case AttackTypeMenu::DEFEND: + // TODO: switch to next character + break; + case AttackTypeMenu::IKARI: + // TODO: switch to ikari attack select + break; + case AttackTypeMenu::ITEM: + // TODO: switch to item select + break; + } + } +} + +void SelectAttackType::UpdateWorld(float deltaT) { + +} + +void SelectAttackType::Render(SDL_Surface *screen) { + Vector offset(battle->CalculateScreenOffset(screen)); + battle->RenderBackground(screen, offset); + battle->RenderMonsters(screen, offset); + battle->RenderHeroTags(screen, offset); + RenderMenu(screen, offset); +} + +void SelectAttackType::RenderMenu(SDL_Surface *screen, const Vector &offset) { + Point position( + (battle->BackgroundWidth() - battle->GetAttackTypeMenu().Width()) / 2, + (battle->BackgroundHeight() * 3 / 4) - (battle->GetAttackTypeMenu().Height() / 2)); + battle->GetAttackTypeMenu().Render(screen, position + offset); +} + +} diff --git a/src/battle/states/SelectAttackType.h b/src/battle/states/SelectAttackType.h new file mode 100644 index 0000000..3b10188 --- /dev/null +++ b/src/battle/states/SelectAttackType.h @@ -0,0 +1,47 @@ +/* + * SelectAttackType.h + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#ifndef BATTLE_SELECTATTACKTYPE_H_ +#define BATTLE_SELECTATTACKTYPE_H_ + +#include "../../app/State.h" +#include "../../geometry/Vector.h" + +namespace battle { + +class AttackTypeMenu; +class BattleState; + +class SelectAttackType +: public app::State { + +public: + explicit SelectAttackType(BattleState *battle) + : ctrl(0), battle(battle) { } + ~SelectAttackType() { } + +public: + virtual void EnterState(app::Application &ctrl, SDL_Surface *screen); + virtual void ExitState(); + + virtual void Resize(int width, int height); + + virtual void HandleInput(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: + void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); + +private: + app::Application *ctrl; + BattleState *battle; + +}; + +} +#endif /* BATTLE_SELECTATTACKTYPE_H_ */ diff --git a/src/battle/states/SelectMoveAction.cpp b/src/battle/states/SelectMoveAction.cpp new file mode 100644 index 0000000..4a19d48 --- /dev/null +++ b/src/battle/states/SelectMoveAction.cpp @@ -0,0 +1,80 @@ +/* + * SelectMoveAction.cpp + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#include "SelectMoveAction.h" + +#include "SelectAttackType.h" +#include "../BattleState.h" +#include "../MoveMenu.h" +#include "../../app/Application.h" +#include "../../app/Input.h" +#include "../../geometry/operators.h" + +using app::Input; +using geometry::Point; +using geometry::Vector; + +namespace battle { + +void SelectMoveAction::EnterState(app::Application &c, SDL_Surface *screen) { + ctrl = &c; +} + +void SelectMoveAction::ExitState() { + ctrl = 0; +} + + +void SelectMoveAction::Resize(int width, int height) { + +} + + +void SelectMoveAction::HandleInput(const app::Input &input) { + if (input.IsDown(Input::PAD_UP)) { + battle->GetMoveMenu().Select(MoveMenu::CHANGE); + } else if (input.IsDown(Input::PAD_DOWN)) { + battle->GetMoveMenu().Select(MoveMenu::RUN); + } else { + battle->GetMoveMenu().Select(MoveMenu::ATTACK); + } + + if (input.JustPressed(Input::ACTION_A)) { + switch (battle->GetMoveMenu().Selected()) { + case MoveMenu::ATTACK: + ctrl->ChangeState(new SelectAttackType(battle)); + break; + case MoveMenu::CHANGE: + // TODO: switch to change state + break; + case MoveMenu::RUN: + // TODO: switch to run state + break; + } + } +} + +void SelectMoveAction::UpdateWorld(float deltaT) { + +} + +void SelectMoveAction::Render(SDL_Surface *screen) { + Vector offset(battle->CalculateScreenOffset(screen)); + battle->RenderBackground(screen, offset); + battle->RenderMonsters(screen, offset); + battle->RenderHeroTags(screen, offset); + RenderMenu(screen, offset); +} + +void SelectMoveAction::RenderMenu(SDL_Surface *screen, const Vector &offset) { + Point position( + (battle->BackgroundWidth() - battle->GetMoveMenu().Width()) / 2, + (battle->BackgroundHeight() * 3 / 4) - (battle->GetMoveMenu().Height() / 2)); + battle->GetMoveMenu().Render(screen, position + offset); +} + +} diff --git a/src/battle/states/SelectMoveAction.h b/src/battle/states/SelectMoveAction.h new file mode 100644 index 0000000..c135663 --- /dev/null +++ b/src/battle/states/SelectMoveAction.h @@ -0,0 +1,48 @@ +/* + * SelectMoveAction.h + * + * Created on: Aug 7, 2012 + * Author: holy + */ + +#ifndef BATTLE_SELECTMOVEACTION_H_ +#define BATTLE_SELECTMOVEACTION_H_ + +#include "../../app/State.h" +#include "../../geometry/Vector.h" + +namespace battle { + +class BattleState; +class MoveMenu; + +class SelectMoveAction +: public app::State { + +public: + explicit SelectMoveAction(BattleState *battle) + : ctrl(0), battle(battle) { } + ~SelectMoveAction() { } + +public: + virtual void EnterState(app::Application &ctrl, SDL_Surface *screen); + virtual void ExitState(); + + virtual void Resize(int width, int height); + + virtual void HandleInput(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: + void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset); + +private: + app::Application *ctrl; + BattleState *battle; + +}; + +} + +#endif /* BATTLE_SELECTMOVEACTION_H_ */ -- 2.39.2