From cdd37fead70cb0770a9917a1292f76b56bcdbfb3 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 1 Aug 2014 00:50:49 +0200 Subject: [PATCH] Add code to register objects to the collider. The character and the platforms can register now, but collision is not triggered for some reason. --- CMakeLists.txt | 1 + src/character.cpp | 7 +++++++ src/character.hpp | 4 +++- src/collider.cpp | 25 ++++++++++++++++++++++++- src/collider.hpp | 2 ++ src/collidertypedef.hpp | 32 ++++++++++++++++++++++++++++++++ src/gameplayscene.cpp | 1 + src/gameplayscene.hpp | 2 +- src/gameplaysceneclassic.cpp | 21 ++++++++++++++++++++- src/platform.cpp | 19 ++++++++++++++++--- src/platform.hpp | 12 +++++++++--- src/platformsystem.cpp | 12 ++++++++++++ src/platformsystem.hpp | 5 +++++ 13 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 src/collidertypedef.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d7aa40b..a38a109 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ find_package(Boost 1.55.0 REQUIRED) add_definitions( ${PNG_DEFINITIONS} -DWITH_VERBOSE_OBS_MANAGER + -DWITH_VERBOSE_COLLIDER ) include_directories(SYSTEM diff --git a/src/character.cpp b/src/character.cpp index 0e4370f..8a530b3 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -20,6 +20,7 @@ #include "character.hpp" #include "sdlmain.hpp" #include "texture.hpp" +#include "collider.hpp" #include namespace cloonel { @@ -52,6 +53,12 @@ namespace cloonel { Character::~Character() noexcept { } + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + void Character::RegisterForCollision (ColliderRegisterFunc parRegisterCollision) { + parRegisterCollision(&m_bottomBar); + } + ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- void Character::Prepare() { diff --git a/src/character.hpp b/src/character.hpp index d758723..cd6741f 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -25,6 +25,7 @@ #include "vector.hpp" #include "sizenotifiable.hpp" #include "horzcollisionbar.hpp" +#include "collidertypedef.hpp" #include #include @@ -35,12 +36,13 @@ namespace cloonel { class Character : public Placeable, public Drawable { public: Character ( const std::string& parPath, SDLMain* parMain, float2 parSize ); - Character ( const std::string&& parPath, SDLMain* parMai, float2 parSize ); + Character ( const std::string&& parPath, SDLMain* parMain, float2 parSize ); virtual ~Character ( void ) noexcept; void Prepare ( void ); void Destroy ( void ) noexcept; virtual void Draw ( void ) const; + void RegisterForCollision ( ColliderRegisterFunc parRegisterCollision ); private: HorzCollisionBar m_bottomBar; diff --git a/src/collider.cpp b/src/collider.cpp index e07113d..5b33a11 100644 --- a/src/collider.cpp +++ b/src/collider.cpp @@ -85,7 +85,7 @@ namespace cloonel { if (Collide(parDeltaT, *bar1, *bar2, overlap)) { #if defined(VERBOSE_COLLIDER) - std::cout << "Collider: Collision while testing group " << parGroup1 << " and " << parGroup2 << " "; + std::cout << "Collider: Collision "; std::cout << "between " << bar1 << " and " << bar2 << "\n"; #endif const auto dir1 = normalized(bar1->GetPos() - bar1->From()); @@ -122,6 +122,9 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void Collider::TieGroups (GroupIDType parGroup1, GroupIDType parGroup2) { +#if defined(VERBOSE_COLLIDER) + std::cout << "Collider: Tying group " << parGroup1 << " to " << parGroup2 << " for collision tests\n"; +#endif assert(m_relationships.end() == std::find(m_relationships.begin(), m_relationships.end(), std::make_pair(parGroup1, parGroup2))); assert(m_relationships.end() == std::find(m_relationships.begin(), m_relationships.end(), std::make_pair(parGroup2, parGroup1))); assert(isGroupRegistered(m_collisionBars, parGroup1)); @@ -132,11 +135,31 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void Collider::RegisterBar (GroupIDType parGroup, const CollisionBar* parBar) { +#if defined(VERBOSE_COLLIDER) + std::cout << "Collider: Registering bar " << parBar << " in group " << parGroup << "\n"; +#endif CollisionBarListType& group = findOrAddGroup(m_collisionBars, parGroup); assert(isGroupRegistered(m_collisionBars, parGroup)); assert(parBar); group.push_back(parBar); } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Collider::UnregisterBar (GroupIDType parGroup, const CollisionBar* parBar) { + assert(isGroupRegistered(m_collisionBars, parGroup)); + CollisionBarListType& group = findOrAddGroup(m_collisionBars, parGroup); + auto itdele = std::find(group.begin(), group.end(), parBar); + assert(itdele != group.end()); + group.erase(itdele); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Collider::Reset() noexcept { + m_relationships.clear(); + m_collisionBars.clear(); + } } //namespace cloonel #if defined(VERBOSE_COLLIDER) diff --git a/src/collider.hpp b/src/collider.hpp index 55af143..2414efa 100644 --- a/src/collider.hpp +++ b/src/collider.hpp @@ -40,6 +40,8 @@ namespace cloonel { void RunCollisionTests ( float parDeltaT ) const; void TieGroups ( GroupIDType parGroup1, GroupIDType parGroup2 ); void RegisterBar ( GroupIDType parGroup, const CollisionBar* parBar ); + void UnregisterBar ( GroupIDType parGroup, const CollisionBar* parBar ); + void Reset ( void ) noexcept; private: typedef std::vector>> CollisionBarGroupListType; diff --git a/src/collidertypedef.hpp b/src/collidertypedef.hpp new file mode 100644 index 0000000..759725a --- /dev/null +++ b/src/collidertypedef.hpp @@ -0,0 +1,32 @@ +/* + 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 . + +*/ + +#ifndef id3DF7C722C6AE4978874605F38F26E350 +#define id3DF7C722C6AE4978874605F38F26E350 + +#include + +namespace cloonel { + class HorzCollisionBar; + typedef std::function ColliderRegisterFunc; + typedef std::function ColliderUnregisterFunc; +} //namespace cloonel + +#endif diff --git a/src/gameplayscene.cpp b/src/gameplayscene.cpp index 7bb5ba7..0a1756d 100644 --- a/src/gameplayscene.cpp +++ b/src/gameplayscene.cpp @@ -49,6 +49,7 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void GameplayScene::Destroy() noexcept { + m_collider.Reset(); } ///-------------------------------------------------------------------------- diff --git a/src/gameplayscene.hpp b/src/gameplayscene.hpp index bbc2b3e..0c7a26b 100644 --- a/src/gameplayscene.hpp +++ b/src/gameplayscene.hpp @@ -40,7 +40,7 @@ namespace cloonel { virtual void Destroy ( void ) noexcept; protected: - Collider* GetCollider ( void ); + Collider* GetCollider ( void ) { return &m_collider; } private: virtual void OnRender ( void ); diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 09fc604..234ab3e 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -32,6 +32,7 @@ #include #include #include +#include namespace cloonel { namespace { @@ -39,6 +40,12 @@ namespace cloonel { GameAction_Left, GameAction_Right }; + + enum { + CollisionID_Platforms, + CollisionID_Player, + CollisionID_Enemies + }; } //unnamed namespace ///-------------------------------------------------------------------------- @@ -62,6 +69,7 @@ namespace cloonel { ///-------------------------------------------------------------------------- void GameplaySceneClassic::OnPrepare() { const float halfRefHeight = static_cast(REFERENCE_HEIGHT) / 2.0f; + Collider& collider = *this->GetCollider(); std::unique_ptr moverSine(new MoverSine()); std::unique_ptr player(new Character("resources/graphics/player.png", SDLObject(), float2(80.0f, 120.0f))); @@ -80,6 +88,15 @@ namespace cloonel { moverWorld->RegisterPlaceable(platforms.get()); wallpaper->Reload(); + { + auto regPlayerCollision(std::bind(&Collider::RegisterBar, &collider, CollisionID_Player, std::placeholders::_1)); + player->RegisterForCollision(regPlayerCollision); + + auto regPlatfCollision(std::bind(&Collider::RegisterBar, &collider, CollisionID_Platforms, std::placeholders::_1)); + auto unregPlatfCollision(std::bind(&Collider::UnregisterBar, &collider, CollisionID_Platforms, std::placeholders::_1)); + platforms->RegisterForCollision(regPlatfCollision, unregPlatfCollision); + } + std::swap(moverSine, m_moverSine); std::swap(player, m_player); std::swap(moverLeftRight, m_moverLeftRight); @@ -96,6 +113,9 @@ namespace cloonel { const float jumpPower = halfRefHeight * 1.29f; m_moverSine->SetPower(jumpPower); + + collider.TieGroups(CollisionID_Platforms, CollisionID_Player); + //collider.TieGroups(CollisionID_Enemies, CollisionID_Player); } ///-------------------------------------------------------------------------- @@ -110,7 +130,6 @@ namespace cloonel { m_moverLeftRight = std::move(std::unique_ptr(nullptr)); m_player = std::move(std::unique_ptr(nullptr)); m_moverSine = std::move(std::unique_ptr(nullptr)); - } ///-------------------------------------------------------------------------- diff --git a/src/platform.cpp b/src/platform.cpp index c6cc049..703ecae 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -19,16 +19,18 @@ #include "platform.hpp" #include "texture.hpp" +#include "horzcollisionbar.hpp" #include +#include namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- Platform::Platform (SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize) : Placeable(parPos), - m_collisionTop(parPos, parSize.x()), m_screenRatio(parSdlMain), m_size(parSize), + m_collisionTop(new HorzCollisionBar(parPos, parSize.x())), m_surface(parTexture) { assert(m_surface); @@ -38,13 +40,18 @@ namespace cloonel { ///-------------------------------------------------------------------------- Platform::Platform (Platform&& parOther) noexcept : Placeable(parOther.GetPos()), - m_collisionTop(parOther.m_collisionTop), m_screenRatio(std::move(parOther.m_screenRatio)), m_size(parOther.m_size), + m_collisionTop(std::move(parOther.m_collisionTop)), m_surface(parOther.m_surface) { } + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Platform::~Platform() noexcept { + } + ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void Platform::Draw() const { @@ -65,6 +72,12 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void Platform::OnRegister (Mover& parMover, Mover::PlaceableTicketType parParentTicket) { - parMover.RegisterPlaceable(&m_collisionTop, parParentTicket); + parMover.RegisterPlaceable(m_collisionTop.get(), parParentTicket); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Platform::RegisterForCollision (ColliderRegisterFunc parReg) { + parReg(m_collisionTop.get()); } } //namespace cloonel diff --git a/src/platform.hpp b/src/platform.hpp index cf7e0c6..a9b8619 100644 --- a/src/platform.hpp +++ b/src/platform.hpp @@ -23,31 +23,37 @@ #include "sizenotifiable.hpp" #include "drawable.hpp" #include "placeable.hpp" -#include "horzcollisionbar.hpp" +#include "collidertypedef.hpp" +#include namespace cloonel { class Texture; class SDLMain; + class HorzCollisionBar; class Platform : public Drawable, public Placeable { public: Platform ( SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize ); Platform ( Platform&& parOther ) noexcept; Platform ( const Platform& ) = delete; - virtual ~Platform ( void ) noexcept = default; + virtual ~Platform ( void ) noexcept; Platform& operator= ( const Platform& parOther ); float2 TopLeft ( void ) const { return GetPos(); } float2 BottomRight ( void ) const { return TopLeft() + m_size; } + void RegisterForCollision ( ColliderRegisterFunc parReg ); + //Overrides virtual void Draw ( void ) const; virtual void OnRegister ( Mover& parMover, Mover::PlaceableTicketType parParentTicket ); private: - HorzCollisionBar m_collisionTop; SizeNotifiable m_screenRatio; float2 m_size; + ColliderRegisterFunc m_registerToCollider; + ColliderUnregisterFunc m_unregisterFromCollider; + std::unique_ptr m_collisionTop; Texture* m_surface; }; } //namespace cloonel diff --git a/src/platformsystem.cpp b/src/platformsystem.cpp index 7f1c915..932abab 100644 --- a/src/platformsystem.cpp +++ b/src/platformsystem.cpp @@ -23,6 +23,7 @@ #include "texture.hpp" #include "gameplayscene.hpp" #include "mover.hpp" +#include "collider.hpp" #include #include #include @@ -167,6 +168,17 @@ namespace cloonel { } } + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void PlatformSystem::RegisterForCollision (ColliderRegisterFunc parReg, ColliderUnregisterFunc parUnreg) { + m_registerToCollider = parReg; + m_unregisterFromCollider = parUnreg; + + for (auto& platf : m_localdata->platforms) { + platf.platform->RegisterForCollision(m_registerToCollider); + } + } + ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void PlatformSystem::Destroy() noexcept { diff --git a/src/platformsystem.hpp b/src/platformsystem.hpp index 2c2af45..eee846a 100644 --- a/src/platformsystem.hpp +++ b/src/platformsystem.hpp @@ -20,11 +20,13 @@ #ifndef id17908979556C47F8A978688BBE4A9D22 #include "placeable.hpp" +#include "collidertypedef.hpp" #include namespace cloonel { class SDLMain; class GameplayScene; + class Collider; class PlatformSystem : public Placeable { public: @@ -36,6 +38,7 @@ namespace cloonel { PlatformSystem& operator= ( const PlatformSystem& ) = delete; void Prepare ( void ); + void RegisterForCollision ( ColliderRegisterFunc parReg, ColliderUnregisterFunc parUnreg ); void AddDrawables ( void ); void Destroy ( void ) noexcept; void SpawnPlatforms ( void ); @@ -47,6 +50,8 @@ namespace cloonel { struct LocalData; const std::unique_ptr m_localdata; + ColliderRegisterFunc m_registerToCollider; + ColliderUnregisterFunc m_unregisterFromCollider; }; } //namespace cloonel