Movement with left/right arrow keys.
Movement is hardcoded but it works. Also fixed some typos that gave me a hard time figuring out why movement was giving all sort of crazy problems. Very bad typos, see gameplaysceneclassic.cpp, inputbag.cpp, inputbag.hpp and key.hpp if you want to get goosebumps.
This commit is contained in:
parent
597607366e
commit
ab31e94bf0
10 changed files with 116 additions and 6 deletions
|
@ -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}
|
||||
|
|
|
@ -58,6 +58,7 @@ namespace cloonel {
|
|||
SDL_Renderer* const ren = m_sdlmain->GetRenderer();
|
||||
|
||||
SDL_RenderClear(ren);
|
||||
OnPreUpdate();
|
||||
OnUpdate(delta);
|
||||
OnRender();
|
||||
SDL_RenderPresent(ren);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
#include "sdlmain.hpp"
|
||||
#include "inputbag.hpp"
|
||||
#include "key.hpp"
|
||||
#include "moverleftright.hpp"
|
||||
#include <algorithm>
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
#include <ciso646>
|
||||
|
||||
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> moverSine(new MoverSine());
|
||||
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120)));
|
||||
std::unique_ptr<MoverLeftRight> 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<float>(SDLObject()->DefWidthHeight().y() / 2));
|
||||
|
@ -59,5 +65,21 @@ namespace cloonel {
|
|||
void GameplaySceneClassic::Destroy() noexcept {
|
||||
m_moverSine = std::move(std::unique_ptr<MoverSine>(nullptr));
|
||||
m_player = std::move(std::unique_ptr<Character>(nullptr));
|
||||
m_moverLeftRight = std::move(std::unique_ptr<MoverLeftRight>(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
|
||||
|
|
|
@ -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 <LocalData> m_local;
|
||||
std::unique_ptr<Character> m_player;
|
||||
std::unique_ptr<MoverSine> m_moverSine;
|
||||
std::unique_ptr<MoverLeftRight> m_moverLeftRight;
|
||||
};
|
||||
} //namespace cloonel
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace cloonel {
|
|||
///When actions vector is reallocated, update pointers in mappings.
|
||||
///----------------------------------------------------------------------
|
||||
void UpdatePointers (Action* parOldAddrStart, Action* parNewAddress, std::map<int, Action*>& 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
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "inputdevicetype.hpp"
|
||||
#include <memory>
|
||||
#include <ciso646>
|
||||
|
||||
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<LocalData> 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
|
||||
|
|
|
@ -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);
|
||||
|
|
40
src/moverleftright.cpp
Normal file
40
src/moverleftright.cpp
Normal file
|
@ -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
|
30
src/moverleftright.hpp
Normal file
30
src/moverleftright.hpp
Normal file
|
@ -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
|
Loading…
Reference in a new issue