Refactoring in Movers.

The displayed texture now bounces vertically.
This commit is contained in:
King_DuckZ 2014-02-22 13:13:49 +01:00
parent 3269b469e3
commit 85c65b3e68
13 changed files with 62 additions and 22 deletions

View file

@ -44,6 +44,7 @@ add_executable(${PROJECT_NAME}
src/moveroneshot.cpp src/moveroneshot.cpp
src/moversine.cpp src/moversine.cpp
src/gameplaysceneclassic.cpp src/gameplaysceneclassic.cpp
src/moverrelative.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}

View file

@ -14,7 +14,7 @@ namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void GameplayScene::OnUpdate (float parDelta) { void GameplayScene::OnUpdate (float parDelta) {
for (auto itMover : m_movers) { for (auto itMover : m_movers) {
itMover->ApplyMotion(parDelta); itMover->Update(parDelta);
} }
} }

View file

@ -1,6 +1,7 @@
#include "gameplaysceneclassic.hpp" #include "gameplaysceneclassic.hpp"
#include "character.hpp" #include "character.hpp"
#include "moversine.hpp" #include "moversine.hpp"
#include "sdlmain.hpp"
#include <algorithm> #include <algorithm>
namespace cloonel { namespace cloonel {
@ -35,6 +36,8 @@ namespace cloonel {
AddMover(m_moverSine.get()); AddMover(m_moverSine.get());
AddDrawable(m_player.get()); AddDrawable(m_player.get());
m_moverSine->SetPower(static_cast<float>(SDLObject()->DefWidthHeight().y() / 2));
} }
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------

View file

@ -5,11 +5,10 @@
namespace cloonel { namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void Mover::ApplyOffsetToPlaceables (const float2& parOffset) { void Mover::Update (float parDelta) {
for (Placeable* currPlaceable : m_placeables) { ApplyMotion(parDelta);
if (currPlaceable) { for (auto currPlaceable : m_placeables) {
currPlaceable->AddOffset(parOffset); UpdateSingle(currPlaceable);
}
} }
} }
} //namespace cloonel } //namespace cloonel

View file

@ -14,16 +14,18 @@ namespace cloonel {
Mover ( void ) = default; Mover ( void ) = default;
virtual ~Mover ( void ) noexcept = 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); } PlaceableTicketType RegisterPlaceable ( Placeable* parPlaceable ) { return m_placeables.Add(parPlaceable); }
void UnregisterPlaceable ( PlaceableTicketType parID ) noexcept { m_placeables.Remove(parID); } void UnregisterPlaceable ( PlaceableTicketType parID ) noexcept { m_placeables.Remove(parID); }
protected: protected:
virtual void Update ( float parDelta ) = 0;
std::size_t PlaceableCount ( void ) const { return m_placeables.size(); } std::size_t PlaceableCount ( void ) const { return m_placeables.size(); }
void ApplyOffsetToPlaceables ( const float2& parOffset );
private: private:
virtual void ApplyMotion ( float parDelta ) = 0;
virtual void UpdateSingle ( Placeable* parPlaceable ) = 0;
void UpdateAll ( float parDelta );
ObserversManager<Placeable*> m_placeables; ObserversManager<Placeable*> m_placeables;
}; };
} //namespace cloonel } //namespace cloonel

View file

@ -1,11 +1,11 @@
#include "moveroneshot.hpp" #include "moveroneshot.hpp"
#include "placeable.hpp"
namespace cloonel { namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void MoverOneShot::ApplyMotion (float parDelta) { void MoverOneShot::UpdateSingle (Placeable* parPlaceable) {
Update(parDelta);
const float2 offs(GetOffset()); const float2 offs(GetOffset());
ApplyOffsetToPlaceables(offs); parPlaceable->AddOffset(offs);
} }
} //namespace cloonel } //namespace cloonel

View file

@ -10,9 +10,8 @@ namespace cloonel {
MoverOneShot ( void ) = default; MoverOneShot ( void ) = default;
virtual ~MoverOneShot ( void ) noexcept = default; virtual ~MoverOneShot ( void ) noexcept = default;
virtual void ApplyMotion ( float parDelta );
private: private:
virtual void UpdateSingle ( Placeable* parPlaceable );
virtual float2 GetOffset ( void ) const = 0; virtual float2 GetOffset ( void ) const = 0;
}; };
} //namespace cloonel } //namespace cloonel

11
src/moverrelative.cpp Normal file
View file

@ -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

18
src/moverrelative.hpp Normal file
View file

@ -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

View file

@ -1,11 +1,14 @@
#include "moversine.hpp" #include "moversine.hpp"
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <cmath> #include <cmath>
namespace cloonel { namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
MoverSine::MoverSine() : MoverSine::MoverSine() :
MoverOneShot(), MoverRelative(),
m_alpha(0.0f), m_alpha(0.0f),
m_power(1.0f) m_power(1.0f)
{ {
@ -14,12 +17,15 @@ namespace cloonel {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
float2 MoverSine::GetOffset() const { 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<float>(M_PI) * 2.0f;
m_alpha += parDelta; m_alpha += parDelta;
if (m_alpha >= pitwo)
m_alpha -= pitwo;
} }
} //namespace cloonel } //namespace cloonel

View file

@ -1,18 +1,18 @@
#ifndef id16A9347A373E4144A9C2B98E7D7A1351 #ifndef id16A9347A373E4144A9C2B98E7D7A1351
#define id16A9347A373E4144A9C2B98E7D7A1351 #define id16A9347A373E4144A9C2B98E7D7A1351
#include "moveroneshot.hpp" #include "moverrelative.hpp"
namespace cloonel { namespace cloonel {
class MoverSine : public MoverOneShot { class MoverSine : public MoverRelative {
public: public:
MoverSine ( void ); MoverSine ( void );
~MoverSine ( void ) noexcept = default; ~MoverSine ( void ) noexcept = default;
protected: void SetPower ( float parPower ) noexcept { m_power = parPower; }
virtual void Update ( float parDelta );
private: private:
virtual void ApplyMotion ( float parDelta );
virtual float2 GetOffset() const; virtual float2 GetOffset() const;
float m_alpha; float m_alpha;

View file

@ -22,7 +22,7 @@ namespace cloonel {
m_mover = nullptr; m_mover = nullptr;
} }
if (parMover) { if (parMover) {
m_idForMover = parMover->RegisterPlaceable(this); m_idForMover = static_cast<int>(parMover->RegisterPlaceable(this));
m_mover = parMover; m_mover = parMover;
} }
} }

View file

@ -9,6 +9,7 @@ namespace cloonel {
class Placeable { class Placeable {
public: public:
const float2& GetPos ( void ) const noexcept { return m_pos; } 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 AddOffset ( const float2& parOffset ) noexcept { m_pos += parOffset; }
void SwapMover ( Mover* parMover ); void SwapMover ( Mover* parMover );