I am looking for a way to avoid the dreaded switch or if/else scenarios between numerous game object types when a collision takes place in a game. For example:
You have a list of game objects which inherit from a common class such as GameObject. You check the list for any collisions and call a method for each colliding object to respond to the collision, but this situation develops:
public void handleCollision(GameObject other) {
switch(other.getType()) {
case Alien:
//Perform some logic
break;
case Ogre:
//Perform some logic
break;
case Wall:
//Perform some logic
break;
case Door:
//Perform some logic
break;
//and so on
}
}
This would become unwieldy with hundreds of different game object types. What I was attempting to do was this:
Map<CollisionKey, Method> collisionCallbackMap = new HashMap<CollisionKey, Method>();
collisionCallbackMap.put(new CollisionKey(gameObjectType, gameObjectType), //add a reference to a method here);
CollisionKey is a class I created for handling game object pairs such as Player-Alien, Player-Ogre, Player-Wall, etc. The idea was to look up a pair of game objects in the map, cast the game object in the collision list to the appropriate class types, and pass them into the method stored in the value slot of the map for that game object pairing. So it would look something like this:
for(GameObject a : collisionListA) {
for(GameObject b : collisionListB) {
if(a.collidesWith(b)) {
collisionCallbackMap.get(new CollisionKey(a.getType(), b.getType(), //cast GameObjects to appropriate types such as Player and Alien and pass it into a method here);
}
}
}
The method called from the map would look something like this if the game objects colliding were a Player and an Alien:
public void playerToAlienCollision(Player p, Alien a) {
//Perform some logic here such as...
p.reduceHealth();
a.reduceHealth();
a.knockBack();
}
This wouldn't be a problem if I were working in, say, C++ with function pointers, but I'm working in Java with Android. Android's Java only supports Java 6, and if I'm not mistaken I believe it now has support for Java 7, but it does not currently support the recently released Java 8. With the addition of lambdas and method references in Java 8 I feel this would be a viable approach, but I don't have access to that so I need another solution. The workaround or Java's version of function pointers before Java 8 seems to involve interfaces with a single method and I believe I would need one for each collision method. It just looks convoluted and not very polished. The accepted answer of this question displays an example of it:
https://stackoverflow.com/questions/4480334/how-to-call-a-method-stored-in-a-hashmap-java
I'm not looking for how collision detection is done, I'm ok with that, its what comes right after I'm seeking a solution for. What is an efficient way to handle collision response and callbacks without the dreaded and bloated switch or if/else scenarios? I think of a large game like Runescape with tons of objects and its written in Java and wonder how they set up their collision response/callbacks.