clooneljump/src/gameplaysceneclassic.cpp

107 lines
4 KiB
C++
Raw Normal View History

2014-02-25 10:04:16 +00:00
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gameplaysceneclassic.hpp"
#include "character.hpp"
#include "moversine.hpp"
#include "sdlmain.hpp"
#include "inputbag.hpp"
#include "key.hpp"
#include "moverleftright.hpp"
#include "tiledwallpaper.hpp"
#include <algorithm>
#include <SDL2/SDL_scancode.h>
#include <ciso646>
namespace cloonel {
namespace {
enum GameActionType {
GameAction_Left,
GameAction_Right
};
} //unnamed namespace
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplaySceneClassic::GameplaySceneClassic (SDLMain* parSdlMain) :
2014-03-06 09:56:02 +00:00
GameplayScene(parSdlMain)
{
//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());
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f)));
2014-03-05 21:09:32 +00:00
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f));
std::unique_ptr<TiledWallpaper> wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject()));
player->Prepare();
2014-02-24 20:09:33 +00:00
moverSine->RegisterPlaceable(player.get());
moverLeftRight->RegisterPlaceable(player.get());
wallpaper->Reload();
std::swap(moverSine, m_moverSine);
std::swap(player, m_player);
std::swap(moverLeftRight, m_moverLeftRight);
std::swap(wallpaper, m_wallpaper);
AddMover(m_moverSine.get());
AddMover(m_moverLeftRight.get());
AddDrawable(m_wallpaper.get());
AddDrawable(m_player.get());
2014-03-06 10:43:28 +00:00
m_moverSine->SetPower(static_cast<float>(SDLObject()->WidthHeight().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));
m_wallpaper = std::move(std::unique_ptr<TiledWallpaper>(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