From 9c660caec6ba057be9af6be58c46b630618663a2 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 10 Jul 2014 20:00:54 +0200 Subject: [PATCH] Use the new tree-based registration system in the mover. --- src/gameplaysceneclassic.cpp | 5 +++-- src/movers/mover.cpp | 9 +++++++++ src/movers/mover.hpp | 6 +++++- src/placeable.cpp | 6 ++++++ src/placeable.hpp | 4 ++-- src/platformsystem.cpp | 22 ++++++++++++++-------- src/platformsystem.hpp | 9 ++++++--- 7 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 230f7e7..5ccbfc4 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -68,16 +68,17 @@ namespace cloonel { std::unique_ptr moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f)); std::unique_ptr moverWorld(new MoverWorld(halfRefHeight)); std::unique_ptr wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject())); - std::unique_ptr platforms(new PlatformSystem("resources/graphics/platform.png", SDLObject(), this, halfRefHeight * 0.9f, *moverWorld)); + std::unique_ptr platforms(new PlatformSystem("resources/graphics/platform.png", SDLObject(), this, halfRefHeight * 0.9f)); player->Prepare(); + platforms->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 + moverWorld->RegisterPlaceable(platforms.get()); wallpaper->Reload(); - platforms->Prepare(); std::swap(moverSine, m_moverSine); std::swap(player, m_player); diff --git a/src/movers/mover.cpp b/src/movers/mover.cpp index 5fd0381..c4a7875 100644 --- a/src/movers/mover.cpp +++ b/src/movers/mover.cpp @@ -30,4 +30,13 @@ namespace cloonel { UpdateSingle(currPlaceable); } } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Mover::PlaceableTicketType Mover::RegisterPlaceable (Placeable* parPlaceable, PlaceableTicketType parTicket) { + assert(parPlaceable); + const auto currTicket = m_placeables.Add(parPlaceable, parTicket); + parPlaceable->OnRegister(*this, currTicket); + return currTicket; + } } //namespace cloonel diff --git a/src/movers/mover.hpp b/src/movers/mover.hpp index 075a4cf..7da8776 100644 --- a/src/movers/mover.hpp +++ b/src/movers/mover.hpp @@ -30,11 +30,15 @@ namespace cloonel { public: typedef ObserversManager::TicketType PlaceableTicketType; + enum { + NullTicket = ObserversManager::Ticket_Null + }; + Mover ( void ) = default; virtual ~Mover ( void ) noexcept = default; virtual void Update ( float parDelta ); - PlaceableTicketType RegisterPlaceable ( Placeable* parPlaceable ) { return m_placeables.Add(parPlaceable); } + PlaceableTicketType RegisterPlaceable ( Placeable* parPlaceable, PlaceableTicketType parParent=ObserversManager::Ticket_Null ); void UnregisterPlaceable ( PlaceableTicketType parID ) noexcept { m_placeables.Remove(parID); } protected: diff --git a/src/placeable.cpp b/src/placeable.cpp index 45c6c26..a196317 100644 --- a/src/placeable.cpp +++ b/src/placeable.cpp @@ -28,4 +28,10 @@ namespace cloonel { m_pos(parPos) { } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Placeable::OnRegister (Mover&, Mover::PlaceableTicketType) { + return; + } } //namespace cloonel diff --git a/src/placeable.hpp b/src/placeable.hpp index 3e82c23..1969160 100644 --- a/src/placeable.hpp +++ b/src/placeable.hpp @@ -21,14 +21,14 @@ #define id703E4B8DFFF747DFA97864384B87E9C1 #include "vector.hpp" +#include "mover.hpp" namespace cloonel { - class Mover; - class Placeable { public: float2 GetPos ( void ) const noexcept; void AddOffset ( const float2& parOffset ) noexcept; + virtual void OnRegister ( Mover& parMover, Mover::PlaceableTicketType parParentTicket ); protected: explicit Placeable ( float2 parPos ); diff --git a/src/platformsystem.cpp b/src/platformsystem.cpp index 066b1e9..c130d3a 100644 --- a/src/platformsystem.cpp +++ b/src/platformsystem.cpp @@ -66,10 +66,9 @@ namespace cloonel { }; struct PlatformSystem::LocalData : public boost::noncopyable { - LocalData ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover ); + LocalData ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance ); ~LocalData ( void ) noexcept = default; - Mover& mover; std::vector platformsBuff; CircularBuffer::iterator> platforms; Texture texture; @@ -80,8 +79,7 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - PlatformSystem::LocalData::LocalData (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover) : - mover(parMover), + PlatformSystem::LocalData::LocalData (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance) : platformsBuff(MAX_PLATFORMS_ON_SCREEN), platforms(platformsBuff.begin(), platformsBuff.end()), texture(parTexturePath, parSDLMain, false), @@ -93,8 +91,9 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover) : - m_localdata(new LocalData(parTexturePath, parSDLMain, parScene, parMaxDistance, parMover)) + PlatformSystem::PlatformSystem (const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance) : + Placeable(float2(0.0f)), + m_localdata(new LocalData(parTexturePath, parSDLMain, parScene, parMaxDistance)) { } @@ -163,8 +162,15 @@ namespace cloonel { 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; + newPlatf.ticket = Mover::NullTicket; + } + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void PlatformSystem::OnRegister (Mover& parMover, Mover::PlaceableTicketType parTicket) { + for (size_t z = 0; z < m_localdata->platforms.size(); ++z) { + m_localdata->platforms[z].ticket = parMover.RegisterPlaceable(m_localdata->platforms[z].platform.get(), parTicket); } } } //namespace cloonel diff --git a/src/platformsystem.hpp b/src/platformsystem.hpp index 7b6324e..2c2af45 100644 --- a/src/platformsystem.hpp +++ b/src/platformsystem.hpp @@ -19,17 +19,17 @@ #ifndef id17908979556C47F8A978688BBE4A9D22 +#include "placeable.hpp" #include namespace cloonel { class SDLMain; class GameplayScene; - class Mover; - class PlatformSystem { + class PlatformSystem : public Placeable { public: PlatformSystem ( void ) = delete; - PlatformSystem ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance, Mover& parMover ); + PlatformSystem ( const char* parTexturePath, SDLMain* parSDLMain, GameplayScene* parScene, float parMaxDistance ); PlatformSystem ( const PlatformSystem& ) = delete; PlatformSystem ( PlatformSystem&& parOther ) = delete; ~PlatformSystem ( void ) noexcept; @@ -40,6 +40,9 @@ namespace cloonel { void Destroy ( void ) noexcept; void SpawnPlatforms ( void ); + //Overrides + virtual void OnRegister ( Mover& parOut, Mover::PlaceableTicketType parTicket ); + private: struct LocalData;