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