-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameEnvironment.java
157 lines (143 loc) · 5 KB
/
gameEnvironment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class gameEnvironment {
private final int height=31; //Number of GameSquares high the map is
private final int width=28; //Number of GameSquares wide the map is
private GameSquare[][] gsquare; //2D array of all the squares making up the map
private pacman pman; //Pacman character
private Ghost[] ghosts = new Ghost[2]; //Array of ghosts
private Character[] characters; //Array of pacman and ghost characters
private Node[] nodes; //Array of Nodes
public gameEnvironment(MapReader m) {
gsquare = new GameSquare[height][width];
initializeBoard(m);
initializeNodes();
pman = new pacman(348,588,nodes[37]);
pman.changeTarget(nodes[29],Direction.LEFT);
characters = new Character[1+ghosts.length];
characters[0]=pman;
ghosts[0]= new Ghost(38,38,Color.GREEN,nodes[0]);
car[1]=ghosts[0];
ghosts[1]= new Ghost(663,38,Color.PINK, nodes[57]);
car[2]=ghosts[1];
}
/* Initializes the gamesquare array from the provided map
* 1 is a wall. 0 is a space with a dot. 6 is a space with a big dot. 3 is an empty space
*/
private void initializeBoard(MapReader m) {
int[][] b = m.getMap();
for(int i = 0;i<height;i++) {
for(int j =0;j<width;j++) {
if(b[i][j]==1)
gsquare[i][j]=new GameSquare(true,false,false,i,j);
else if(b[i][j]==0)
gsquare[i][j]=new GameSquare(false,true,false,i,j);
else if(b[i][j]==6)
gsquare[i][j]=new GameSquare(false,false,true,i,j);
else
gsquare[i][j]=new GameSquare(false,false,false,i,j);
}
}
}
/* Establishes an map containing the nodes and edges connecting them within the pacman map.
* These are stored in an array in a specific order starting with the top left corner and reading down.
*/
private void initializeNodes() {
try {
Scanner a = new Scanner(new File("graph.txt"));
nodes = new Node[a.nextInt()];
for(int i =0;i<nodes.length;i++)
nodes[i]= new Node(a.nextInt(),a.nextInt());
for(int i =0;i<nodes.length;i++) {
while(!a.hasNext("-1")) {
int node = a.nextInt()-1;
int cost = a.nextInt();
nodes[i].addNeighbors(nodes[node], cost);
nodes[node].addNeighbors(nodes[i], cost);
}a.nextInt();
}
a.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//Update the game
public void update() {
pman.move();
for(Ghost a: ghosts)
makePath(a);
pointsDeaths();
}
private void makePath(Ghost a) {
if((a.getX()-13)%25==0&&(a.getY()-13)%25==0&&(a.getX()-13)/25==a.getNodeAt().getCol()&&(a.getY()-13)/25==a.getNodeAt().getRow()) {
if(pman.getTargetNode()!=null)
a.setPath((new PathfindingAlgos().astar(nodes, a.getNodeAt(), pman.getTargetNode())));
else
a.setPath((new PathfindingAlgos().astar(nodes, a.getNodeAt(), pman.getNodeAt())));
}a.move();
}
/* Changes the direction of the Pacman character
* Case 1: New direction is opposite of current direction
* Case 2: Pacman currently isn't moving
* Case 3: A standard change of direction
*/
public void changeD(Direction d) {
if(d.opposite()==pman.getDirection()) {
Node n = pman.getTargetNode();
pman.targetNodeNull();
pman.changeTarget(pman.getNodeAt(), d);
pman.setNodeAt(n);
pman.changeNextTargetNode(null);
}else if(pman.getTargetNode()==null) {
Node n = pman.getNodeAt().neighborInDirection(d);
if(n!=null)
pman.changeTarget(n,d);
}else {
Node n = pman.getTargetNode().neighborInDirection(d);
if(n!=null)
pman.changeTarget(n,d);
}
}
/*First goes through and sees if the pacman character and any of the ghost characters are in the same GameSquare
* if they are, the pacman loses a life and all characters reset their positions
* Secondly if the pacman is in a GameSquare containg a point than he eats it and increases his score
*/
private void pointsDeaths() {
for(Ghost a: ghosts) {
if((new Rectangle(a.getX()-10,a.getY()-10,20,20)).intersects(new Rectangle(pman.getX()-10,pman.getY()-10,20,20))){
pman.loseLife();
for(Character c: characters)
c.resetPosition();
pman.targetNodeNull();
pman.changeTarget(nodes[29],Direction.LEFT);
pman.changeNextTargetNode(null);
break;
}
}
if(gsquare[pman.getY()/25][pman.getX()/25].hasDot()) {
pman.increaseScore(10);
gsquare[pman.getY()/25][pman.getX()/25].removeDot();
}
}
//Returns the score of the pacman
public int getScore() {
return pman.getScore();
}
//Returns the lives of the pacman
public int getLives() {
return pman.getLife();
}
//Calls the draw methods for each GameSquare and Character respectively
public void draw(Graphics g) {
for(int i =0;i<gsquare.length;i++) {
for(int j =0;j<gsquare[0].length;j++)
gsquare[i][j].draw(g);
}
for(Character a: characters)
a.draw(g);
}
}