clooneljump/src/gameplaysceneclassic.cpp

86 lines
3.1 KiB
C++
Raw Normal View History

#include "gameplaysceneclassic.hpp"
#include "character.hpp"
#include "moversine.hpp"
#include "sdlmain.hpp"
#include "inputbag.hpp"
#include "key.hpp"
#include "moverleftright.hpp"
#include <algorithm>
#include <SDL2/SDL_scancode.h>
#include <ciso646>
namespace cloonel {
namespace {
enum GameActionType {
GameAction_Left,
GameAction_Right
};
} //unnamed namespace
struct GameplaySceneClassic::LocalData {
};
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplaySceneClassic::GameplaySceneClassic (SDLMain* parSdlMain) :
GameplayScene(parSdlMain),
m_local(new LocalData)
{
//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_Right, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right");
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplaySceneClassic::~GameplaySceneClassic() noexcept {
Destroy();
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplaySceneClassic::Prepare() {
std::unique_ptr<MoverSine> moverSine(new MoverSine());
2014-02-22 11:25:16 +00:00
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120)));
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight());
player->Prepare();
2014-02-24 20:09:33 +00:00
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));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
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