Compare commits
6 commits
1f69ccd417
...
3b03f0af32
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 3b03f0af32 | ||
Bruno BELANYI | bd5f947469 | ||
Bruno BELANYI | 245d7d123d | ||
Bruno BELANYI | 7c6db4c19e | ||
Bruno BELANYI | bb48eaea34 | ||
Bruno BELANYI | 0cb79dc69e |
|
@ -122,6 +122,8 @@ struct Asteroid;
|
|||
struct Spaceship;
|
||||
|
||||
struct SpaceObject {
|
||||
virtual ~SpaceObject() = default;
|
||||
|
||||
// Only used to kick-start the double-dispatch process
|
||||
virtual void collide_with(SpaceObject& other) = 0;
|
||||
|
||||
|
@ -130,32 +132,24 @@ struct SpaceObject {
|
|||
virtual void collide_with(Spaceship& other) = 0;
|
||||
};
|
||||
|
||||
struct Asteroid {
|
||||
struct Asteroid : SpaceObject {
|
||||
void collide_with(SpaceObject& other) override {
|
||||
// `*this` is an `Asteroid&` which kick-starts the double-dispatch
|
||||
other.collide_with(*this);
|
||||
};
|
||||
|
||||
void collide_with(Asteroid& other) override {
|
||||
// Asteroid/Asteroid
|
||||
};
|
||||
void collide_with(Spaceship& other) override {
|
||||
// Asteroid/Spaceship
|
||||
};
|
||||
void collide_with(Asteroid& other) override { /* Asteroid/Asteroid */ };
|
||||
void collide_with(Spaceship& other) override { /* Asteroid/Spaceship */ };
|
||||
};
|
||||
|
||||
struct Spaceship {
|
||||
struct Spaceship : SpaceObject {
|
||||
void collide_with(SpaceObject& other) override {
|
||||
// `*this` is a `Spaceship&` which kick-starts the double-dispatch
|
||||
other.collide_with(*this);
|
||||
};
|
||||
|
||||
void collide_with(Asteroid& other) override {
|
||||
// Spaceship/Asteroid
|
||||
};
|
||||
void collide_with(Spaceship& other) override {
|
||||
// Spaceship/Spaceship
|
||||
};
|
||||
void collide_with(Asteroid& other) override { /* Spaceship/Asteroid */ };
|
||||
void collide_with(Spaceship& other) override { /* Spaceship/Spaceship */ };
|
||||
};
|
||||
|
||||
void collide(SpaceObject& first, SpaceObject& second) {
|
||||
|
|
Loading…
Reference in a new issue