-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.js
More file actions
196 lines (176 loc) · 5.88 KB
/
Tile.js
File metadata and controls
196 lines (176 loc) · 5.88 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* all different types of tiles
*/
const TYPES = [
"BARRIER",
"BISCUIT",
"OPEN",
"CHERRY",
"GHOST",
"PACMAN"
];
const TILE_SPEED = 0.10; // speed of tile's movement
const DIMENSIONS = 20; // size of field
const SIZE = 25; // size of each tile
const HALF_SIZE = SIZE / 2;
const THIRD_SIZE = SIZE / 3;
const QUARTER_SIZE = SIZE / 4;
/**
* makes up the field
* tiles can be moved
* tiles can restrict movement
*/
function Tile(x, y, type, behavior) {
this.x = x;
this.y = y;
this.type = type;
this.destination = (-1, -1);
this.moving = false;
this.intact = true;
this.speed = 0.15;
this.behavior = behavior; // GHOSTs only; 0 = agressive, 1 = nonchalant
}
/**
* handles movement, eating, and AI
*/
Tile.prototype.update = function() {
if (!this.intact) // no need to update
return;
/* movement */
if (this.moving) {
console.log(this.x, this.y, "before lerp");
console.log(this.destination.x, this.destination.y);
this.x = lerp(this.x, this.destination.x, this.speed);
this.y = lerp(this.y, this.destination.y, this.speed);
console.log(this.x, this.y, "after lerp");
var distanceX = Math.abs(this.x - this.destination.x);
var distanceY = Math.abs(this.y - this.destination.y);
if (distanceX < 0.1 && distanceY < 0.1) { // round to the nearest position
this.x = this.destination.x;
this.y = this.destination.y;
this.moving = false; // done moving
}
}
/* eating */
if (this.type == "PACMAN") { // only PACMAN may eat!
// Tile to which Pac-man is moving
var destinationTile = getTile(Math.floor(this.x), Math.floor(this.y));
if (destinationTile.intact) {
switch (destinationTile.type) {
case "BISCUIT":
score++; // worth 1 point
destinationTile.intact = false;
break;
case "CHERRY":
score += 10; // worth 10 points
destinationTile.intact = false;
break;
}
}
if (score == endScore) // check if Pac-man has won
endGame(true);
} else if (this.type == "GHOST") {
/* GHOST AI */
var distance = dist(pacman.x, pacman.y, this.x, this.y);
if (distance < 0.3) // if Pac-man has touched a GHOST
endGame(false);
/* movement */
if (this.moving) // can't move multiple times at once
return;
/* relative possible movements */
var possibleMoves = [
getTile(this.x - 1, this.y), // left
getTile(this.x + 1, this.y), // right
getTile(this.x, this.y - 1), // top
getTile(this.x, this.y + 1), // bottom
];
/* sort by distance from Pac-man */
possibleMoves.sort(function(a, b) {
var aD = dist(a.x, a.y, pacman.x, pacman.y);
var bD = dist(b.x, b.y, pacman.x, pacman.y);
return aD - bD;
});
if (this.behavior === 0) { // if they're agressive
for (var i = 0; i < possibleMoves.length; i++) {
if (this.move(possibleMoves[i].x, possibleMoves[i].y, false)) { // attempt to move
break;
}
}
} else {
// move nonchalantly
var index = Math.floor(random(4));
this.move(possibleMoves[index].x, possibleMoves[index].y, false);
}
}
};
Tile.prototype.draw = function() {
switch (this.type) {
case "BARRIER":
strokeWeight(5);
stroke(0);
fill("#0000FF");
rect(this.x * SIZE, this.y * SIZE, SIZE, SIZE);
break;
case "BISCUIT":
ellipseMode(CORNER);
noStroke();
fill(255);
ellipse(this.x * SIZE + THIRD_SIZE, this.y * SIZE + THIRD_SIZE, THIRD_SIZE);
break;
case "CHERRY":
ellipseMode(CORNER);
stroke(255);
strokeWeight(2);
fill("#FF2222");
ellipse(this.x * SIZE + QUARTER_SIZE, this.y * SIZE + QUARTER_SIZE, HALF_SIZE);
break;
case "GHOST":
fill("#FF00EE");
stroke(0);
strokeWeight(1);
/* draw a triangle */
beginShape();
vertex(this.x * SIZE + HALF_SIZE, this.y * SIZE + QUARTER_SIZE);
vertex(this.x * SIZE + QUARTER_SIZE, this.y * SIZE + (QUARTER_SIZE * 3));
vertex(this.x * SIZE + (QUARTER_SIZE * 3), this.y * SIZE + (QUARTER_SIZE * 3));
endShape(CLOSE);
break;
case "PACMAN":
ellipseMode(CORNER);
stroke("#FFFF00");
strokeWeight(5);
fill("#FFFF33");
ellipse(this.x * SIZE + QUARTER_SIZE, this.y * SIZE + QUARTER_SIZE, HALF_SIZE);
break;
}
};
/**
* calculates movement for use within update function
* returns whether it's a valid move or not
*/
Tile.prototype.move = function(x, y, relative) {
var destinationX, destinationY;
if (relative) { // relative to the tile
destinationX = this.x + x;
destinationY = this.y + y;
} else {
destinationX = x;
destinationY = y;
}
if (this.moving) // no need to recalculate everything
return false;
var destinationTile = getTile(destinationX, destinationY);
var type = destinationTile.type;
if ((type == "BARRIER" && this.type != "BARRIER") || // only certain tiles may
(type == "GHOST" && this.type == "GHOST")) // move to other certain tiles
return false;
this.moving = true; // begin movement next update
this.destination = createVector(destinationX, destinationY);
return true;
};
/**
* returns tile with coordinates (x, y)
*/
function getTile(x, y) {
return field[y * DIMENSIONS + x];
}