Playin' in the Maze
I stayed up very late last night and worked on it most of the day today. I have to mop floors and get some sleep, so no blogging today.
I'm proud of the architecture of this game. I'll write it up sometime, but for now, here's the code that determines the behavior of the missiles:
public class MissileBehavior extends Robot {protected Physob owner;
/** * A missile defaults to a delay of zero. Bloody fast. */ public MissileBehavior(Physob owner) { this(owner, 0); }
/** * Make a missile that moves every "delay" clock ticks. */ public MissileBehavior(Physob owner, int delay) { super(delay); this.owner = owner; }
public Physob getOwner() { return owner; }
public Action doSomething(Physob ob, MazeModel maze, int x, int y, int dir) { if (maze.isCellShared(ob)) return explode(ob, maze, x, y); else if (maze.forwardWallP(x, y, dir)) return DIE; else { x += maze.deltaX(dir); y += maze.deltaY(dir); if (maze.isCellOccupied(x, y)) { return explode(ob, maze, x, y); } else return FORWARD; } }
protected Action explode(Physob ob, MazeModel maze, int x, int y) { Physob[] obs = maze.getOccupants(x, y); for (int i=0; i<obs.length; i++) { Physob o = obs[i]; if ((o != ob) && (o != owner)) o.kill(owner); } return DIE; } }