/** *
* PacmanEngine.java * * Revisions: 1.0 Nov. 25, 2002 * Created the PacmanEngine class * 1.1 Nov. 26, 2002 * Compiled, Finished * 1.2 Nov. 26, 2002 * Corrected Movement problem, AI still seems wacky, but can't * find the problem * ** * Collaboration statement: * I worked on this assignment with Roland Krystian Alberciak * * @author *Roland Krystian Alberciak *@author Jose Manuel Caban * @version Version 1.2, Nov. 26, 2002 */ import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class PacmanEngine implements ActionListener, KeyListener{ /** *Timers to fire every x.ms and do whatever */ private Timer GhostTimer;//fire at P6Constants.GHOST_INTERVAL ms private Timer PacmanTimer;//fire at P6Constants.PACMAN_INTERVAL ms /** *globals to hold data for engine use */ private GameBoard board; private PacmanFrame pf; private Ghost[] ghosts; private Pacman pacman; /** *Constructor *@param pf, the PacmanFrame instance *@param gb, the GameBoard instance */ public PacmanEngine(PacmanFrame pf, GameBoard gb){ board = gb; this.pf = pf; //gb = pf.getGB(); pf.addKeyListener(this); //PacmanFileReader pfr = new PacmanFileReader(P6Constants.BOARD_FILE); GhostTimer = new Timer(P6Constants.GHOST_INTERVAL,this); PacmanTimer = new Timer(P6Constants.PACMAN_INTERVAL,this); ghosts = board.getGhosts(); pacman = board.getPacman(); } /** *@param ae, the action performed *Handles what ae asks for */ public void actionPerformed(ActionEvent ae){ if(ae.getSource() == PacmanTimer){ isLegalMove(pacman,pacman.getDirection()); } else if(ae.getSource() == GhostTimer){ for(int i=0; i<4; i++){ int[] array = ghosts[i].getMove(pacman.getX(),pacman.getY()); boolean legal = false; int z=0; do{ legal = isLegalMove(ghosts[z],array[z]); z++; }while(!legal && z<4); }//end for() }//end else if() GamePiece[][] temp = board.getPieceArray(); //check pacman eat nibble if(temp[pacman.getX()][pacman.getY()] instanceof FloorPiece){ ((FloorPiece)temp[pacman.getX()][pacman.getY()]).setNibble(false); } if(temp[pacman.getX()][pacman.getY()] instanceof FloorPiece){ if(((FloorPiece)temp[pacman.getX()][pacman.getY()]).getSpecial()){ ((FloorPiece)temp[pacman.getX()][pacman.getY()]).setSpecial(false); for(int i=0; i<4; i++){ ghosts[i].runAway(); } } } //check for game over boolean isDone = true; for(int x=0; x