Earlier this year i was working on a pacman game (http://www.java-gaming.org/forums/index.php?topic=12086.0) for a course in AI at the university im studying at, roughly based on Kevin Glass his Space Invaders tutorial. I finished it and got an A+ for it of which im quite proud, but since im following a course on Design Patterns now and since i have plans to do an internship at a major gamedeveloper and thus need a nice portfolio, ive decided to implement some of them into my Pacman game to make the code cleaner and the game extendable.
Since i still use the original collision detection methods from the tutorial, something like this;
if(pacman.collidesWith(x)){
pacman.collidedWith(x);
x.collidedWith(pacman);
}
It also still uses the instanceof, for example here in my GhostEntity class;
public void collidedWith(Entity other) {
if(other instanceof PacmanEntity) {
finalPath.clear();
}
}
Point is that ive come to the conclusion that using instanceof is a prove of flaud design in this case, and ive decided to replace this collidedWith method by implementing collision strategies somehow on the hand of the Strategy pattern (http://en.wikipedia.org/wiki/Strategy_pattern). What if i could do something like this, and make derived classes for each entity that needs to have a collisionstrategy:
public interface CollisionStrategy {
public void collidedWith(GhostEntity other);
public void collidedWith(PacmanEntity other);
public void collidedWith(WallEntity other);
public void collidedWith(DotEntity other);
}
For instance we have the PacmanCollisionStrategy which looks like this;
public class PacmanCollisionStrategy implements CollisionStrategy {
private PacmanGame game;
public PacmanCollisionStrategy(PacmanGame game){
this.game = game;
}
public void collidedWith(GhostEntity other) {
game.notifyDeath();
}
public void collidedWith(PacmanEntity other) {}
public void collidedWith(WallEntity other) {}
public void collidedWith(DotEntity other) {}
}
And the constructor of the Entity class of which all entities derive then looks like this, together with the collisioncheck method (which obviously doesnt work in this way);
public Entity(String ref, double x, double y, int size, CollisionStrategy collisionStrategy) {
this.sprite = SpriteStore.get().getSprite(ref);
this.x = x;
this.y = y;
this.size = size;
this.collisionStrategy = collisionStrategy;
this.setSpawnPosition(new Dimension((int)x/20,(int)y/20));
}
/
public void collidedWith(Entity other) {
collisionStrategy.collidedWith(other);
}
And i can now do this in my game initialization method:
pacman = new PacmanEntity("pacman.gif",this,20,20,size,moveSpeed, new PacmanCollisionStrategy(this));
The only thing is, im still not rid of the point that i have to determine somehow which method to use, similar to why you would use instanceof, because im still stuck with an arraylist of Entity objects, which i traverse inside my collision detection method to check for collisions.
for (int p=0;p<entityList.size();p++) {
for (int s=p+1;s<entityList.size();s++) {
Entity me = (Entity) entityList.get(p);
Entity him = (Entity) entityList.get(s);
if (me.collidesWith(him)) {
me.collidedWith(him);
him.collidedWith(me);
}
}
}
Anyone got any ideas on how i could get to where i want to be?