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

Code:
001	package PacmanGame;
002	 
003	import java.util.Random;
004	import java.awt.Graphics;
005	 
006	public abstract class Ghost extends Character{
007	     
008	    protected static final int DOWN = 0;
009	    protected static final int UP = 1;
010	    protected static final int LEFT = 2;
011	    protected static final int RIGHT = 3;
012	 
013	    protected Tiles target;
014	    protected Tiles scatterTile;
015	     
016	    protected boolean scatterMode;
017	    protected boolean chaseMode;
018	    protected boolean frightenedMode;
019	     
020	    protected Tiles pacmanTile;
021	    protected Tiles redGhostTile;
022	    protected int pacmanDirection;
023	    protected int redDirection;
024	     
025	    protected int futureDirection;
026	    protected int direction;
027	    protected int timer;
028	     
029	    protected Random randomNumGen = new Random();
030	     
031	     
032	    public Ghost(int x, int y, Maze maze){
033	        super(x, y, maze);
034	        futureDirection = 0;
035	        timer = 0;
036	        scatterMode = true;
037	        chaseMode = false;
038	        frightenedMode = false;
039	    }
040	     
041	    public void move() {
042	        target = calculateTarget();
043	        moveGhost(target);
044	    }
045	 
046	    public void moveGhost(Tiles target) {
047	        if(specialIntersections()){
048	            direction = direction; //keeps going in the same direction
049	        }
050	        else{
051	            int oppDir;
052	            if(direction == UP){
053	                oppDir = DOWN;
054	            }
055	            else if(direction == DOWN){
056	                oppDir = UP;
057	            }
058	            else if(direction == LEFT){
059	                oppDir = RIGHT;
060	            }
061	            else{
062	                oppDir=LEFT;
063	            }
064	 
065	            double minDist = 10000.0;
066	            Tiles potentialNext;
067	 
068	            for(int i=0; i<4; i++){
069	                if(i!=oppDir){
070	                    potentialNext = maze.nextTile(getCurrentPos(), i);
071	                    if(!(potentialNext.wall()) && check(potentialNext)){
072	                        if(calculateDistance(target, potentialNext) < minDist){
073	                            minDist = calculateDistance(target, potentialNext);
074	                            futureDirection = i;
075	                        }
076	                    }
077	                }
078	            }
079	        }
080	         
081	         
082	        changeDirection();
083	        timer++;
084	        increment();
085	         
086	        x += xinc;
087	        y += yinc;
088	        tunnel();
089	    }
090	     
091	    public void increment(){
092	        //...
093	    }
094	     
095	    public void changeDirection(){
096	        if(timer==5){
097	            direction = futureDirection;
098	            timer=0;
099	        }
100	    }
101	 
102	    public double calculateDistance(Tiles target, Tiles start) {
103	        //...
104	 
105	        return distance;
106	    }  
107	 
108	    public int getDirection() {
109	        return this.direction;
110	    }
111	 
112	    public boolean specialIntersections(){
113	 
114	        //...
115	    }
116	     
117	    public void retrieve(Tiles t, int d1, Tiles r, int d2){
118	        pacmanTile = t;
119	        pacmanDirection = d1;
120	        redGhostTile = r;
121	    }
122	     
123	    public void setTarget(Tiles t){
124	        target = t;
125	    }
126	     
127	 
128	    /*public void setFrightened(){
129	        if(scatterMode){
130	            scatterMode = false;
131	        }
132	        else{
133	            chaseMode = false;
134	        }
135	        frightenedMode = true;
136	         
137	        if(direction == UP){
138	            direction = DOWN;
139	        }
140	        else if(direction == DOWN){
141	            direction = UP;
142	        }
143	        else if(direction == LEFT){
144	            direction = RIGHT;
145	        }
146	        else{
147	            direction = LEFT;
148	        }
149	    }*/
150	     
151	//DOESN'T WORK!
152	public void setChase(){
153	        if (timer == 1)
154	            chaseMode=true;
155	         
156	    }
157	    public Tiles generateRandomTile(){
158	        //...
159	    }
160	     
161	    protected abstract Tiles calculateTarget();
162	     
163	    protected abstract void paintComponent(Graphics G);
164	}

GHOSTRED.java
Code:
01	package PacmanGame;
02	 
03	import java.awt.*;
04	 
05	public class GhostRed extends Ghost{
06	     
07	    public GhostRed(int x, int y, Maze maze) {
08	        super(x, y, maze);
09	        scatterTile = new Tiles(25, -1);
10	        direction = 0;
11	    }
12	 
13	    //@Override
14	    public void paintComponent(Graphics G){
15	        if(frightenedMode){
16	            G.setColor(Color.BLUE);
17	        }
18	        if(scatterMode){
19	            G.setColor(Color.GREEN);
20	        }
21	        else{
22	            G.setColor(Color.RED);
23	        }
24	        G.fillRect(x, y, 20, 20);
25	    }
26	     
27	    protected Tiles calculateTarget(){
28	        if(scatterMode){
29	            return scatterTile;
30	        }
31	        else if(frightenedMode){
32	            return generateRandomTile();
33	        }
34	        return pacmanTile;
35	    }
36	}