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

View File

@ -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}
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);
@ -94,7 +96,7 @@ 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;
@ -103,7 +105,7 @@ namespace Collision {
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);
@ -134,7 +136,7 @@ namespace Collision {
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);
@ -173,4 +175,3 @@ namespace Collision {
return true;
}
}