Movers moved into a separate directory.

This commit is contained in:
King_DuckZ 2014-07-05 18:48:04 +02:00
parent 9a8367c00c
commit 976d34f17e
11 changed files with 6 additions and 5 deletions

33
src/movers/mover.cpp Normal file
View file

@ -0,0 +1,33 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mover.hpp"
#include "placeable.hpp"
#include <cassert>
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void Mover::Update (float parDelta) {
ApplyMotion(parDelta);
for (auto currPlaceable : m_placeables) {
UpdateSingle(currPlaceable);
}
}
} //namespace cloonel

52
src/movers/mover.hpp Normal file
View file

@ -0,0 +1,52 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id20409B43E62B4247A278B413D1114896
#define id20409B43E62B4247A278B413D1114896
#include "vector.hpp"
#include "observersmanager.hpp"
namespace cloonel {
class Placeable;
class Mover {
public:
typedef ObserversManager<Placeable*>::TicketType PlaceableTicketType;
Mover ( void ) = default;
virtual ~Mover ( void ) noexcept = default;
virtual void Update ( float parDelta );
PlaceableTicketType RegisterPlaceable ( Placeable* parPlaceable ) { return m_placeables.Add(parPlaceable); }
void UnregisterPlaceable ( PlaceableTicketType parID ) noexcept { m_placeables.Remove(parID); }
protected:
std::size_t PlaceableCount ( void ) const { return m_placeables.size(); }
private:
virtual void ApplyMotion ( float parDelta ) = 0;
virtual void UpdateSingle ( Placeable* parPlaceable ) = 0;
void UpdateAll ( float parDelta );
ObserversManager<Placeable*> m_placeables;
};
} //namespace cloonel
#endif

View file

@ -0,0 +1,81 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "moverleftright.hpp"
#include <algorithm>
#include <cmath>
namespace cloonel {
namespace {
float Clamp ( float parValue, float parMin, float parMax ) __attribute__((pure));
float Clamp ( float parValue, float parMin, float parMax ) {
assert(parMin <= parMax);
return std::max(parMin, std::min(parMax, parValue));
}
} //unnamed namespace
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
MoverLeftRight::MoverLeftRight (float parMaxVelocity, float parMass, float parForce) :
//TODO: mass does not belong to here
m_velocity(0.0f),
m_maxVelocity(parMaxVelocity),
m_invMass(1.0f / parMass),
m_force(parForce),
m_movementTarget(0.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverLeftRight::ApplyMotion (float parDelta) {
//TODO: implement a movement that is more mathematically correct and
//that feels nice.
//const float forceDirection = std::abs(m_movementTarget) * 2.0f - 1.0f;
const float notMoving = 1.0f - std::abs(m_movementTarget);
const float friction = 1.757f * m_velocity * notMoving * parDelta;
m_velocity += m_movementTarget * m_force * m_invMass * parDelta - std::copysign(friction, m_velocity);
m_velocity = Clamp(m_velocity, -m_maxVelocity, m_maxVelocity);
if (m_movementTarget == 0.0f and std::abs(m_velocity) < 0.1f)
m_velocity = 0.0f;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
float2 MoverLeftRight::GetOffset() const {
return float2(m_velocity, 0.0f);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverLeftRight::SetMovement (MovementDirectionType parDirection) {
switch (parDirection) {
case MovementDirection_Left:
m_movementTarget = -1.0f;
break;
case MovementDirection_Right:
m_movementTarget = 1.0f;
break;
case MovementDirection_Still:
m_movementTarget = 0.0f;
break;
}
}
} //namespace cloonel

View file

@ -0,0 +1,51 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id340A16673C014E0BBFE8AC24D8FC1C77
#define id340A16673C014E0BBFE8AC24D8FC1C77
#include "moveroneshot.hpp"
namespace cloonel {
class MoverLeftRight : public MoverOneShot {
public:
enum MovementDirectionType {
MovementDirection_Left,
MovementDirection_Right,
MovementDirection_Still
};
MoverLeftRight ( float parMaxVelocity, float parMass, float parForce );
virtual ~MoverLeftRight ( void ) noexcept = default;
void SetMovement ( MovementDirectionType parDirection );
private:
virtual float2 GetOffset ( void ) const;
virtual void ApplyMotion ( float parDelta );
float m_velocity;
float m_maxVelocity;
float m_invMass;
float m_force;
float m_movementTarget;
};
} //namespace cloonel
#endif

View file

@ -0,0 +1,30 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "moveroneshot.hpp"
#include "placeable.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverOneShot::UpdateSingle (Placeable* parPlaceable) {
const float2 offs(GetOffset());
parPlaceable->AddOffset(offs);
}
} //namespace cloonel

View file

@ -0,0 +1,44 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id9CDCF10D483641A2845353C0835F93EC
#define id9CDCF10D483641A2845353C0835F93EC
//Move a Placeable by issuind an offset.
//The offset is applied to the current position of the Placeable you are
//moving.
//For example issuing 1, then 2 will put your Placeable in position 3,
//assuming it started at 0.
#include "vector.hpp"
#include "mover.hpp"
namespace cloonel {
class MoverOneShot : public Mover {
public:
MoverOneShot ( void ) = default;
virtual ~MoverOneShot ( void ) noexcept = default;
private:
virtual void UpdateSingle ( Placeable* parPlaceable );
virtual float2 GetOffset ( void ) const = 0;
};
} //namespace cloonel
#endif

View file

@ -0,0 +1,44 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "moverrelative.hpp"
#include "placeable.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
MoverRelative::MoverRelative() :
m_prevOffset(0.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverRelative::UpdateSingle (Placeable* parPlaceable) {
const float2 offs(GetOffset() - m_prevOffset);
parPlaceable->AddOffset(offs);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverRelative::Update (float parDelta) {
m_prevOffset = GetOffset();
Mover::Update(parDelta);
}
} //namespace cloonel

View file

@ -0,0 +1,48 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idBAA1271995604A659EF3FEC4B8FC3870
#define idBAA1271995604A659EF3FEC4B8FC3870
//Move a Placeable by issuing an offset relative to a base position.
//Use this if for example your Placeable is following a mathematical formula or
//if you're giving a position you want it to be at any specific time.
//For example issuing 1, then 2 will put your Placeable in position 2,
//assuming it started at 0.
#include "mover.hpp"
namespace cloonel {
class MoverRelative : public Mover {
public:
MoverRelative ( void );
virtual ~MoverRelative ( void ) noexcept = default;
virtual void Update ( float parDelta );
protected:
virtual void UpdateSingle ( Placeable* parPlaceable );
virtual float2 GetOffset ( void ) const = 0;
private:
float2 m_prevOffset;
};
} //namespace cloonel
#endif

50
src/movers/moversine.cpp Normal file
View file

@ -0,0 +1,50 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "moversine.hpp"
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
#include <cmath>
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
MoverSine::MoverSine() :
MoverRelative(),
m_alpha(0.0f),
m_power(1.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
float2 MoverSine::GetOffset() const {
return float2(0.0f, std::abs(std::sin(m_alpha)) * m_power);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverSine::ApplyMotion (float parDelta) {
const float pitwo = static_cast<float>(M_PI) * 2.0f;
m_alpha += parDelta * 2.6f;
if (m_alpha >= pitwo)
m_alpha -= pitwo;
}
} //namespace cloonel

42
src/movers/moversine.hpp Normal file
View file

@ -0,0 +1,42 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id16A9347A373E4144A9C2B98E7D7A1351
#define id16A9347A373E4144A9C2B98E7D7A1351
#include "moverrelative.hpp"
namespace cloonel {
class MoverSine : public MoverRelative {
public:
MoverSine ( void );
~MoverSine ( void ) noexcept = default;
void SetPower ( float parPower ) noexcept { m_power = parPower; }
private:
virtual void ApplyMotion ( float parDelta );
virtual float2 GetOffset() const;
float m_alpha;
float m_power;
};
} //namespace cloonel
#endif