diff --git a/CMakeLists.txt b/CMakeLists.txt index 99e6b02..74a506a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ add_executable(${PROJECT_NAME} src/moveroneshot.cpp src/moversine.cpp src/gameplaysceneclassic.cpp + src/moverrelative.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/gameplayscene.cpp b/src/gameplayscene.cpp index 9e5e442..a8c483d 100644 --- a/src/gameplayscene.cpp +++ b/src/gameplayscene.cpp @@ -14,7 +14,7 @@ namespace cloonel { ///-------------------------------------------------------------------------- void GameplayScene::OnUpdate (float parDelta) { for (auto itMover : m_movers) { - itMover->ApplyMotion(parDelta); + itMover->Update(parDelta); } } diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 932b0c9..4b83eab 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -1,6 +1,7 @@ #include "gameplaysceneclassic.hpp" #include "character.hpp" #include "moversine.hpp" +#include "sdlmain.hpp" #include namespace cloonel { @@ -35,6 +36,8 @@ namespace cloonel { AddMover(m_moverSine.get()); AddDrawable(m_player.get()); + + m_moverSine->SetPower(static_cast(SDLObject()->DefWidthHeight().y() / 2)); } ///-------------------------------------------------------------------------- diff --git a/src/mover.cpp b/src/mover.cpp index 7316074..0efb478 100644 --- a/src/mover.cpp +++ b/src/mover.cpp @@ -5,11 +5,10 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void Mover::ApplyOffsetToPlaceables (const float2& parOffset) { - for (Placeable* currPlaceable : m_placeables) { - if (currPlaceable) { - currPlaceable->AddOffset(parOffset); - } + void Mover::Update (float parDelta) { + ApplyMotion(parDelta); + for (auto currPlaceable : m_placeables) { + UpdateSingle(currPlaceable); } } } //namespace cloonel diff --git a/src/mover.hpp b/src/mover.hpp index 394c7c5..2dd1f1a 100644 --- a/src/mover.hpp +++ b/src/mover.hpp @@ -14,16 +14,18 @@ namespace cloonel { Mover ( void ) = default; virtual ~Mover ( void ) noexcept = default; - virtual void ApplyMotion ( float parDelta ) = 0; + void Update ( float parDelta ); PlaceableTicketType RegisterPlaceable ( Placeable* parPlaceable ) { return m_placeables.Add(parPlaceable); } void UnregisterPlaceable ( PlaceableTicketType parID ) noexcept { m_placeables.Remove(parID); } protected: - virtual void Update ( float parDelta ) = 0; std::size_t PlaceableCount ( void ) const { return m_placeables.size(); } - void ApplyOffsetToPlaceables ( const float2& parOffset ); private: + virtual void ApplyMotion ( float parDelta ) = 0; + virtual void UpdateSingle ( Placeable* parPlaceable ) = 0; + void UpdateAll ( float parDelta ); + ObserversManager m_placeables; }; } //namespace cloonel diff --git a/src/moveroneshot.cpp b/src/moveroneshot.cpp index 0db1d5e..47bcde4 100644 --- a/src/moveroneshot.cpp +++ b/src/moveroneshot.cpp @@ -1,11 +1,11 @@ #include "moveroneshot.hpp" +#include "placeable.hpp" namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void MoverOneShot::ApplyMotion (float parDelta) { - Update(parDelta); + void MoverOneShot::UpdateSingle (Placeable* parPlaceable) { const float2 offs(GetOffset()); - ApplyOffsetToPlaceables(offs); + parPlaceable->AddOffset(offs); } } //namespace cloonel diff --git a/src/moveroneshot.hpp b/src/moveroneshot.hpp index 75f1f14..21b35d7 100644 --- a/src/moveroneshot.hpp +++ b/src/moveroneshot.hpp @@ -10,9 +10,8 @@ namespace cloonel { MoverOneShot ( void ) = default; virtual ~MoverOneShot ( void ) noexcept = default; - virtual void ApplyMotion ( float parDelta ); - private: + virtual void UpdateSingle ( Placeable* parPlaceable ); virtual float2 GetOffset ( void ) const = 0; }; } //namespace cloonel diff --git a/src/moverrelative.cpp b/src/moverrelative.cpp new file mode 100644 index 0000000..b74ed31 --- /dev/null +++ b/src/moverrelative.cpp @@ -0,0 +1,11 @@ +#include "moverrelative.hpp" +#include "placeable.hpp" + +namespace cloonel { + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void MoverRelative::UpdateSingle (Placeable* parPlaceable) { + const float2 newPos(GetOffset() - parPlaceable->QueryPosition()); + parPlaceable->AddOffset(newPos); + } +} //namespace cloonel diff --git a/src/moverrelative.hpp b/src/moverrelative.hpp new file mode 100644 index 0000000..cb3642d --- /dev/null +++ b/src/moverrelative.hpp @@ -0,0 +1,18 @@ +#ifndef idBAA1271995604A659EF3FEC4B8FC3870 +#define idBAA1271995604A659EF3FEC4B8FC3870 + +#include "mover.hpp" + +namespace cloonel { + class MoverRelative : public Mover { + public: + MoverRelative ( void ) = default; + virtual ~MoverRelative ( void ) noexcept = default; + + protected: + virtual void UpdateSingle ( Placeable* parPlaceable ); + virtual float2 GetOffset ( void ) const = 0; + }; +} //namespace cloonel + +#endif diff --git a/src/moversine.cpp b/src/moversine.cpp index 13bec7a..d376610 100644 --- a/src/moversine.cpp +++ b/src/moversine.cpp @@ -1,11 +1,14 @@ #include "moversine.hpp" +#if !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif #include namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- MoverSine::MoverSine() : - MoverOneShot(), + MoverRelative(), m_alpha(0.0f), m_power(1.0f) { @@ -14,12 +17,15 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- float2 MoverSine::GetOffset() const { - return float2(std::sin(m_alpha) * m_power); + return float2(0.0f, std::abs(std::sin(m_alpha)) * m_power); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void MoverSine::Update (float parDelta) { + void MoverSine::ApplyMotion (float parDelta) { + const float pitwo = static_cast(M_PI) * 2.0f; m_alpha += parDelta; + if (m_alpha >= pitwo) + m_alpha -= pitwo; } } //namespace cloonel diff --git a/src/moversine.hpp b/src/moversine.hpp index f164636..232c059 100644 --- a/src/moversine.hpp +++ b/src/moversine.hpp @@ -1,18 +1,18 @@ #ifndef id16A9347A373E4144A9C2B98E7D7A1351 #define id16A9347A373E4144A9C2B98E7D7A1351 -#include "moveroneshot.hpp" +#include "moverrelative.hpp" namespace cloonel { - class MoverSine : public MoverOneShot { + class MoverSine : public MoverRelative { public: MoverSine ( void ); ~MoverSine ( void ) noexcept = default; - protected: - virtual void Update ( float parDelta ); + void SetPower ( float parPower ) noexcept { m_power = parPower; } private: + virtual void ApplyMotion ( float parDelta ); virtual float2 GetOffset() const; float m_alpha; diff --git a/src/placeable.cpp b/src/placeable.cpp index b6cb316..3e3e139 100644 --- a/src/placeable.cpp +++ b/src/placeable.cpp @@ -22,7 +22,7 @@ namespace cloonel { m_mover = nullptr; } if (parMover) { - m_idForMover = parMover->RegisterPlaceable(this); + m_idForMover = static_cast(parMover->RegisterPlaceable(this)); m_mover = parMover; } } diff --git a/src/placeable.hpp b/src/placeable.hpp index c4251ed..3874011 100644 --- a/src/placeable.hpp +++ b/src/placeable.hpp @@ -9,6 +9,7 @@ namespace cloonel { class Placeable { public: const float2& GetPos ( void ) const noexcept { return m_pos; } + const float2& QueryPosition ( void ) const noexcept { return m_pos; } void AddOffset ( const float2& parOffset ) noexcept { m_pos += parOffset; } void SwapMover ( Mover* parMover );