📄 ghostactor.java
字号:
/**
* PacMan for J2ME Devices
* CS 327 - Design Project, Fall 2002
* University of Illinois, Urbana-Champaign
*
* file: GhostActor.java
* contact: Braden Kowitz
* date: 11/24/02
**/
//----------------------------------------------------------------------------//
import javax.microedition.lcdui.*;
/**
* Represents a ghost on the game board.
**/
public class GhostActor
{
//--------------------------//
// ENUM
//--------------------------//
/**
* Enum for a desired dirrection.
**/
public static final int NONE = 0;
/**
* Enum for a desired dirrection.
**/
public static final int UP = 1;
/**
* Enum for a desired dirrection.
**/
public static final int DOWN = 2;
/**
* Enum for a desired dirrection.
**/
public static final int LEFT = 3;
/**
* Enum for a desired dirrection.
**/
public static final int RIGHT = 4;
//--------------------------//
// MEMBER VARIABLES
//--------------------------//
/**
* The node where this ghost currently resides
**/
private GridNode myNode_;
/**
* This is the start node and where the ghost goes back to if
* pacman is killed
**/
private GridNode resetNode_;
/**
* The pacman which this ghost is chasing
* (or running from)
**/
private PacmanActor target_;
/**
* current X Pixel coord of this actor
**/
private int pixelX_;
/**
* current Y Pixel coord of this actor
**/
private int pixelY_;
/**
* destination X Pixel coord for this actor
**/
private int newPixelX_;
/**
* destination Y Pixel coord for this actor
**/
private int newPixelY_;
/**
* This value is true if the ghosts should run from pacman.
**/
private static boolean runFromPac_;
//--------------------------//
// FUNCTIONS
//--------------------------//
/**
* Constructor for this GhostActor.
* @param startingPosition the GridNode where this ghosts begins the game
* @param target the PacmanActor this ghost should chase or run from.
**/
public GhostActor(GridNode startingPosition, PacmanActor target)
{
myNode_ = startingPosition;
resetNode_ = startingPosition;
target_ = target;
pixelX_ = myNode_.pixelCenterX();
pixelY_ = myNode_.pixelCenterY();
newPixelX_ = pixelX_;
newPixelY_ = pixelY_;
}
/**
* Sets when all ghosts should run from pacman.
**/
public static void setRunFromPac(boolean b)
{ runFromPac_ = b; }
/**
* Added by Benson Fung (for killing pacman)
* @return the current node where this ghost resides
**/
public GridNode getNode() {
return myNode_;
}
/**
* Called to advance the logic of the game one frame.
**/
public void advanceFrame()
{
// if we are in an animation:
if ((pixelX_ != newPixelX_) || (pixelY_ != newPixelY_))
{
// move closer to the destination:
if (pixelX_ < newPixelX_) pixelX_++;
if (pixelX_ > newPixelX_ ) pixelX_--;
if (pixelY_ < newPixelY_) pixelY_++;
if (pixelY_ > newPixelY_ ) pixelY_--;
}
else
{
// find out the XY for pacman:
int pacX = target_.getNode().pixelCenterX();
int pacY = target_.getNode().pixelCenterY();
// calculate distance from current position
pacX -= pixelX_;
pacY -= pixelY_;
System.out.println("(" + pacX +"," + pacY + ")");
// do some logic to pick directions:
if (Math.abs(pacX) > Math.abs(pacY))
{
if (pacX > 0) moveTowards(RIGHT,UP,DOWN,LEFT);
else moveTowards(LEFT,DOWN,UP,RIGHT);
}
else
{
if (pacY > 0) moveTowards(DOWN,LEFT,RIGHT,DOWN);
else moveTowards(UP,RIGHT,LEFT,UP);
}
}
}
/**
* Tries to move the ghost in these directions.
* The first one is most favorable.
* @param i1 First most desierable choice
* @param i2 Second most desierable choice
* @param i3 Third most desierable choice
* @param i4 Fourth most desierable choice
**/
private void moveTowards(int i1, int i2, int i3, int i4)
{
// if we're running from pacman, we do these in the opposite order:
if (runFromPac_)
{
if ( goInDirection(i4) ) return;
if ( goInDirection(i3) ) return;
if ( goInDirection(i2) ) return;
if ( goInDirection(i1) ) return;
}
else
{
if ( goInDirection(i1) ) return;
if ( goInDirection(i2) ) return;
if ( goInDirection(i3) ) return;
if ( goInDirection(i4) ) return;
}
}
/**
* Tries to start moving in the specified dirrection.
* @param d desired dirrection in which to move
* @return true if we can go in this dirrection
**/
private boolean goInDirection(int d)
{
if (d == LEFT) return goToNode(myNode_.getLeft());
if (d == RIGHT) return goToNode(myNode_.getRight());
if (d == UP) return goToNode(myNode_.getUp());
if (d == DOWN) return goToNode(myNode_.getDown());
return false;
}
/**
* @param n Goes to this node if it is not null
* @return true when it's possible to go to the node
**/
private boolean goToNode(GridNode n)
{
if (n == null) return false;
myNode_ = n;
newPixelX_ = myNode_.pixelCenterX();
newPixelY_ = myNode_.pixelCenterY();
return true;
}
/**
* Paints the GhostActor
* @param g Graphics object where drawing should take place.
**/
public void paint(Graphics g)
{
g.setColor(0,0,0);
g.drawArc(pixelX_-4,
pixelY_-4,
8,8,
0,
360);
}
/**
* Returns the current x coordinate of the ghost
**/
public int getX()
{
return pixelX_;
}
/**
* Returns the current y coordinate of the ghost
**/
public int getY()
{
return pixelY_;
}
/*
Restart function:
Added 4-15-03 by Trevor Donarski
When pacman dies, this function will be called to move the ghosts to their initial
positions. This will also ensure that if a ghost is on the start position when pacman
dies, he doesn't respawn exactly on top of the ghost and start a loop of infinite kills.
*/
public void restart()
{
myNode_ = resetNode_;
pixelX_ = resetNode_.pixelCenterX();
pixelY_ = resetNode_.pixelCenterY();
newPixelX_ = pixelX_;
newPixelY_ = pixelY_;
}
}
//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -