#include <windows.h>
#include <tchar.h>
#include <map>
#include <string>
#include <iostream>
using namespace std;
class GameObject
{
public:
virtual ~GameObject() {};
};
class SpaceShip: public GameObject
{
};
class SpaceStation: public GameObject
{
};
class Asteriod: public GameObject
{
};
class CollisionMap
{
public:
typedef void (*HitFunctionPtr) (GameObject&, GameObject&);
public:
typedef map<pair<string, string>, HitFunctionPtr> HitMap;
public:
void AddEntry (const string& type1, const string& type2, HitFunctionPtr collisionFunction, bool symmetric = true);
void RemoveEntry (const string& type1, const string& type2);
HitFunctionPtr Lookup(const string& type1, const string& type2);
static CollisionMap& TheCollisionMap ();
public:
pair<string, string> MakeStringPair(const char *s1, const char *s2);
CollisionMap () {};
CollisionMap (const CollisionMap &) {}
public:
HitMap map_;
};
class RegisterCollisionFunction
{
public:
RegisterCollisionFunction (const string& type1, const string& type2, CollisionMap::HitFunctionPtr collisionFunction, bool symetric = true)
{
CollisionMap::TheCollisionMap().AddEntry(type1, type2, collisionFunction, symetric);
}
};
CollisionMap& CollisionMap::TheCollisionMap ()
{
static CollisionMap map;
return map;
}
inline pair<string, string> CollisionMap::MakeStringPair(const char *s1, const char *s2)
{
return pair<string, string>(s1, s2);
}
inline void CollisionMap::AddEntry (const string& type1, const string& type2, HitFunctionPtr collisionFunction, bool symmetric)
{
string temp1;
string temp2;
temp1 = "class " + type1;
temp2 = "class " + type2;
map_ [MakeStringPair (temp1.c_str(), temp2.c_str())] = collisionFunction;
}
inline void CollisionMap::RemoveEntry (const string& type1, const string& type2)
{
string temp1;
string temp2;
temp1 = "class " + type1;
temp2 = "class " + type2;
map_.erase(MakeStringPair (temp1.c_str(), temp2.c_str()));
}
inline CollisionMap::HitFunctionPtr CollisionMap::Lookup(const string& type1, const string& type2)
{
HitMap::iterator mapEntry = map_.find ( MakeStringPair(type1.c_str(), type2.c_str()) );
if (mapEntry == map_.end () ) return 0;
return (*mapEntry).second;
}
void ShipShip(GameObject& shipObject1, GameObject& shipObject2)
{
SpaceShip& ship1 = dynamic_cast<SpaceShip&> (shipObject1);
SpaceShip& ship2 = dynamic_cast<SpaceShip&> (shipObject2);
cout << "The space ship is colliding with a space ship" << endl;
}
void ShipAsteriod(GameObject& shipObject, GameObject& asteriodObject)
{
SpaceShip& ship = dynamic_cast<SpaceShip&> (shipObject);
Asteriod& asteriod = dynamic_cast<Asteriod&> (asteriodObject);
cout << "The space ship is colliding with a asteriod" << endl;
}
void ShipSation(GameObject& shipObject, GameObject& stationObject)
{
SpaceShip& ship = dynamic_cast<SpaceShip&> (shipObject);
SpaceStation& station = dynamic_cast<SpaceStation&> (stationObject);
cout << "The space ship is colliding with a space station" << endl;
}
void AsteriodAsteriod(GameObject& asteriodObject1, GameObject& asteriodObject2)
{
Asteriod& asteriod1 = dynamic_cast<Asteriod&> (asteriodObject1);
Asteriod& asteriod2 = dynamic_cast<Asteriod&> (asteriodObject2);
cout << "The asteriod is colliding with a asteriod" << endl;
}
void AsteriodShip(GameObject& asteriodObject, GameObject& shipObject)
{
Asteriod& asteriod = dynamic_cast<Asteriod&> (asteriodObject);
SpaceShip& ship = dynamic_cast<SpaceShip&> (shipObject);
cout << "The asteriod is colliding with a space ship" << endl;
}
void AsteriodSation(GameObject& asteriodObject, GameObject& stationObject)
{
Asteriod& asteriod = dynamic_cast<Asteriod&> (asteriodObject);
SpaceStation& station = dynamic_cast<SpaceStation&> (stationObject);
cout << "The asteriod is colliding with a space staion" << endl;
}
void StationStation(GameObject& stationObject1, GameObject& stationObject2)
{
SpaceStation& station1 = dynamic_cast<SpaceStation&> (stationObject1);
SpaceStation& station2 = dynamic_cast<SpaceStation&> (stationObject2);
cout << "The space station is colliding with a space station" << endl;
}
void StationShip(GameObject& stationObject, GameObject& shipObject)
{
SpaceStation& station = dynamic_cast<SpaceStation&> (stationObject);
SpaceShip& ship = dynamic_cast<SpaceShip&> (shipObject);
cout << "The space staion is colliding with a space ship" << endl;
}
void StationAsteriod(GameObject& stationObject, GameObject& asteriodObject)
{
SpaceStation& station = dynamic_cast<SpaceStation&> (stationObject);
Asteriod& asteriod = dynamic_cast<Asteriod&> (asteriodObject);
cout << "The space station is colliding with a asteriod" << endl;
}
void AsteriodStation(GameObject& aa, GameObject& bb)
{
Asteriod& a = dynamic_cast<Asteriod&> (aa);
SpaceStation& b = dynamic_cast<SpaceStation&> (bb);
cout << "The space station is colliding with a asteriod" << endl;
}
void ProcessCollision(GameObject& object1, GameObject& object2)
{
CollisionMap::HitFunctionPtr phf = CollisionMap::TheCollisionMap().Lookup(typeid(object1).name(), typeid(object2).name());
if (phf)
phf(object1, object2);
else
{
cout << "Unknown object type" << endl;
}
}
RegisterCollisionFunction cf1 ("SpaceShip", "Asteriod", &ShipAsteriod);
RegisterCollisionFunction cf2 ("SpaceShip", "SpaceStation", &ShipSation);
RegisterCollisionFunction cf3 ("SpaceShip", "SpaceShip", &ShipShip);
RegisterCollisionFunction cf4 ("Asteriod", "Asteriod", &AsteriodAsteriod);
RegisterCollisionFunction cf5 ("Asteriod", "SpaceShip", &AsteriodShip);
RegisterCollisionFunction cf6 ("Asteriod", "SpaceStation", &AsteriodSation);
RegisterCollisionFunction cf7 ("SpaceStation", "SpaceStation", &StationStation);
RegisterCollisionFunction cf8 ("SpaceStation", "SpaceShip", &StationShip);
RegisterCollisionFunction cf9 ("SpaceStation", "Asteriod", &StationAsteriod);
int _tmain(int argc, _TCHAR* argv[])
{
GameObject* ship = new SpaceShip;
GameObject* station = new SpaceStation;
GameObject* asteriod = new Asteriod;
ProcessCollision(*station,*ship);
ProcessCollision(*ship, *ship);
ProcessCollision(*ship, *asteriod);
delete ship;
delete station;
delete asteriod;
return 0;
}
----------------------------------------------------------------------
The space staion is colliding with a space ship
The space ship is colliding with a space ship
The space ship is colliding with a asteriod