-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.js
160 lines (145 loc) · 4.21 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
class Pacman {
constructor(x, y, width, height, speed) {
this.x = x
this.y = y
this.width = width
this.height = height
this.speed = speed
this.direction = DIRECTION_RIGHT
this.nextDirection = this.direction
this.currentFrame = 1
this.frameCount = 7
setInterval(() => {
this.changeAnimation()
}, 100)
}
moveProcess () {
this.changeDirectionIfPossible()
this.moveFowards()
if(this.checkCollision()) {
this.moveBackwards()
}
}
eat () {
for (let i = 0; i < map.length; i ++) {
for(let j = 0; j < map[0].length; j++) {
if(map[i][j] == 0 &&
this.getMapX() == j &&
this.getMapY() == i) {
map[i][j] = 3
score ++
}
}
}
}
moveBackwards () {
switch(this.direction) {
case DIRECTION_RIGHT:
this.x -= this.speed;
break;
case DIRETCION_UP:
this.y += this.speed;
break;
case DIRECTION_LEFT:
this.x += this.speed;
break;
case DIRECTION_BOTTOM:
this.y -= this.speed;
break;
}
}
moveFowards () {
switch( this.direction) {
case DIRECTION_RIGHT:
this.x += this.speed;
break;
case DIRETCION_UP:
this.y -= this.speed;
break;
case DIRECTION_LEFT:
this.x -= this.speed;
break;
case DIRECTION_BOTTOM:
this.y += this.speed;
break;
}
}
checkCollision () {
if (
map[this.getMapY()][this.getMapX()] == 1
|| map[this.getMapYRightSide()][this.getMapX()] == 1
|| map[this.getMapY()][this.getMapXRightSide()] == 1
|| map[this.getMapYRightSide()][this.getMapXRightSide()] == 1
){
return true
}
else return false
}
checkGhostCollision () {
for (let i = 0; i < ghosts.length; i++) {
let ghost = ghosts[i]
if(ghost.getMapX() == this.getMapX() && ghost.getMapY() == this.getMapY()) {
return true
}
}
return false
}
changeDirectionIfPossible () {
if(this.direction == this.nextDirection) return
let tempDirection = this.direction
this.direction = this.nextDirection
this.moveFowards()
if(this.checkCollision()){
this.moveBackwards()
this.direction = tempDirection
} else {
this.moveBackwards()
}
}
changeAnimation () {
this.currentFrame = this.currentFrame == this.frameCount ? 1 : this.currentFrame + 1
}
/**
* draw pacman saving context
* and replace in the same origin
*/
draw () {
canvasContext.save() // ????
canvasContext.translate(
this.x + oneBlockSize / 2,
this.y + oneBlockSize / 2
)
canvasContext.rotate((this.direction * 90 * Math.PI) / 180) // ?????
canvasContext.translate(
-this.x - oneBlockSize / 2,
-this.y - oneBlockSize / 2
)
//// pacmanFrames maybe bug for out of scope game.js
//// oneblockSize too ?
////drawImage has multiples constructors
canvasContext.drawImage(
pacmanFrames,
(this.currentFrame - 1) * oneBlockSize,
0,
oneBlockSize,
oneBlockSize,
this.x,
this.y,
this.width,
this.height
);
canvasContext.restore() // ????
}
getMapX () {
return parseInt(this.x / oneBlockSize)
}
getMapY () {
return parseInt(this.y / oneBlockSize)
}
getMapXRightSide () {
return parseInt((this.x + 0.9999 * oneBlockSize) / oneBlockSize)
}
getMapYRightSide () {
return parseInt((this.y + 0.9999 * oneBlockSize) / oneBlockSize)
}
}