Bouncing works but going too high causes an assertion.
This commit also reduces the jump size and the character's size.
This commit is contained in:
parent
df8f5ca6b2
commit
164030202c
9 changed files with 82 additions and 7 deletions
|
@ -21,7 +21,7 @@
|
|||
#define id5FC1D6EEF9DF41E790068FBC6753035F
|
||||
|
||||
#define GameName "@PROJECT_NAME@"
|
||||
#define GameVersionMinor 11
|
||||
#define GameVersionMinor 12
|
||||
#define GameVersionMajor 0
|
||||
|
||||
#define REFERENCE_WIDTH 480
|
||||
|
@ -32,6 +32,8 @@
|
|||
#define MAX_PLATFORMS_ON_SCREEN 24
|
||||
#define REFERENCE_PLATFORM_WIDTH 104
|
||||
#define REFERENCE_PLATFORM_HEIGHT 25
|
||||
#define REFERENCE_CHARA_WIDTH 55
|
||||
#define REFERENCE_CHARA_HEIGHT 70
|
||||
|
||||
/* TODO: make this path relative */
|
||||
#define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@"
|
||||
|
|
|
@ -21,9 +21,15 @@
|
|||
#include "sdlmain.hpp"
|
||||
#include "texture.hpp"
|
||||
#include "collider.hpp"
|
||||
#include "line.hpp"
|
||||
#include <cassert>
|
||||
|
||||
namespace cloonel {
|
||||
namespace {
|
||||
void DoNothing (const Line<float, 2>&, const float2&) {
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) :
|
||||
|
@ -31,9 +37,11 @@ namespace cloonel {
|
|||
Drawable(parSize),
|
||||
m_bottomBar(float2(0.0f), parSize.x()),
|
||||
m_screenRatio(parMain),
|
||||
m_bounceCallback(&DoNothing),
|
||||
m_texture(new Texture(parPath, parMain, false))
|
||||
{
|
||||
assert(parMain);
|
||||
m_bottomBar.SetCallback(std::bind(&Character::OnBounce, this, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
|
@ -43,9 +51,11 @@ namespace cloonel {
|
|||
Drawable(parSize),
|
||||
m_bottomBar(float2(0.0f), parSize.x()),
|
||||
m_screenRatio(parMain),
|
||||
m_bounceCallback(&DoNothing),
|
||||
m_texture(new Texture(parPath, parMain, false))
|
||||
{
|
||||
assert(parMain);
|
||||
m_bottomBar.SetCallback(std::bind(&Character::OnBounce, this, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
|
@ -85,4 +95,21 @@ namespace cloonel {
|
|||
void Character::OnRegister (Mover& parMover, Mover::PlaceableTicketType parParentTicket) {
|
||||
parMover.RegisterPlaceable(&m_bottomBar, parParentTicket);
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
void Character::SetOnBounceCallback (BounceCallbackType parCallb) {
|
||||
m_bounceCallback = parCallb;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
void Character::OnBounce (const Line<float, 2>& parCollision, const float2& parDirection) {
|
||||
if (parDirection.y() < 0.0f) {
|
||||
const float2 newPos(GetPos().x(), parCollision.Start().y());
|
||||
this->SetAbsolutePosition(newPos);
|
||||
m_bottomBar.SetAbsolutePosition(newPos);
|
||||
m_bounceCallback(parCollision, parDirection);
|
||||
}
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -28,28 +28,38 @@
|
|||
#include "collidertypedef.hpp"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
namespace cloonel {
|
||||
class SDLMain;
|
||||
class Texture;
|
||||
template <typename T, uint32_t S> class Line;
|
||||
|
||||
class Character : public Placeable, public Drawable {
|
||||
public:
|
||||
typedef std::function<void(const Line<float, 2>&, const float2&)> BounceCallbackType;
|
||||
|
||||
Character ( const std::string& parPath, SDLMain* parMain, float2 parSize );
|
||||
Character ( const std::string&& parPath, SDLMain* parMain, float2 parSize );
|
||||
Character ( const Character& ) = delete;
|
||||
virtual ~Character ( void ) noexcept;
|
||||
|
||||
void Prepare ( void );
|
||||
void Destroy ( void ) noexcept;
|
||||
virtual void Draw ( void ) const;
|
||||
void RegisterForCollision ( ColliderRegisterFunc parRegisterCollision );
|
||||
void SetOnBounceCallback ( BounceCallbackType parCallb );
|
||||
|
||||
private:
|
||||
//Overrides
|
||||
virtual void OnRegister ( Mover& parMover, Mover::PlaceableTicketType parParentTicket );
|
||||
|
||||
void OnBounce ( const Line<float, 2>& parCollision, const float2& parDirection );
|
||||
|
||||
HorzCollisionBar m_bottomBar;
|
||||
SizeNotifiable<regbehaviours::AutoRegister> m_screenRatio;
|
||||
BounceCallbackType m_bounceCallback;
|
||||
const std::unique_ptr<Texture> m_texture;
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
|
|
@ -88,9 +88,25 @@ namespace cloonel {
|
|||
std::cout << "Collider: Collision ";
|
||||
std::cout << "between " << bar1 << " and " << bar2 << "\n";
|
||||
#endif
|
||||
const auto dir1 = normalized(bar1->GetPos() - bar1->From());
|
||||
const auto& offs1 = bar1->GetOffset();
|
||||
const auto& offs2 = bar2->GetOffset();
|
||||
float2 dir1, dir2;
|
||||
if (offs1 == float2(0.0f) and offs2 == float2(0.0f)) {
|
||||
dir1 = dir2 = float2(0.0f);
|
||||
}
|
||||
else if (offs1 == float2(0.0f)) {
|
||||
dir2 = normalized(offs2);
|
||||
dir1 = -1.0f * dir2;
|
||||
}
|
||||
else if (offs2 == float2(0.0f)) {
|
||||
dir1 = normalized(offs1);
|
||||
dir2 = -1.0f * dir1;
|
||||
}
|
||||
else {
|
||||
dir1 = normalized(offs1);
|
||||
dir2 = normalized(offs2);
|
||||
}
|
||||
bar1->InvokeCallback(overlap, dir1);
|
||||
const auto dir2 = normalized(bar2->GetPos() - bar2->From());
|
||||
bar2->InvokeCallback(overlap, dir2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,10 @@ namespace cloonel {
|
|||
CollisionID_Player,
|
||||
CollisionID_Enemies
|
||||
};
|
||||
|
||||
void OnCharacterBounce (MoverSine* parMover, const Line<float, 2>&, const float2&) {
|
||||
parMover->ResetToBounce();
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
|
@ -70,9 +74,10 @@ namespace cloonel {
|
|||
void GameplaySceneClassic::OnPrepare() {
|
||||
const float halfRefHeight = static_cast<float>(REFERENCE_HEIGHT) / 2.0f;
|
||||
Collider& collider = *this->GetCollider();
|
||||
const float2 charaRefSize(static_cast<float>(REFERENCE_CHARA_WIDTH), static_cast<float>(REFERENCE_CHARA_HEIGHT));
|
||||
|
||||
std::unique_ptr<MoverSine> moverSine(new MoverSine());
|
||||
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f)));
|
||||
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), charaRefSize));
|
||||
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f));
|
||||
std::unique_ptr<MoverWorld> moverWorld(new MoverWorld(halfRefHeight));
|
||||
std::unique_ptr<TiledWallpaper> wallpaper(new TiledWallpaper("resources/graphics/background_tile.png", SDLObject()));
|
||||
|
@ -97,6 +102,8 @@ namespace cloonel {
|
|||
platforms->RegisterForCollision(regPlatfCollision, unregPlatfCollision);
|
||||
}
|
||||
|
||||
player->SetOnBounceCallback(std::bind(&OnCharacterBounce, moverSine.get(), std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
std::swap(moverSine, m_moverSine);
|
||||
std::swap(player, m_player);
|
||||
std::swap(moverLeftRight, m_moverLeftRight);
|
||||
|
@ -111,7 +118,7 @@ namespace cloonel {
|
|||
m_platforms->AddDrawables();
|
||||
AddDrawable(m_player.get());
|
||||
|
||||
const float jumpPower = halfRefHeight * 1.29f;
|
||||
const float jumpPower = halfRefHeight * 0.71f;
|
||||
m_moverSine->SetPower(jumpPower);
|
||||
|
||||
collider.TieGroups(CollisionID_Platforms, CollisionID_Player);
|
||||
|
|
|
@ -67,8 +67,7 @@ namespace cloonel {
|
|||
|
||||
const auto& retStart = (midpointA.Start().x() > midpointB.Start().x() ? midpointA.Start() : midpointB.Start());
|
||||
const auto& retEnd = (midpointA.End().x() < midpointB.End().x() ? midpointA.End() : midpointB.End());
|
||||
assert(retStart.y() == retEnd.y());
|
||||
return std::make_pair(true, Line2D(retStart, retEnd));
|
||||
return std::make_pair(true, Line2D(retStart, float2(retEnd.x(), retStart.y())));
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------
|
||||
|
|
|
@ -47,4 +47,10 @@ namespace cloonel {
|
|||
while (m_alpha >= pitwo)
|
||||
m_alpha -= pitwo;
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
void MoverSine::ResetToBounce() {
|
||||
m_alpha = 0.0f;
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -29,6 +29,7 @@ namespace cloonel {
|
|||
~MoverSine ( void ) noexcept = default;
|
||||
|
||||
void SetPower ( float parPower ) noexcept { m_power = parPower; }
|
||||
void ResetToBounce ( void );
|
||||
|
||||
private:
|
||||
virtual void ApplyMotion ( float parDelta );
|
||||
|
|
|
@ -28,6 +28,7 @@ namespace cloonel {
|
|||
public:
|
||||
float2 GetPos ( void ) const noexcept;
|
||||
virtual void AddOffset ( const float2& parOffset ) noexcept;
|
||||
virtual void SetAbsolutePosition ( const float2& parPos ) noexcept;
|
||||
virtual void OnRegister ( Mover& parMover, Mover::PlaceableTicketType parParentTicket );
|
||||
virtual void BeginMovement ( void );
|
||||
|
||||
|
@ -50,6 +51,12 @@ namespace cloonel {
|
|||
inline float2 Placeable::GetPos() const noexcept {
|
||||
return m_pos;
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
inline void Placeable::SetAbsolutePosition (const float2& parPos) noexcept {
|
||||
m_pos = parPos;
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue