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:
King_DuckZ 2014-02-25 00:36:33 +01:00
parent 597607366e
commit ab31e94bf0
10 changed files with 116 additions and 6 deletions

View file

@ -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