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/moversine.cpp
src/gameplaysceneclassic.cpp
src/moverrelative.cpp
)
target_link_libraries(${PROJECT_NAME}

View file

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

View file

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

View file

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

View file

@ -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<Placeable*> m_placeables;
};
} //namespace cloonel

View file

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

View file

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

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"
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <cmath>
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<float>(M_PI) * 2.0f;
m_alpha += parDelta;
if (m_alpha >= pitwo)
m_alpha -= pitwo;
}
} //namespace cloonel

View file

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

View file

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

View file

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