Utilisation du type std::function plutôt que pointeur sur callback
This commit is contained in:
parent
03cae0eb2e
commit
b7eb450225
|
@ -5,11 +5,13 @@
|
||||||
#include "collision_data.hpp"
|
#include "collision_data.hpp"
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace Collision {
|
namespace Collision {
|
||||||
typedef bool (*collision_detect)(CollisionData&);
|
extern std::map<
|
||||||
typedef std::map<std::pair<unsigned int, unsigned int>, collision_detect> collision_dispatcher;
|
std::pair<unsigned int, unsigned int>,
|
||||||
extern collision_dispatcher dispatch;
|
std::function<bool(CollisionData&)>
|
||||||
|
> dispatch;
|
||||||
|
|
||||||
bool playerToBlock(CollisionData& data);
|
bool playerToBlock(CollisionData& data);
|
||||||
bool blockToPlayer(CollisionData& data);
|
bool blockToPlayer(CollisionData& data);
|
||||||
|
|
|
@ -5,17 +5,19 @@
|
||||||
#include "object.hpp"
|
#include "object.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace Collision {
|
// initialisation du dictionnaire associant les types
|
||||||
// initialisation du dictionnaire associant les types
|
// impliqués dans une collision à leur fonction de résolution
|
||||||
// impliqués dans une collision à leur fonction de résolution
|
std::map<
|
||||||
collision_dispatcher dispatch = {
|
std::pair<unsigned int, unsigned int>,
|
||||||
{std::make_pair(Player::TYPE_ID, Block::TYPE_ID), &playerToBlock},
|
std::function<bool(CollisionData&)>
|
||||||
{std::make_pair(Block::TYPE_ID, Player::TYPE_ID), &blockToPlayer},
|
> Collision::dispatch = {
|
||||||
{std::make_pair(Player::TYPE_ID, Player::TYPE_ID), &playerToPlayer},
|
{std::make_pair(Player::TYPE_ID, Block::TYPE_ID), Collision::playerToBlock},
|
||||||
{std::make_pair(Block::TYPE_ID, Block::TYPE_ID), &blockToBlock}
|
{std::make_pair(Block::TYPE_ID, Player::TYPE_ID), Collision::blockToPlayer},
|
||||||
};
|
{std::make_pair(Player::TYPE_ID, Player::TYPE_ID), Collision::playerToPlayer},
|
||||||
|
{std::make_pair(Block::TYPE_ID, Block::TYPE_ID), Collision::blockToBlock}
|
||||||
|
};
|
||||||
|
|
||||||
bool playerToBlock(CollisionData& data) {
|
bool Collision::playerToBlock(CollisionData& data) {
|
||||||
Player player = dynamic_cast<Player&>(data.objA);
|
Player player = dynamic_cast<Player&>(data.objA);
|
||||||
Block block = dynamic_cast<Block&>(data.objB);
|
Block block = dynamic_cast<Block&>(data.objB);
|
||||||
|
|
||||||
|
@ -92,18 +94,18 @@ namespace Collision {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool blockToPlayer(CollisionData& data) {
|
bool Collision::blockToPlayer(CollisionData& data) {
|
||||||
// la collision Block -> Player est la collision Player -> Block
|
// la collision Block -> Player est la collision Player -> Block
|
||||||
Object& objT = data.objB;
|
Object& objT = data.objB;
|
||||||
data.objB = data.objA;
|
data.objB = data.objA;
|
||||||
data.objA = objT;
|
data.objA = objT;
|
||||||
|
|
||||||
return playerToBlock(data);
|
return playerToBlock(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool playerToPlayer(CollisionData& data) {
|
bool Collision::playerToPlayer(CollisionData& data) {
|
||||||
Player playerA = dynamic_cast<Player&>(data.objA);
|
Player playerA = dynamic_cast<Player&>(data.objA);
|
||||||
Player playerB = dynamic_cast<Player&>(data.objB);
|
Player playerB = dynamic_cast<Player&>(data.objB);
|
||||||
|
|
||||||
|
@ -132,9 +134,9 @@ namespace Collision {
|
||||||
data.depth = totalRadius - length;
|
data.depth = totalRadius - length;
|
||||||
data.normal = dir / length;
|
data.normal = dir / length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool blockToBlock(CollisionData& data) {
|
bool Collision::blockToBlock(CollisionData& data) {
|
||||||
Block blockA = dynamic_cast<Block&>(data.objA);
|
Block blockA = dynamic_cast<Block&>(data.objA);
|
||||||
Block blockB = dynamic_cast<Block&>(data.objB);
|
Block blockB = dynamic_cast<Block&>(data.objB);
|
||||||
|
|
||||||
|
@ -172,5 +174,4 @@ namespace Collision {
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue