-
Notifications
You must be signed in to change notification settings - Fork 19
/
Pacman.js
180 lines (142 loc) · 3.66 KB
/
Pacman.js
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
//////////////////////////////////////////////////////
// Group members: Zi Wang (ziw), Bingying Xia(bxia) //
//////////////////////////////////////////////////////
function Pacman(xCord, yCord, direction){
this.x = xCord;
this.y = yCord;
this.dir = direction;
this.nextDir = undefined; //the direction to turn at next available turning point
this.radius = PACMAN_RADIUS;
this.mouthOpen = true;
}
Pacman.prototype.draw = function(color) {
if (color == undefined){
ctx.fillStyle = PACMAN_COLOR;
}
else{
ctx.fillStyle = color;
}
ctx.beginPath();
if (!this.mouthOpen){
switch(this.dir){
case UP:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*11/18, 2*Math.PI-Math.PI*7/18, true);
break;
case DOWN:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*29/18, 2*Math.PI-Math.PI*25/18, true);
break;
case LEFT:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*10/9, 2*Math.PI-Math.PI*8/9, true);
break;
case RIGHT:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI/9, 2*Math.PI-Math.PI*17/9, true);
break;
default:
break;
}
}
else {
switch(this.dir){
case UP:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*7/9, 2*Math.PI-Math.PI*2/9, true);
break;
case DOWN:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*16/9, 2*Math.PI-Math.PI*11/9, true);
break;
case LEFT:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*23/18, 2*Math.PI-Math.PI*13/18, true);
break;
case RIGHT:
ctx.arc(this.x, this.y, this.radius, 2*Math.PI-Math.PI*5/18, 2*Math.PI-Math.PI*31/18, true);
break;
default:
break;
}
}
ctx.lineTo(this.x, this.y);
ctx.fill();
};
//get the row index of current location
Pacman.prototype.getRow = function() {
return getRowIndex(this.y);
};
//get the col index of current location
Pacman.prototype.getCol = function() {
return getColIndex(this.x);
};
//return if pacman can move with current direction & tile
Pacman.prototype.canMove = function(dir) {
return canMove(this.x, this.y, dir);
};
//try to turn(if necessary) and move the pacman.
Pacman.prototype.move = function() {
if(onGridCenter(this.x, this.y) === false){
//not on a grid center
if(this.nextDir != undefined && (
(this.dir === UP && this.nextDir === DOWN )||
(this.dir === DOWN && this.nextDir === UP) ||
(this.dir === LEFT && this.nextDir === RIGHT) ||
(this.dir === RIGHT && this.nextDir ===LEFT)
))
{
this.dir = this.nextDir;
this.nextDir = undefined;
}
this.moveOneStep();
return;
}
else{
//on grid center. change direction if needed
if(this.nextDir != undefined && this.canMove(this.nextDir)){
this.dir = this.nextDir;
this.nextDir = undefined;
this.moveOneStep();
}
else{
//check if pacman can keep moving
if(this.canMove(this.dir)){
this.moveOneStep();
}
}
}
};
//move one step in the current direction if allowed
Pacman.prototype.moveOneStep = function() {
var newX =0;
var newY =0;
if(!canMove(this.x, this.y, this.dir)){
return;
}
switch(this.dir){
case UP:
newY = this.y - speed;
if(newY - this.radius - WALL_WIDTH > 0){
this.y = newY;
this.mouthOpen = ! this.mouthOpen;
}
break;
case DOWN:
newY = this.y + speed;
if(newY + this.radius + WALL_WIDTH < CANVAS_HEIGHT) {
this.y = newY;
this.mouthOpen = ! this.mouthOpen;
}
break;
case LEFT:
newX = this.x - speed;
if(newX - this.radius - WALL_WIDTH > 0 ){
this.x = newX;
this.mouthOpen = ! this.mouthOpen;
}
break;
case RIGHT:
newX = this.x + speed;
if(newX + this.radius + WALL_WIDTH < CANVAS_WIDTH){
this.x = newX;
this.mouthOpen = ! this.mouthOpen;
}
break;
default:
break;
}
};