343 lines
7.7 KiB
Java
343 lines
7.7 KiB
Java
import javax.swing.*; //includes Timer
|
|
import java.awt.event.*; //ActionListener
|
|
|
|
|
|
/**
|
|
* CS1322: Programming Assignment #6 - Fall 2002
|
|
*
|
|
* <PRE>
|
|
* IDBox for: PacmanEngine.java
|
|
*
|
|
* Revisions: 1.0 Nov. 24, 2002
|
|
* Created the PacmanEngine class
|
|
*
|
|
* </PRE>
|
|
*
|
|
* Collaboration statement:
|
|
* "I worked on the homework assignment alone, using only
|
|
* course materials."
|
|
*
|
|
* @author
|
|
<A HREF="mailto:gtg142g@mail.gatech.edu">Roland Krystian Alberciak</A>
|
|
* @version Version 1.0, Nov. 24, 2002
|
|
*/
|
|
|
|
// imports are listed above IDBox
|
|
|
|
/*
|
|
* CONTROLLER
|
|
*/
|
|
|
|
public class PacmanEngine implements ActionListener, KeyListener
|
|
{
|
|
/** Debug value for toggling System.out.* output */
|
|
public static boolean DEBUG = false;
|
|
public static boolean DEBUG_CODE = false;
|
|
public static boolean DEBUG_MAIN = false;
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Constructors
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Constructor 1. Specified default-overriding constructor for PacmanEngine.
|
|
* @param void No parameters.
|
|
* @return PacmanEngine instance/object.
|
|
*
|
|
public PacmanEngine()
|
|
{
|
|
|
|
} //end of PacmanEngine()
|
|
*/
|
|
|
|
/**
|
|
* Constructor 2. Specified default-overloading constructor for PacmanEngine.
|
|
* @param void No parameters.
|
|
* @return PacmanEngine instance/object.
|
|
*/
|
|
public PacmanEngine(PacmanFrame pmf, GameBoard gb)
|
|
{
|
|
this.setPmf( pmf );
|
|
this.setBoard( gb );
|
|
|
|
this.setGhosts( gb.getPfr().getGhosts() );
|
|
this.setPacman( gb.getPfr().getPacman() );
|
|
/*
|
|
GhostTimer= new Timer(P6Constants.GHOST_INTERVAL, ); //add AL
|
|
PacmanTimer = new Timer(P6Constants.PACMAN_INTERVAL, ); //add AL
|
|
*/
|
|
} //end of PacmanEngine()
|
|
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Methods which override class Object methods
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/**
|
|
* Equal instance comparison. Compares if two instances are equal.
|
|
* @param o object-type reference of (hopefully) an PacmanEngine.
|
|
* @return boolean whether objects are equal.
|
|
*
|
|
public boolean equals (Object o)
|
|
{
|
|
|
|
} //end of equals(Object)
|
|
*/
|
|
|
|
/**
|
|
* toString instance printer. Prints properties of PacmanEngine.
|
|
* @return System.out.println(PacmanEngine instance) instance print.
|
|
*
|
|
public String toString()
|
|
{
|
|
|
|
} //end of toString()
|
|
*/
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Class specific methods local to this instance (ex: forced by interface)
|
|
// =======================================================================
|
|
*/
|
|
|
|
public void actionPerformed(ActionEvent ae)
|
|
{
|
|
int togglePG=0;
|
|
//CALL REPAINT
|
|
|
|
if( (ae.getSource() instanceof Timer) ==true )
|
|
{
|
|
//Now to determine if moves are legal
|
|
|
|
if
|
|
(
|
|
(ae.getActionCommand()=="PacmanTimer")
|
|
|| (ae.getActionCommand()=="GhostTimer")
|
|
)
|
|
{
|
|
checkMoveValid(
|
|
pacman.getDirection(),
|
|
pacman.getX(),
|
|
pacman.getY(),
|
|
this.board.getPieceArray()[pacman.getX()][pacman.getY()]
|
|
);
|
|
}
|
|
else
|
|
System.out.println(this.getClass()+" error: wrong Timer.");
|
|
|
|
|
|
if (this.legal==true)
|
|
{
|
|
if (ae.getActionCommand()=="PacmanTimer")
|
|
getPacman().move(pacman.getDirection());
|
|
|
|
if (ae.getActionCommand()=="GhostTimer")
|
|
{
|
|
int gMoves[];
|
|
for(int itr=0; itr<getGhosts().length; itr=itr+1)
|
|
gMoves= getGhosts()[itr].getMove(this.pacman.getX(),
|
|
this.pacman.getY()
|
|
);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
// returns true if valid, false if not.
|
|
public void checkMoveValid( int direction, int xAt, int yAt, Object piece )
|
|
{
|
|
boolean result=false;
|
|
|
|
switch (direction)
|
|
{
|
|
case (P6Constants.NORTH):
|
|
break;
|
|
case (P6Constants.EAST):
|
|
break;
|
|
case (P6Constants.SOUTH):
|
|
break;
|
|
case (P6Constants.WEST):
|
|
break;
|
|
}
|
|
|
|
this.legal=result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Invoked when a key has been pressed.
|
|
*/
|
|
public void keyPressed(KeyEvent e) {}
|
|
|
|
/**
|
|
* Invoked when a key has been released.
|
|
*/
|
|
public void keyReleased(KeyEvent e) {}
|
|
|
|
/**
|
|
* Invoked when a key has been typed.
|
|
*/
|
|
public void keyTyped(KeyEvent e) {}
|
|
|
|
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Methods dealing directly with the data within this instance
|
|
// (accessors, modifiers, etc)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Get the value of pmf.
|
|
* @return value of pmf.
|
|
*/
|
|
public PacmanFrame getPmf() {
|
|
return pmf;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pmf.
|
|
* @param v Value to assign to pmf.
|
|
*/
|
|
public void setPmf(PacmanFrame v) {
|
|
this.pmf = v;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Get the value of pacman.
|
|
* @return value of pacman.
|
|
*/
|
|
public Pacman getPacman()
|
|
{
|
|
return pacman;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pacman.
|
|
* @param v Value to assign to pacman.
|
|
*/
|
|
public void setPacman(Pacman v)
|
|
{
|
|
this.pacman = v;
|
|
}
|
|
|
|
/**
|
|
* Get the value of ghosts.
|
|
* @return value of ghosts.
|
|
*/
|
|
public Ghost[] getGhosts()
|
|
{
|
|
return ghosts;
|
|
}
|
|
|
|
/**
|
|
* Set the value of ghosts.
|
|
* @param v Value to assign to ghosts.
|
|
*/
|
|
public void setGhosts(Ghost[] v)
|
|
{
|
|
this.ghosts = v;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the value of board.
|
|
* @return value of board.
|
|
*/
|
|
public GameBoard getBoard() {
|
|
return board;
|
|
}
|
|
|
|
/**
|
|
* Set the value of board.
|
|
* @param v Value to assign to board.
|
|
*/
|
|
public void setBoard(GameBoard v) {
|
|
this.board = v;
|
|
}
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Protected Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Private Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
private Timer GhostTimer;
|
|
private Timer PacmanTimer;
|
|
|
|
/*
|
|
* board. GameBoard instance.
|
|
* The GameBoard instance used to draw the state of the board.
|
|
*/
|
|
private GameBoard board;
|
|
|
|
private PacmanFrame pmf;
|
|
|
|
private Ghost[] ghosts;
|
|
|
|
/**
|
|
* pacman. Pacman instance.
|
|
* The Pacman instance in play.
|
|
*/
|
|
private Pacman pacman;
|
|
|
|
private boolean legal;
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Main method (overrides this commenting system for readibility reasons)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Debugging main for class PacmanEngine.
|
|
* This method will rigorously test my code.
|
|
*
|
|
* <br><br>
|
|
* @param args a String array of command line arguments.
|
|
*/
|
|
public static void main(String[] args)
|
|
{
|
|
|
|
if ( (args.length==1) && (args[0].equals("-db") ) )
|
|
DEBUG_MAIN=true; //else leave false: ignore input.
|
|
|
|
if (DEBUG_MAIN)
|
|
{ // NOTE: This {} encapsulates most of main and is leftmost margin as a flag.
|
|
// redundant ifs left in, in case the main if(DEBUG) statement was ommitted,
|
|
// this would then print out neat output, as an example of real running.
|
|
|
|
|
|
|
|
|
|
|
|
if (DEBUG) System.out.println("end main(String[] args) call:");
|
|
} //end of DEBUG if
|
|
|
|
} //end of main(String[] args)
|
|
|
|
} //end of class PacmanEngine
|