diff --git a/src/CloonelJumpConfig.h.in b/src/CloonelJumpConfig.h.in index 1d2fc18..99c66d6 100644 --- a/src/CloonelJumpConfig.h.in +++ b/src/CloonelJumpConfig.h.in @@ -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@" diff --git a/src/character.cpp b/src/character.cpp index 948e8f6..f59b7e3 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -21,9 +21,15 @@ #include "sdlmain.hpp" #include "texture.hpp" #include "collider.hpp" +#include "line.hpp" #include namespace cloonel { + namespace { + void DoNothing (const Line&, 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& 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 diff --git a/src/character.hpp b/src/character.hpp index 611b1bb..da31922 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -28,28 +28,38 @@ #include "collidertypedef.hpp" #include #include +#include +#include namespace cloonel { class SDLMain; class Texture; + template class Line; class Character : public Placeable, public Drawable { public: + typedef std::function&, 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& parCollision, const float2& parDirection ); + HorzCollisionBar m_bottomBar; SizeNotifiable m_screenRatio; + BounceCallbackType m_bounceCallback; const std::unique_ptr m_texture; }; } //unnamed namespace diff --git a/src/collider.cpp b/src/collider.cpp index 5b33a11..5d5bdf0 100644 --- a/src/collider.cpp +++ b/src/collider.cpp @@ -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); } } diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 234ab3e..93f48b3 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -46,6 +46,10 @@ namespace cloonel { CollisionID_Player, CollisionID_Enemies }; + + void OnCharacterBounce (MoverSine* parMover, const Line&, const float2&) { + parMover->ResetToBounce(); + } } //unnamed namespace ///-------------------------------------------------------------------------- @@ -70,9 +74,10 @@ namespace cloonel { void GameplaySceneClassic::OnPrepare() { const float halfRefHeight = static_cast(REFERENCE_HEIGHT) / 2.0f; Collider& collider = *this->GetCollider(); + const float2 charaRefSize(static_cast(REFERENCE_CHARA_WIDTH), static_cast(REFERENCE_CHARA_HEIGHT)); std::unique_ptr moverSine(new MoverSine()); - std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f))); + std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), charaRefSize)); std::unique_ptr moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f)); std::unique_ptr moverWorld(new MoverWorld(halfRefHeight)); std::unique_ptr 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); diff --git a/src/horzcollisionbar.cpp b/src/horzcollisionbar.cpp index d3b5454..76704c4 100644 --- a/src/horzcollisionbar.cpp +++ b/src/horzcollisionbar.cpp @@ -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()))); } ///---------------------------------------------------------------------- diff --git a/src/movers/moversine.cpp b/src/movers/moversine.cpp index a1bf299..cf86823 100644 --- a/src/movers/moversine.cpp +++ b/src/movers/moversine.cpp @@ -47,4 +47,10 @@ namespace cloonel { while (m_alpha >= pitwo) m_alpha -= pitwo; } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void MoverSine::ResetToBounce() { + m_alpha = 0.0f; + } } //namespace cloonel diff --git a/src/movers/moversine.hpp b/src/movers/moversine.hpp index d6ccaa3..a11fefd 100644 --- a/src/movers/moversine.hpp +++ b/src/movers/moversine.hpp @@ -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 ); diff --git a/src/placeable.hpp b/src/placeable.hpp index f9208da..3a870a2 100644 --- a/src/placeable.hpp +++ b/src/placeable.hpp @@ -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