Utilisation du type std::function plutôt que pointeur sur callback

This commit is contained in:
Mattéo Delabre 2016-03-30 20:28:33 +02:00
parent 03cae0eb2e
commit b7eb450225
2 changed files with 156 additions and 153 deletions

View File

@ -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);

View File

@ -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
collision_dispatcher dispatch = { std::map<
{std::make_pair(Player::TYPE_ID, Block::TYPE_ID), &playerToBlock}, std::pair<unsigned int, unsigned int>,
{std::make_pair(Block::TYPE_ID, Player::TYPE_ID), &blockToPlayer}, std::function<bool(CollisionData&)>
{std::make_pair(Player::TYPE_ID, Player::TYPE_ID), &playerToPlayer}, > Collision::dispatch = {
{std::make_pair(Block::TYPE_ID, Block::TYPE_ID), &blockToBlock} {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); Player player = dynamic_cast<Player&>(data.objA);
Block block = dynamic_cast<Block&>(data.objB); Block block = dynamic_cast<Block&>(data.objB);
@ -94,7 +96,7 @@ 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;
@ -103,7 +105,7 @@ namespace Collision {
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);
@ -134,7 +136,7 @@ namespace Collision {
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);
@ -173,4 +175,3 @@ namespace Collision {
return true; return true;
} }
}