I am designing a basic game engine. I have an abstract GameObject
class which are inherited by two classes Player
and Bullet
. I am trying to handle collision of two game objects. If both of the collided objects are the same, nothing happens. If one of them is bullet and other is player, the player's health is set to 0. Here is a snippet showing the required method and class definitions:
public interface GameObject {
void onCollision(GameObject gameObject);
}
public class GameObjectImpl implements GameObject {
...
// By default, do nothing.
@Override
public void onCollision(GameObject object) {}
}
public class Player extends GameObjectImpl {
...
@Override
public void onCollision(GameObject gameObject) {
if (gameObject instanceof Player) {
// Do nothing
} else if (gameObject instanceof Bullet) {
this.health = 0;
}
}
}
I want to avoid instanceof
but I could not think of any way to do so. I wanted to implement it with using polymorphism
as clients of GameObject
interface should not know the details about bullets or players etc. It will just call onCollision
method for objects in each frame. How can I achieve this with a cleaner code? Thank you.