diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b75b11..0b64ec9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ add_executable(${PROJECT_NAME} src/gameplaysceneclassic.cpp src/moverrelative.cpp src/inputbag.cpp + src/moverleftright.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/gamebase.cpp b/src/gamebase.cpp index 04a5ca1..b313f73 100644 --- a/src/gamebase.cpp +++ b/src/gamebase.cpp @@ -58,6 +58,7 @@ namespace cloonel { SDL_Renderer* const ren = m_sdlmain->GetRenderer(); SDL_RenderClear(ren); + OnPreUpdate(); OnUpdate(delta); OnRender(); SDL_RenderPresent(ren); diff --git a/src/gamebase.hpp b/src/gamebase.hpp index d9f3089..8b5d696 100644 --- a/src/gamebase.hpp +++ b/src/gamebase.hpp @@ -25,6 +25,7 @@ namespace cloonel { private: virtual void OnRender ( void ) = 0; + virtual void OnPreUpdate ( void ) = 0; virtual void OnUpdate ( float parDelta ) = 0; virtual bool ShouldQuit ( void ) const; diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 8685d9f..9b0f7db 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -4,8 +4,10 @@ #include "sdlmain.hpp" #include "inputbag.hpp" #include "key.hpp" +#include "moverleftright.hpp" #include #include +#include namespace cloonel { namespace { @@ -27,7 +29,7 @@ namespace cloonel { //TODO: replace the hardcoded bindings with something more customizable. InputBag& input = *InputBagObject(); input.AddAction(GameAction_Left, Key(InputDevice_Keyboard, SDL_SCANCODE_LEFT), "Move left"); - input.AddAction(GameAction_Left, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right"); + input.AddAction(GameAction_Right, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right"); } ///-------------------------------------------------------------------------- @@ -41,14 +43,18 @@ namespace cloonel { void GameplaySceneClassic::Prepare() { std::unique_ptr moverSine(new MoverSine()); std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120))); + std::unique_ptr moverLeftRight(new MoverLeftRight()); player->Prepare(); moverSine->RegisterPlaceable(player.get()); + moverLeftRight->RegisterPlaceable(player.get()); std::swap(moverSine, m_moverSine); std::swap(player, m_player); + std::swap(moverLeftRight, m_moverLeftRight); AddMover(m_moverSine.get()); + AddMover(m_moverLeftRight.get()); AddDrawable(m_player.get()); m_moverSine->SetPower(static_cast(SDLObject()->DefWidthHeight().y() / 2)); @@ -59,5 +65,21 @@ namespace cloonel { void GameplaySceneClassic::Destroy() noexcept { m_moverSine = std::move(std::unique_ptr(nullptr)); m_player = std::move(std::unique_ptr(nullptr)); + m_moverLeftRight = std::move(std::unique_ptr(nullptr)); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void GameplaySceneClassic::OnPreUpdate() { + InputBag& input = *InputBagObject(); + if (IsPressed(input, GameAction_Left)) { + m_moverLeftRight->SetMovement(MoverLeftRight::MovementDirection_Left); + } + else if (IsPressed(input, GameAction_Right)) { + m_moverLeftRight->SetMovement(MoverLeftRight::MovementDirection_Right); + } + else { + m_moverLeftRight->SetMovement(MoverLeftRight::MovementDirection_Still); + } } } //namespace cloonel diff --git a/src/gameplaysceneclassic.hpp b/src/gameplaysceneclassic.hpp index bd52e74..cebc4ab 100644 --- a/src/gameplaysceneclassic.hpp +++ b/src/gameplaysceneclassic.hpp @@ -8,21 +8,25 @@ namespace cloonel { class SDLMain; class Character; class MoverSine; + class MoverLeftRight; class GameplaySceneClassic : public GameplayScene { public: explicit GameplaySceneClassic ( SDLMain* parSdlMain ); - ~GameplaySceneClassic ( void ) noexcept; + virtual ~GameplaySceneClassic ( void ) noexcept; virtual void Prepare ( void ); virtual void Destroy ( void ) noexcept; private: + virtual void OnPreUpdate ( void ); + struct LocalData; const std::unique_ptr m_local; std::unique_ptr m_player; std::unique_ptr m_moverSine; + std::unique_ptr m_moverLeftRight; }; } //namespace cloonel diff --git a/src/inputbag.cpp b/src/inputbag.cpp index d8364b0..2d1aba5 100644 --- a/src/inputbag.cpp +++ b/src/inputbag.cpp @@ -43,7 +43,7 @@ namespace cloonel { ///When actions vector is reallocated, update pointers in mappings. ///---------------------------------------------------------------------- void UpdatePointers (Action* parOldAddrStart, Action* parNewAddress, std::map& parUpdate, int parOffset) { - for (auto itMap : parUpdate) { + for (auto& itMap : parUpdate) { if (itMap.second >= parOldAddrStart) { itMap.second = parNewAddress + (itMap.second - parOldAddrStart) + parOffset; } @@ -121,4 +121,11 @@ namespace cloonel { currAction.state = ActionState_Released; } } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + bool IsPressed (const InputBag& parInput, int parAction) { + const InputBag::ActionStateType state = parInput.ActionState(parAction); + return InputBag::ActionState_Pressed == state or InputBag::ActionState_JustPressed == state; + } } //namespace cloonel diff --git a/src/inputbag.hpp b/src/inputbag.hpp index 2dfa84d..aafce4a 100644 --- a/src/inputbag.hpp +++ b/src/inputbag.hpp @@ -3,6 +3,7 @@ #include "inputdevicetype.hpp" #include +#include namespace cloonel { struct Key; @@ -12,8 +13,8 @@ namespace cloonel { enum ActionStateType { ActionState_Released, ActionState_Pressed, - ActionState_JustPressed, - ActionState_JustReleased + ActionState_JustReleased, + ActionState_JustPressed }; InputBag ( void ); @@ -31,6 +32,9 @@ namespace cloonel { const std::unique_ptr m_localdata; }; + + bool IsPressed ( const InputBag& parInput, int parAction ); + inline bool IsReleased ( const InputBag& parInput, int parAction ) { return not IsPressed(parInput, parAction); } } //namespace cloonel #endif diff --git a/src/key.hpp b/src/key.hpp index 985b87d..29cfde6 100644 --- a/src/key.hpp +++ b/src/key.hpp @@ -25,7 +25,7 @@ namespace cloonel { ~Key ( void ) noexcept = default; bool operator== ( const Key& parOther ) const { - return srcdevice == parOther.srcdevice and scancode == parOther.srcdevice; + return srcdevice == parOther.srcdevice and scancode == parOther.scancode; } bool operator!= ( const Key& parOther ) const { return not this->operator==(parOther); diff --git a/src/moverleftright.cpp b/src/moverleftright.cpp new file mode 100644 index 0000000..218bb36 --- /dev/null +++ b/src/moverleftright.cpp @@ -0,0 +1,40 @@ +#include "moverleftright.hpp" + +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + MoverLeftRight::MoverLeftRight() : + m_movement(0.0f), + m_velocity(320.0f), + m_step(0.0f) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void MoverLeftRight::ApplyMotion (float parDelta) { + m_step = m_velocity * parDelta * m_movement; + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + float2 MoverLeftRight::GetOffset() const { + return float2(m_step, 0.0f); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void MoverLeftRight::SetMovement (MovementDirectionType parDirection) { + switch (parDirection) { + case MovementDirection_Left: + m_movement = -1.0f; + break; + case MovementDirection_Right: + m_movement = 1.0f; + break; + case MovementDirection_Still: + m_movement = 0.0f; + break; + } + } +} //namespace cloonel diff --git a/src/moverleftright.hpp b/src/moverleftright.hpp new file mode 100644 index 0000000..dfc42e2 --- /dev/null +++ b/src/moverleftright.hpp @@ -0,0 +1,30 @@ +#ifndef id340A16673C014E0BBFE8AC24D8FC1C77 +#define id340A16673C014E0BBFE8AC24D8FC1C77 + +#include "moveroneshot.hpp" + +namespace cloonel { + class MoverLeftRight : public MoverOneShot { + public: + enum MovementDirectionType { + MovementDirection_Left, + MovementDirection_Right, + MovementDirection_Still + }; + + MoverLeftRight ( void ); + virtual ~MoverLeftRight ( void ) noexcept = default; + + void SetMovement ( MovementDirectionType parDirection ); + + private: + virtual float2 GetOffset ( void ) const; + virtual void ApplyMotion ( float parDelta ); + + float m_movement; + float m_velocity; + float m_step; + }; +} //namespace cloonel + +#endif