Add some (badly) generated platforms that will scroll

down whenever the player's top side depasses the middle
of the screen.
WiP.
This commit is contained in:
King_DuckZ 2014-07-05 20:33:16 +02:00
parent 976d34f17e
commit 4a3a0a4782
13 changed files with 251 additions and 55 deletions

View file

@ -54,6 +54,7 @@ add_executable(${PROJECT_NAME}
src/sizenotifiable.cpp
src/platform.cpp
src/platformsystem.cpp
src/movers/moverworld.cpp
)
target_link_libraries(${PROJECT_NAME}

View file

@ -29,6 +29,7 @@
#define DEF_WIN_WIDTH REFERENCE_WIDTH
#define DEF_WIN_HEIGHT REFERENCE_HEIGHT
#define DEF_RANDOM_SEED 1984
#define MAX_PLATFORMS_ON_SCREEN 8
#define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@"

View file

@ -24,9 +24,11 @@
#include "inputbag.hpp"
#include "key.hpp"
#include "moverleftright.hpp"
#include "moverworld.hpp"
#include "tiledwallpaper.hpp"
#include "texture.hpp"
#include "platformsystem.hpp"
#include "CloonelJumpConfig.h"
#include <algorithm>
#include <SDL2/SDL_scancode.h>
#include <ciso646>
@ -59,40 +61,52 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplaySceneClassic::Prepare() {
const float halfRefHeight = static_cast<float>(REFERENCE_HEIGHT) / 2.0f;
std::unique_ptr<MoverSine> moverSine(new MoverSine());
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f)));
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f));
std::unique_ptr<MoverWorld> moverWorld(new MoverWorld(halfRefHeight));
std::unique_ptr<TiledWallpaper> wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject()));
std::unique_ptr<PlatformSystem> platforms(new PlatformSystem("resources/graphics/platform.png", SDLObject(), this));
std::unique_ptr<PlatformSystem> platforms(new PlatformSystem("resources/graphics/platform.png", SDLObject(), this, halfRefHeight * 0.9f, *moverWorld));
player->Prepare();
moverSine->RegisterPlaceable(player.get());
moverSine->RegisterPlaceable(moverWorld.get()); //Keep an invisible mover
moverLeftRight->RegisterPlaceable(player.get());
moverWorld->RegisterPlaceable(player.get()); //It compensates the position when the chara goes over the mid
moverWorld->RegisterPlaceable(moverWorld.get()); //The mover has to be in sync with the character
wallpaper->Reload();
platforms->Prepare();
std::swap(moverSine, m_moverSine);
std::swap(player, m_player);
std::swap(moverLeftRight, m_moverLeftRight);
std::swap(moverWorld, m_moverWorld);
std::swap(wallpaper, m_wallpaper);
std::swap(platforms, m_platforms);
AddMover(m_moverSine.get());
AddMover(m_moverLeftRight.get());
AddDrawable(m_wallpaper.get());
AddMover(m_moverWorld.get());
m_platforms->AddDrawables();
AddDrawable(m_player.get());
m_moverSine->SetPower(static_cast<float>(SDLObject()->WidthHeight().y() / 2));
const float jumpPower = halfRefHeight * 1.29f;
m_moverSine->SetPower(jumpPower);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
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));
//Destroy in reverse creation order
m_platforms = std::move(std::unique_ptr<PlatformSystem>(nullptr));
m_wallpaper = std::move(std::unique_ptr<TiledWallpaper>(nullptr));
m_moverWorld = std::move(std::unique_ptr<MoverWorld>(nullptr));
m_moverLeftRight = std::move(std::unique_ptr<MoverLeftRight>(nullptr));
m_player = std::move(std::unique_ptr<Character>(nullptr));
m_moverSine = std::move(std::unique_ptr<MoverSine>(nullptr));
}
///--------------------------------------------------------------------------

View file

@ -28,6 +28,7 @@ namespace cloonel {
class Character;
class MoverSine;
class MoverLeftRight;
class MoverWorld;
class TiledWallpaper;
class Texture;
class PlatformSystem;
@ -46,6 +47,7 @@ namespace cloonel {
std::unique_ptr<Character> m_player;
std::unique_ptr<MoverSine> m_moverSine;
std::unique_ptr<MoverLeftRight> m_moverLeftRight;
std::unique_ptr<MoverWorld> m_moverWorld;
std::unique_ptr<TiledWallpaper> m_wallpaper;
std::unique_ptr<PlatformSystem> m_platforms;
};

View file

@ -20,7 +20,7 @@
#ifndef id9CDCF10D483641A2845353C0835F93EC
#define id9CDCF10D483641A2845353C0835F93EC
//Move a Placeable by issuind an offset.
//Move a Placeable by issuing an offset.
//The offset is applied to the current position of the Placeable you are
//moving.
//For example issuing 1, then 2 will put your Placeable in position 3,

View file

@ -22,7 +22,7 @@
//Move a Placeable by issuing an offset relative to a base position.
//Use this if for example your Placeable is following a mathematical formula or
//if you're giving a position you want it to be at any specific time.
//if you're giving a position you want it to be at at any specific time.
//For example issuing 1, then 2 will put your Placeable in position 2,
//assuming it started at 0.

View file

@ -44,7 +44,7 @@ namespace cloonel {
void MoverSine::ApplyMotion (float parDelta) {
const float pitwo = static_cast<float>(M_PI) * 2.0f;
m_alpha += parDelta * 2.6f;
if (m_alpha >= pitwo)
while (m_alpha >= pitwo)
m_alpha -= pitwo;
}
} //namespace cloonel

39
src/movers/moverworld.cpp Normal file
View file

@ -0,0 +1,39 @@
/*
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 "moverworld.hpp"
#include <algorithm>
namespace cloonel {
MoverWorld::MoverWorld (float parMidPoint) :
Placeable(float2(0.0f)),
m_midPoint(parMidPoint),
m_offs(0.0f)
{
}
float2 MoverWorld::GetOffset() const {
return float2(0.0f, -m_offs);
}
void MoverWorld::ApplyMotion (float) {
const auto vertPos = this->GetPos().y();
m_offs = std::max(0.0f, vertPos - m_midPoint);
}
} //namespace cloonel

41
src/movers/moverworld.hpp Normal file
View file

@ -0,0 +1,41 @@
/*
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/>.
*/
#ifndef id47E717F84F3647A69E14C3B695B38F7B
#define id47E717F84F3647A69E14C3B695B38F7B
#include "moveroneshot.hpp"
#include "placeable.hpp"
namespace cloonel {
class MoverWorld : public MoverOneShot, public Placeable {
public:
explicit MoverWorld ( float parMidPoint );
virtual ~MoverWorld ( void ) noexcept = default;
private:
virtual float2 GetOffset ( void ) const;
virtual void ApplyMotion ( float parDelta );
const float m_midPoint;
float m_offs;
};
} //namespace cloonel
#endif

View file

@ -25,8 +25,8 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Platform::Platform (SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize) :
Placeable(parPos),
m_screenRatio(parSdlMain),
m_position(parPos),
m_size(parSize),
m_surface(parTexture)
{
@ -35,9 +35,9 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Platform::Platform (Platform&& parOther) :
Platform::Platform (Platform&& parOther) noexcept :
Placeable(parOther.GetPos()),
m_screenRatio(std::move(parOther.m_screenRatio)),
m_position(parOther.m_position),
m_size(parOther.m_size),
m_surface(parOther.m_surface)
{
@ -46,6 +46,17 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void Platform::Draw() const {
m_surface->Render(m_position, m_size, m_screenRatio.Ratio(), false);
m_surface->Render(TopLeft(), m_size, m_screenRatio.Ratio(), false);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Platform& Platform::operator= (const Platform& parOther) {
Drawable::operator= (parOther);
Placeable::operator= (parOther);
m_size = parOther.m_size;
m_surface = parOther.m_surface;
return *this;
}
} //namespace cloonel

View file

@ -22,28 +22,28 @@
#include "sizenotifiable.hpp"
#include "drawable.hpp"
#include "placeable.hpp"
namespace cloonel {
class Texture;
class SDLMain;
class Platform : public Drawable {
class Platform : public Drawable, public Placeable {
public:
Platform ( SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize );
Platform ( Platform&& parOther );
Platform ( Platform&& parOther ) noexcept;
Platform ( const Platform& ) = delete;
virtual ~Platform ( void ) noexcept = default;
Platform& operator= ( Platform&& parOther ) = delete;
Platform& operator= ( const Platform& parOther );
virtual void Draw ( void ) const;
const float2& TopLeft ( void ) const { return m_position; }
float2 BottomRight ( void ) const { return m_position + m_size; }
float2 TopLeft ( void ) const { return GetPos(); }
float2 BottomRight ( void ) const { return TopLeft() + m_size; }
private:
SizeNotifiable<regbehaviuors::AutoRegister> m_screenRatio;
float2 m_position;
const float2 m_size;
Texture* const m_surface;
float2 m_size;
Texture* m_surface;
};
} //namespace cloonel

View file

@ -22,57 +22,149 @@
#include "CloonelJumpConfig.h"
#include "texture.hpp"
#include "gameplayscene.hpp"
#include "mover.hpp"
#include "circularbuffer.hpp"
#include <ciso646>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <boost/noncopyable.hpp>
namespace cloonel {
namespace {
const uint32_t g_platfWidth = 104;
const uint32_t g_platfHeight = 25;
} //unnamed namespace
struct PlatformInfo {
PlatformInfo ( void ) :
platform(nullptr),
ticket(0)
{
}
PlatformInfo ( const PlatformInfo& ) = delete;
PlatformInfo ( Platform&& parPlatf ) noexcept :
platform(new Platform(std::move(parPlatf))),
ticket(0)
{
}
PlatformInfo ( PlatformInfo&& parOther ) noexcept :
platform(std::move(parOther.platform)),
ticket(parOther.ticket)
{
}
~PlatformInfo ( void ) noexcept = default;
PlatformInfo& operator= ( const PlatformInfo& ) = delete;
PlatformInfo& operator= ( PlatformInfo&& parOther ) {
platform.swap(parOther.platform);
std::swap(ticket, parOther.ticket);
return *this;
}
std::unique_ptr<Platform> platform;
Mover::PlaceableTicketType ticket;
};
struct PlatformSystem::LocalData : public boost::noncopyable {
LocalData ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover );
~LocalData ( void ) noexcept = default;
Mover& mover;
std::vector<PlatformInfo> platformsBuff;
CircularBuffer<std::vector<PlatformInfo>::iterator> platforms;
Texture texture;
SDLMain* const sdlmain;
GameplayScene* const scene;
const float maxDistance;
};
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene) :
m_sizeRatio(parSDLMain),
m_platforms(),
m_texture(new Texture(parTexturePath, parSDLMain, false)),
m_sdlmain(parSDLMain),
m_scene(parScene)
PlatformSystem::LocalData::LocalData (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover) :
mover(parMover),
platformsBuff(MAX_PLATFORMS_ON_SCREEN),
platforms(platformsBuff.begin(), platformsBuff.end()),
texture(parTexturePath, parSDLMain, false),
sdlmain(parSDLMain),
scene(parScene),
maxDistance(parMaxDistance)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover) :
m_localdata(new LocalData(parTexturePath, parSDLMain, parScene, parMaxDistance, parMover))
{
assert(m_sdlmain);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
PlatformSystem::~PlatformSystem() noexcept {
Destroy();
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void PlatformSystem::SpawnPlatforms() {
const int2 refWH(REFERENCE_WIDTH, REFERENCE_HEIGHT);
const int2 platfWH(110, 30);
if (m_platforms.empty()) {
const float2 newPos(static_cast<float>(std::rand() % (refWH.x() - platfWH.x())), static_cast<float>(std::rand() % (refWH.y() - platfWH.y())));
m_platforms.push(Platform(m_sdlmain, newPos, m_texture.get(), static_cast<float2>(platfWH)));
m_scene->AddDrawable(&m_platforms.back());
}
}
//const int2 refWH(REFERENCE_WIDTH, REFERENCE_HEIGHT);
//const int2 platfWH(g_platfWidth, g_platfHeight);
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void PlatformSystem::GarbageCollect() {
while (not m_platforms.empty() and m_platforms.front().TopLeft().y() <= 0.0f) {
m_platforms.pop();
}
// if (m_localdata.platforms.back().platform->TopLeft().y() <= 0.0f) {
// const float2 newPos(static_cast<float>(std::rand() % (refWH.x() - platfWH.x())), static_cast<float>(std::rand() % (refWH.y() - platfWH.y())));
//if (m_localdata->platforms.empty()) {
// const float2 newPos(static_cast<float>(std::rand() % (refWH.x() - platfWH.x())), static_cast<float>(std::rand() % (refWH.y() - platfWH.y())));
// m_localdata->platforms.push_back(Platform(m_localdata->sdlmain, newPos, &m_localdata->texture, static_cast<float2>(platfWH)));
// PlatformInfo& newPlatf = m_localdata->platforms.back();
// m_localdata->scene->AddDrawable(&newPlatf.platform);
// const Mover::PlaceableTicketType ticket = m_localdata->mover.RegisterPlaceable(&newPlatf.platform);
// newPlatf.ticket = ticket;
//}
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void PlatformSystem::Prepare() {
m_texture->Reload();
m_localdata->texture.Reload();
//Spawn the initial platforms
m_localdata->platforms.reset();
const size_t totalPlatf = m_localdata->platforms.capacity();
const auto totalPlatfFloatInv = 1.0f / static_cast<float>(totalPlatf);
const int2 platfWH(g_platfWidth, g_platfHeight);
for (size_t z = 0; z < totalPlatf; ++z) {
const float y = std::min(
static_cast<float>(REFERENCE_HEIGHT) - 25.0f,
static_cast<float>(z) * (static_cast<float>(REFERENCE_HEIGHT) * totalPlatfFloatInv)
);
const auto rndNum = std::rand() % (REFERENCE_WIDTH - platfWH.x());
const float2 newPos(static_cast<float>(rndNum), 100.0f + std::min(m_localdata->maxDistance, y));
m_localdata->platforms.push(Platform(m_localdata->sdlmain, newPos, &m_localdata->texture, static_cast<float2>(platfWH)));
}
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void PlatformSystem::Destroy() noexcept {
m_texture->Destroy();
m_localdata->texture.Destroy();
for (size_t z = 0; z < m_localdata->platforms.size(); ++z) {
m_localdata->mover.UnregisterPlaceable(m_localdata->platforms[z].ticket);
}
m_localdata->platforms.reset();
m_localdata->platformsBuff.clear();
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void PlatformSystem::AddDrawables() {
for (size_t z = 0; z < m_localdata->platforms.size(); ++z) {
PlatformInfo& newPlatf = m_localdata->platforms[z];
m_localdata->scene->AddDrawable(newPlatf.platform.get());
const Mover::PlaceableTicketType ticket = m_localdata->mover.RegisterPlaceable(newPlatf.platform.get());
newPlatf.ticket = ticket;
}
}
} //namespace cloonel

View file

@ -19,36 +19,31 @@
#ifndef id17908979556C47F8A978688BBE4A9D22
#include "sizenotifiable.hpp"
#include <queue>
#include <memory>
namespace cloonel {
class Platform;
class SDLMain;
class Texture;
class GameplayScene;
class Mover;
class PlatformSystem {
public:
PlatformSystem ( void ) = delete;
PlatformSystem ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene );
PlatformSystem ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover );
PlatformSystem ( const PlatformSystem& ) = delete;
PlatformSystem ( PlatformSystem&& parOther ) = delete;
~PlatformSystem ( void ) noexcept;
PlatformSystem& operator= ( const PlatformSystem& ) = delete;
void Prepare ( void );
void AddDrawables ( void );
void Destroy ( void ) noexcept;
void SpawnPlatforms ( void );
void GarbageCollect ( void );
private:
SizeNotifiable<regbehaviuors::AutoRegister> m_sizeRatio;
std::queue<Platform> m_platforms;
const std::unique_ptr<Texture> m_texture;
SDLMain* const m_sdlmain;
GameplayScene* const m_scene;
struct LocalData;
const std::unique_ptr<LocalData> m_localdata;
};
} //namespace cloonel