Implémentation plus propre des fonctions de calcul d'info collision
This commit is contained in:
parent
b1f236ba0c
commit
aba30eefcd
|
@ -1,22 +1,13 @@
|
||||||
#ifndef __PTF_COLLISION_HPP__
|
#ifndef __PTF_COLLISION_HPP__
|
||||||
#define __PTF_COLLISION_HPP__
|
#define __PTF_COLLISION_HPP__
|
||||||
|
|
||||||
#include "object.hpp"
|
|
||||||
#include "collision_data.hpp"
|
#include "collision_data.hpp"
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
#include <utility>
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
namespace Collision {
|
/**
|
||||||
extern std::map<
|
* Détermine les informations sur la collision
|
||||||
std::pair<unsigned int, unsigned int>,
|
* entre les deux objets donnés dans data et
|
||||||
std::function<bool(CollisionData&)>
|
* les stocke dans data
|
||||||
> dispatch;
|
*/
|
||||||
|
bool getCollisionData(CollisionData& data);
|
||||||
bool playerToBlock(CollisionData& data);
|
|
||||||
bool blockToPlayer(CollisionData& data);
|
|
||||||
bool playerToPlayer(CollisionData& data);
|
|
||||||
bool blockToBlock(CollisionData& data);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,21 +3,14 @@
|
||||||
#include "player.hpp"
|
#include "player.hpp"
|
||||||
#include "block.hpp"
|
#include "block.hpp"
|
||||||
#include "object.hpp"
|
#include "object.hpp"
|
||||||
|
#include <functional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
// initialisation du dictionnaire associant les types
|
/**
|
||||||
// impliqués dans une collision à leur fonction de résolution
|
* Détermination des informations sur une collision entre
|
||||||
std::map<
|
* un joueur et un bloc (normale et profondeur de collision)
|
||||||
std::pair<unsigned int, unsigned int>,
|
*/
|
||||||
std::function<bool(CollisionData&)>
|
bool playerToBlock(CollisionData& data) {
|
||||||
> 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 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);
|
||||||
|
|
||||||
|
@ -96,7 +89,11 @@ bool Collision::playerToBlock(CollisionData& data) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Collision::blockToPlayer(CollisionData& data) {
|
/**
|
||||||
|
* Détermination des informations sur une collision entre
|
||||||
|
* un bloc et un joueur (normale et profondeur de collision)
|
||||||
|
*/
|
||||||
|
bool 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;
|
||||||
|
@ -105,7 +102,11 @@ bool Collision::blockToPlayer(CollisionData& data) {
|
||||||
return playerToBlock(data);
|
return playerToBlock(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Collision::playerToPlayer(CollisionData& data) {
|
/**
|
||||||
|
* Détermination des informations sur une collision entre
|
||||||
|
* deux joueurs (normale et profondeur de collision)
|
||||||
|
*/
|
||||||
|
bool 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);
|
||||||
|
|
||||||
|
@ -136,7 +137,11 @@ bool Collision::playerToPlayer(CollisionData& data) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Collision::blockToBlock(CollisionData& data) {
|
/**
|
||||||
|
* Détermination des informations sur une collision entre
|
||||||
|
* deux blocs (normale et profondeur de collision)
|
||||||
|
*/
|
||||||
|
bool 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);
|
||||||
|
|
||||||
|
@ -175,3 +180,24 @@ bool Collision::blockToBlock(CollisionData& data) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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_map = {
|
||||||
|
{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}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool getCollisionData(CollisionData& data) {
|
||||||
|
return collision_map[std::make_pair(
|
||||||
|
data.objA.getTypeId(),
|
||||||
|
data.objB.getTypeId()
|
||||||
|
)](data);
|
||||||
|
}
|
||||||
|
|
|
@ -102,11 +102,7 @@ bool Object::detectCollision(const Object& obj, CollisionData& data) const {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collision::dispatch[
|
return getCollisionData(data);
|
||||||
// la fonction de détection fine de collision est choisie
|
|
||||||
// en fonction des types des deux objets en question
|
|
||||||
std::make_pair(getTypeId(), obj.getTypeId())
|
|
||||||
](data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::solveCollision(Object& obj, const sf::Vector2f& normal) {
|
void Object::solveCollision(Object& obj, const sf::Vector2f& normal) {
|
||||||
|
|
Loading…
Reference in New Issue