From d5d75b034e0c61caf25782961f051977375e31d3 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 6 Aug 2014 16:04:17 +0200 Subject: [PATCH] Generate new platforms as you climb. This is actually broken as platforms never unregister themselves. An upcoming refactoring will change things, so registering/unregistering will not be necessary anymore. --- src/platformsystem.cpp | 48 ++++++++++++++++++------------------------ src/platformsystem.hpp | 1 + 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/platformsystem.cpp b/src/platformsystem.cpp index 932abab..c76b15d 100644 --- a/src/platformsystem.cpp +++ b/src/platformsystem.cpp @@ -115,7 +115,8 @@ namespace cloonel { ///-------------------------------------------------------------------------- PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance) : Placeable(float2(0.0f)), - m_localdata(new LocalData(parTexturePath, parSDLMain, parScene, parMaxDistance)) + m_localdata(new LocalData(parTexturePath, parSDLMain, parScene, parMaxDistance)), + m_targetAverage(parMaxDistance / 5.25f) //TODO: change this value to make up for the difficulty { } @@ -128,21 +129,27 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void PlatformSystem::SpawnPlatforms() { - //const int2 refWH(REFERENCE_WIDTH, REFERENCE_HEIGHT); - //const int2 platfWH(g_platfWidth, g_platfHeight); + const size_t totalPlatf = MAX_PLATFORMS_ON_SCREEN; + const float2 platfWH(static_cast(g_platfWidth), static_cast(g_platfHeight)); - // if (m_localdata.platforms.back().platform->TopLeft().y() <= 0.0f) { - // const float2 newPos(static_cast(std::rand() % (refWH.x() - platfWH.x())), static_cast(std::rand() % (refWH.y() - platfWH.y()))); + size_t freePlatforms = 0; + for (size_t z = 0; z < m_localdata->platforms.size(); ++z) { + if (m_localdata->platforms[z].platform->GetPos().y() < 0.0f) + ++freePlatforms; + else + break; + } + freePlatforms = totalPlatf - m_localdata->platforms.size() + freePlatforms; - //if (m_localdata->platforms.empty()) { - // const float2 newPos(static_cast(std::rand() % (refWH.x() - platfWH.x())), static_cast(std::rand() % (refWH.y() - platfWH.y()))); - // m_localdata->platforms.push_back(Platform(m_localdata->sdlmain, newPos, &m_localdata->texture, static_cast(platfWH))); + float prevPlatf = (m_localdata->platforms.empty() ? 0.0f : m_localdata->platforms.back().platform->GetPos().y() + GetPos().y()); + while (static_cast(REFERENCE_HEIGHT) - prevPlatf >= m_targetAverage) { + assert(freePlatforms > 0); + const auto newPos(PositionForNewPlatf(prevPlatf, m_targetAverage, m_localdata->maxDistance, freePlatforms)); + --freePlatforms; + prevPlatf = newPos.y(); - // 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; - //} + m_localdata->platforms.push_back(Platform(m_localdata->sdlmain, newPos, &m_localdata->texture, platfWH)); + } } ///-------------------------------------------------------------------------- @@ -152,20 +159,7 @@ namespace cloonel { //Spawn the initial platforms m_localdata->platforms.clear(); - const size_t totalPlatf = MAX_PLATFORMS_ON_SCREEN; - //const auto totalPlatfFloatInv = 1.0f / static_cast(totalPlatf); - const float2 platfWH(static_cast(g_platfWidth), static_cast(g_platfHeight)); - float prevPlatf = 0.0f; - const float averageTarget = m_localdata->maxDistance / 5.25f; //TODO: change this value to make up for the difficulty - for (size_t z = 0; z < totalPlatf; ++z) { - const auto newPos(PositionForNewPlatf(prevPlatf, averageTarget, m_localdata->maxDistance, totalPlatf - z)); - prevPlatf = newPos.y(); - - m_localdata->platforms.push_back(Platform(m_localdata->sdlmain, newPos, &m_localdata->texture, platfWH)); - - if (static_cast(REFERENCE_HEIGHT) - newPos.y() < averageTarget) - break; - } + this->SpawnPlatforms(); } ///-------------------------------------------------------------------------- diff --git a/src/platformsystem.hpp b/src/platformsystem.hpp index eee846a..5002812 100644 --- a/src/platformsystem.hpp +++ b/src/platformsystem.hpp @@ -52,6 +52,7 @@ namespace cloonel { const std::unique_ptr m_localdata; ColliderRegisterFunc m_registerToCollider; ColliderUnregisterFunc m_unregisterFromCollider; + float m_targetAverage; }; } //namespace cloonel