#include #include
#include
#include
#include
#include
Using namespace std;
//
// Class SpaceStation;
// Class Asteroid;
// Class SpaceShip;
Class CollisionwithunkNownObject {public: CollisionwithunknownObject () {std :: cout << "unknown error" << std :: end1;} // ...};
Class GameObject {public: Virtual Void Collide (GameObject & OtherObject) = 0; // ...};
// The class SpaceShip Declarationclass SpaceShip: public GameObject {public: virtual void collide (GameObject & otherObject); virtual void hitSpaceShip (GameObject & spaceShip); virtual void hitSpaceStation (GameObject & spaceStation); virtual void hitAsteroid (GameObject & asteroid); // ... private: typedef void (SpaceShip :: * HitFunctionPtr) (GameObject & what); static HitFunctionPtr lookup (GameObject & what); typedef map
// The class SpaceStation Declarationclass SpaceStation: public GameObject {public: virtual void collide (GameObject & otherObject); virtual void hitSpaceShip (GameObject & otherObject); virtual void hitSpaceStation (GameObject & otherObject); virtual void hitAsteroid (GameObject & otherobject); // ... private: typedef void (SpaceStation :: * HitFunctionPtr) (GameObject & what); static HitFunctionPtr lookup (GameObject & what); typedef map
// The class Asteroid Declarationclass Asteroid: public GameObject {public: virtual void collide (GameObject & otherObject); virtual void hitSpaceShip (GameObject & otherObject); virtual void hitSpaceStation (GameObject & otherObject); virtual void hitAsteroid (GameObject & otherobject); // ... private: typedef void (Asteroid :: * HitFunctionPtr) (GameObject & what); static HitFunctionPtr lookup (GameObject & what); typedef map
// The class SpaceShip Definitionvoid SpaceShip :: collide (GameObject & otherObject) {HitFunctionPtr hfp = lookup (otherObject); // if the lookup function to call (hfp) {(this -> * hfp) (otherObject);} else {throw CollisionwithunknownObject ();}}
Spaceeship :: HitFunctionPTRSPACESHIP :: Lookup (GameObject & what) {// is not good, there will be a copy of the copy assignment // static hitmap collisionmap = initializecollisionimum (); // with a smart pointer static auto_ptr
CollisionMap (InitializationCollisionMap ());
Hitmap :: item MapenTry = CollisionMap-> Find (TypeId (what) .name ());
// If the lookup failed, MaPENTRY == CollisionMap.end ();
IF (MaPENTRY == COLLISIONMAP-> END ())
Return 0;
// If you found it, MaPENTRY will point to a complete MAP entry, // is a string, hitfunctionptr pair. We only want to // its second part, so this is Return (* mapentry). Second;
}
Spaceeship :: Hitmap * Spaceeship :: InitializeCollisionMap () {hitmap * phm = new hitmap
// This book is directly (* phm ["spaceship"] = & HITSPACESHIP; // and I use the DEV-C compiler, and I found that // typeid (spaceship) .Name () is not only spaceship, Instead, 128sPaceship // So I made the following modification (* phM) [typeid (spaceship) .name ()] = & spacessiP :: HitspaceShip; (* phM) [typeid (spacStation) .name ()] = & spacessiP :: HitsPacStation ; (* PhM) [typeid (asteroid) .Name ()] = & spacessiP :: HitasterOid; returnip :: HitspaceShip (GameObject & space) {spaceship & oship = Dynamic_cast
(spaceship);
Std :: cout << "Spaceeship Collide with space" << std :: end1;}
Void SpaceShip :: HitsPacStation (GameObject & SpaceStation) {spacStation & station = Dynamic_cast
SpaceStation;
Std :: cout << "Spaceeship Collide with space" << std :: endl;}
Void Spaceeship :: HitasterOid (GameObject & Asteroid) {askOID & THEASTEROID = Dynamic_cast
(asteroid);
Std :: cout << "spaceship collide with ask" << std :: end1;
// The class SpaceStation Definitionvoid SpaceStation :: collide (GameObject & otherObject) {HitFunctionPtr hfp = lookup (otherObject); if (hfp) {(this -> * hfp) (otherObject);} else {throw CollisionWithUnknownObject ();}}
SpacStation :: HitFunctionPtr SpaceStation :: Lookup (GameObject & what) {static auto_ptr
CollisionMap (InitializationCollisionMap ());
Hitmap :: item MapenTry = CollisionMap-> Find (TypeId (what) .name ());
IF (MaPENTRY! = CollisionMap-> end ()) return (* mapentry). Second;}
SpaceStation :: hitmap * spacestation :: initizecollisionMap () {hitmap * pfm = new hitmap; (* pfm) [typeid (spaceship) .name ()] = & spaceStation :: HitsPaceShip; (* pfm) [typeid (spacStation) .name ()] = & SpaceStation :: HitsPacStation; (* PFM) [typeid (asteroid) .name ()] = & spaceStation :: HitasterOid; Return PFM;
Void SpaceStation :: HitspaceShip (GameObject & OtherObject) {spaceship & ship = Dynamic_cast
(OtherObject); std :: cout << "spacStation collide with space" << std :: end1;
Void SpaceStation :: HitsPacStation (GameObject & OtherObject) {spacStation & OtherSpaceStation = DYNAMIC_CAST
OtherObject);
Std :: cout << "SpaceStation Collide with space" << std :: endl;
}
Void SpaceStation :: HitasterOid (GameObject & OtherObject) {askOID & THEASTEROID = Dynamic_cast
OtherObject);
Std :: cout << "SpaceStation Collide with Asteroid" << std :: endl;
}
// The class Asteroid Definitionvoid Asteroid :: collide (GameObject & otherObject) {HitFunctionPtr hfp = lookup (otherObject); if (hfp) {(this -> * hfp) (otherObject);} else {throw CollisionWithUnknownObject ();}}
Asteroid :: HitFunctionPtr Asteroid :: lookup (GameObject & what) {static auto_ptr
IF (MaPENTRY! = CollisionMap-> end ()) return (* mapentry). Second;}
Asteroid :: Hitmap * Asteroid :: InitializationCollisionMap () {hitmap * pfm = new hitmap; (* pfm) [typeid (spaceship) .Name ()] = & assetoid :: HitspaceShip; (* pfm) [typeid (spacStation) .name ()] = & Asset; (* pfm) [typeid (asteroid) .name ()] = & asset :: Hitasteroid; Return PFM;
Void Asteroid :: HitspaceShip (GameObject & OtherObject) {spaceship & ship = Dynamic_cast
(OtherObject); std :: cout << "Asteroid Collide with space" << std :: endl;}
Void Asteroid :: HitsPacStation (GameObject & OtherObject) {spacStation & station = Dynamic_cast
(OtherObject); std :: cout << "Asteroid Collide with space" << std :: end1;
Void Asteroid :: HitasterOid (GameObject & OtherObject) {askOID & OTHERASTEROID = Dynamic_cast
OtherObject);
Std :: Cout << "Asteroid Collide with Asteroid << std :: endl;
}
INT main () {gameObject * g = new spaceso; gameObject * h = new spaceObject * a = new asseteroid; g-> collide (* a); g-> collide (* h); h-> collide (* g); h-> collide (* a); A-> Collide (* g); A-> Collide (* h);
System ("pause");
}
This program passes on WinXP Professional DEV-C , and VC.NET is also passed.
Just DEV-C requires code
(* PFM) [TypeId (SpaceShip) .Name ()] = & assetoid :: HitspaceShip;
As long as the VC
(* PFM) [TypeId (spaceship) .Name ()] = & HITSPACESHIP;
You can
Other similar code processing