Skip to content

Instantly share code, notes, and snippets.

@Madsy
Last active February 27, 2016 02:00
Show Gist options
  • Save Madsy/23b62e6dd714bad4ce92 to your computer and use it in GitHub Desktop.
Save Madsy/23b62e6dd714bad4ce92 to your computer and use it in GitHub Desktop.
class Asteroid;
class Ship;
class Bullet;
class GameObject;
class CollisionDispatcher {
public:
virtual bool collidesWith(Asteroid* obj1, GameObject* obj2){
return obj2->collidesWithAsteroid(obj1);
}
virtual bool collidesWith(Ship* obj1, GameObject* obj2){
return obj2->collidesWithShip(obj1);
}
virtual bool collidesWith(Bullet* obj1, GameObject* obj2){
return obj2->collidesWithBullet(obj1);
}
//actual implementation of the collision checks for all combinations
virtual bool collidesWith(Asteroid* obj1, Asteroid* obj2){
...
}
virtual bool collidesWith(Asteroid* obj1, Ship* obj2){
...
}
virtual bool collidesWith(Asteroid* obj1, Bullet* obj2){
...
}
virtual bool collidesWith(Ship* obj1, Asteroid* obj2){
...
}
virtual bool collidesWith(Ship* obj1, Ship* obj2){
...
}
virtual bool collidesWith(Ship* obj1, Bullet* obj2){
...
}
virtual bool collidesWith(Bullet* obj1, Asteroid* obj2){
...
}
virtual bool collidesWith(Bullet* obj1, Ship* obj2){
...
}
virtual bool collidesWith(Bullet* obj1, Bullet* obj2){
...
}
};
class GameObject {
public:
GameObject(CollisionDispatcher* visitor) : m_CollisionDispatcher(visitor) { ... }
virtual const Box getBoundingBox() const = 0;
virtual const Vector3& getPosition() const = 0;
virtual bool collidesWith(Asteroid* obj) = 0;
virtual bool collidesWith(Ship* obj) = 0;
virtual bool collidesWith(Bullet* obj) = 0;
virtual bool collidesWith(GameObject* obj) = 0;
protected:
CollisionDispatcher* getCollisionDispatcher(){ return m_CollisionDispatcher; }
...
private:
CollisionDispatcher* m_CollisionDispatcher;
};
class Asteroid : public GameObject {
public:
Asteroid(CollisionDispatcher* v) : GameObject(v){ ... }
const Box getBoundingBox() const {
...
}
const Vector3& getPosition() const {
...
}
//These dispatcher methods are always the same (boilerplate)
//but have to be implemented
bool collidesWith(Asteroid* obj){
//we know 'this' is an asteroid, and that 'obj' is an Asteroid type
return this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Ship* obj){
//we know 'this' is an asteroid, and that 'obj' is an Ship type
return this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Bullet* obj){
//we know 'this' is an asteroid, and that 'obj' is an Bullet type
return this->getCollisionDispatcher()->collidesWith(this, obj);
}
virtual bool collidesWith(GameObject* obj){
return this->getCollisionDispatcher()->collidesWith(this, obj);
} ...
};
class Ship : public GameObject {
public:
Ship(CollisionDispatcher* v) : GameObject(v){ ... }
const Box getBoundingBox() const {
...
}
const Vector3& getPosition() const {
...
}
//These dispatcher methods are always the same (boilerplate)
//but have to be implemented
bool collidesWith(Asteroid* obj){
//we know 'this' is a ship, and that 'obj' is an Asteroid type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Ship* obj){
//we know 'this' is a ship, and that 'obj' is an Ship type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Bullet* obj){
//we know 'this' is a ship, and that 'obj' is an Bullet type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
virtual bool collidesWith(GameObject* obj){
return this->getCollisionDispatcher()->collidesWith(this, obj);
}
...
};
class Bullet : public GameObject {
public:
Bullet(CollisionDispatcher* v) : GameObject(v){ ... }
const Box getBoundingBox() const {
...
}
const Vector3& getPosition() const {
...
}
//These dispatcher methods are always the same (boilerplate)
//but have to be implemented
bool collidesWith(Asteroid* obj){
//we know 'this' is a bullet, and that 'obj' is an Asteroid type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Ship* obj){
//we know 'this' is a bullet, and that 'obj' is an Ship type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
bool collidesWith(Bullet* obj){
//we know 'this' is a bullet, and that 'obj' is an Bullet type
this->getCollisionDispatcher()->collidesWith(this, obj);
}
virtual bool collidesWith(GameObject* obj){
return this->getCollisionDispatcher()->collidesWith(this, obj);
}
...
};
CollisionDispatcher visitor;
GameObject* ship = new Ship(&visitor);
GameObject* asteroid = new Asteroid(&visitor);
GameObject* bullet = new Bullet(&visitor);
//far removed from the concrete types..
if(ship->collidesWith(asteroid)){
...
}
if(bullet->collidesWith(ship)){
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment