1 Replies - 1455 Views - Last Post: 19 November 2012 - 07:05 PM Rate Topic: -----

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

pacman switching btw modes in a level

Posted 13 November 2012 - 01:47 PM

Hi, Im doing a Pacman game. I have implemented the chase and scattered algorithms for the ghost but I don't know how to make them switch btw each other in a level. It should go like this:
scatter 7s
chase 20s
scatter 7sec
chase 20 sec
scatter 5 sec
chase 20sec
scatter 5sec
chase forever

Im including the GhostRed class which calculates the target of the red ghost and Ghost superclass which includes move ghost algo.


GHOST.java
package PacmanGame;

import java.util.Random;
import java.awt.Graphics;

public abstract class Ghost extends Character{
	
	protected static final int DOWN = 0;
	protected static final int UP = 1;
	protected static final int LEFT = 2;
	protected static final int RIGHT = 3;

	protected Tiles target;
	protected Tiles scatterTile;
	
	protected boolean scatterMode;
	protected boolean chaseMode;
	protected boolean frightenedMode;
	
	protected Tiles pacmanTile;
	protected Tiles redGhostTile;
	protected int pacmanDirection;
	protected int redDirection;
	
	protected int futureDirection;
	protected int direction;
	protected int timer;
	
	protected Random randomNumGen = new Random();
	
	
	public Ghost(int x, int y, Maze maze){
		super(x, y, maze);
		futureDirection = 0;
		timer = 0;
		scatterMode = true;
		chaseMode = false;
		frightenedMode = false;
	}
	
	public void move() {
		target = calculateTarget();
		moveGhost(target);
	}

	public void moveGhost(Tiles target) {
		if(specialIntersections()){
			direction = direction; //keeps going in the same direction
		}
		else{
			int oppDir;
			if(direction == UP){
				oppDir = DOWN;
			}
			else if(direction == DOWN){
				oppDir = UP;
			}
			else if(direction == LEFT){
				oppDir = RIGHT;
			}
			else{
				oppDir=LEFT;
			}

			double minDist = 10000.0;
			Tiles potentialNext;

			for(int i=0; i<4; i++){
				if(i!=oppDir){
					potentialNext = maze.nextTile(getCurrentPos(), i);
					if(!(potentialNext.wall()) && check(potentialNext)){
						if(calculateDistance(target, potentialNext) < minDist){
							minDist = calculateDistance(target, potentialNext);
							futureDirection = i;
						}
					}
				}
			}
		}
		
		
		changeDirection();
		timer++;
		increment();
		
		x += xinc;
		y += yinc;
		tunnel();
	}
	
	public void increment(){
		//...
	}
	
	public void changeDirection(){
		if(timer==5){
			direction = futureDirection;
			timer=0;
		}
	}

	public double calculateDistance(Tiles target, Tiles start) {
		//...

		return distance;
	}	

	public int getDirection() {
		return this.direction;
	}

	public boolean specialIntersections(){

		//...
	}
	
	public void retrieve(Tiles t, int d1, Tiles r, int d2){
		pacmanTile = t;
		pacmanDirection = d1;
		redGhostTile = r;
	}
	
	public void setTarget(Tiles t){
		target = t;
	}
	

	/*public void setFrightened(){
		if(scatterMode){
			scatterMode = false;
		}
		else{
			chaseMode = false;
		}
		frightenedMode = true;
		
		if(direction == UP){
			direction = DOWN;
		}
		else if(direction == DOWN){
			direction = UP;
		}
		else if(direction == LEFT){
			direction = RIGHT;
		}
		else{
			direction = LEFT;
		}
	}*/
	
//DOESN'T WORK!
public void setChase(){
		if (timer == 1)
			chaseMode=true;
		
	}
	public Tiles generateRandomTile(){
		//...
	}
	
	protected abstract Tiles calculateTarget();
	
	protected abstract void paintComponent(Graphics G);
}



GHOSTRED.java
package PacmanGame;

import java.awt.*;

public class GhostRed extends Ghost{
	
	public GhostRed(int x, int y, Maze maze) {
		super(x, y, maze);
		scatterTile = new Tiles(25, -1);
		direction = 0;
	}

	//@Override
	public void paintComponent(Graphics G){
		if(frightenedMode){
			G.setColor(Color.BLUE);
		}
		if(scatterMode){
			G.setColor(Color.GREEN);
		}
		else{
			G.setColor(Color.RED);
		}
		G.fillRect(x, y, 20, 20);
	}
	
	protected Tiles calculateTarget(){
		if(scatterMode){
			return scatterTile;
		}
		else if(frightenedMode){
			return generateRandomTile();
		}
		return pacmanTile;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: pacman switching btw modes in a level

#2 Leadwerks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 15-November 12

Re: pacman switching btw modes in a level

Posted 19 November 2012 - 07:05 PM

One way is to collapse your 3 "modes" scatterMode, chaseMode, frightenedMode into one enumeration type:

protected enum GHOST_MODE{
SCATTER,CHASE,FRIGHTENED
}

Then have an update function that keeps track of time between frames. You can probably use: System.currentTimeMillis()

http://docs.oracle.c...entTimeMillis()

Finally your moveGhost function should have a switch statement based on each ghosts current "GHOST_MODE" and change movement depending on current mode.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1