@@ -21,12 +21,12 @@ class Ghost {
21
21
this . imageY = imageY ;
22
22
this . imageHeight = imageHeight ;
23
23
this . imageWidth = imageWidth ;
24
- this . loopCounter = 1 ;
25
24
this . range = range ;
26
- this . randomDirection = DIRECTION_BOTTOM ;
25
+ this . randomTargetIndex = parseInt ( Math . random ( ) * 4 ) ;
26
+ this . target = randomTargetsForGhosts [ this . randomTargetIndex ] ;
27
27
setInterval ( ( ) => {
28
28
this . changeRandomDirection ( ) ;
29
- } , 5000 ) ;
29
+ } , 3000 ) ;
30
30
}
31
31
32
32
isInRange ( ) {
@@ -42,36 +42,31 @@ class Ghost {
42
42
}
43
43
44
44
changeRandomDirection ( ) {
45
- this . randomDirection = this . randomDirection % 4 ;
46
- this . randomDirection += parseInt ( Math . random * 4 ) ;
47
- this . randomDirection = ( this . randomDirection % 4 ) + 1 ;
48
- console . log ( "change" ) ;
45
+ let addition = 1 ;
46
+ this . randomTargetIndex += addition ;
47
+ this . randomTargetIndex = this . randomTargetIndex % 4 ;
49
48
}
50
49
51
- playRandomMove ( ) {
52
- this . direction = this . randomDirection ;
53
- this . moveForwards ( ) ;
54
- if ( this . checkCollisions ( ) ) {
55
- this . moveBackwards ( ) ;
56
- this . changeRandomDirection ( ) ;
57
- } else {
58
- this . moveBackwards ( ) ;
59
- }
60
- }
50
+ // playRandomMove() {
51
+ // console.log("random ");
52
+ // this.target = randomDirectionsForGhosts[this.randomDirectionIndex];
53
+ // this.moveForwards();
54
+ // if (this.checkCollisions()) {
55
+ // this.moveBackwards();
56
+ // this.changeRandomDirection();
57
+ // } else {
58
+ // this.moveBackwards();
59
+ // }
60
+ // }
61
61
62
62
moveProcess ( ) {
63
- // console.log(this.loopCounter);
64
- // console.log(parseInt(oneBlockSize / this.speed));
65
- // console.log(this.loopCounter % parseInt(oneBlockSize / this.speed));
66
-
67
63
if ( this . isInRange ( ) ) {
68
- this . changeDirectionIfPossible ( ) ;
64
+ this . target = pacman ;
69
65
} else {
70
- this . playRandomMove ( ) ;
66
+ this . target = randomTargetsForGhosts [ this . randomTargetIndex ] ;
71
67
}
72
-
68
+ this . changeDirectionIfPossible ( ) ;
73
69
this . moveForwards ( ) ;
74
-
75
70
if ( this . checkCollisions ( ) ) {
76
71
this . moveBackwards ( ) ;
77
72
return ;
@@ -137,22 +132,23 @@ class Ghost {
137
132
let tempDirection = this . direction ;
138
133
this . direction = this . calculateNewDirection (
139
134
map ,
140
- parseInt ( pacman . x / oneBlockSize ) ,
141
- parseInt ( pacman . y / oneBlockSize )
135
+ parseInt ( this . target . x / oneBlockSize ) ,
136
+ parseInt ( this . target . y / oneBlockSize )
142
137
) ;
143
138
if ( typeof this . direction == "undefined" ) {
144
- console . log ( "undefined" ) ;
145
139
this . direction = tempDirection ;
146
140
return ;
147
141
}
148
- this . loopCounter ++ ;
149
142
this . moveForwards ( ) ;
150
- if ( this . checkCollisions ( ) ) {
151
- this . moveBackwards ( ) ;
152
- this . direction = tempDirection ;
153
- } else {
143
+ let addition = 0 ;
144
+ while ( this . checkCollisions ( ) ) {
145
+ console . log ( "collided" , addition ) ;
154
146
this . moveBackwards ( ) ;
147
+ this . direction = ( tempDirection + addition ) % 4 ;
148
+ this . moveForwards ( ) ;
149
+ addition ++ ;
155
150
}
151
+ this . moveBackwards ( ) ;
156
152
}
157
153
158
154
calculateNewDirection ( map , destX , destY ) {
@@ -168,8 +164,6 @@ class Ghost {
168
164
moves : [ ] ,
169
165
} ,
170
166
] ;
171
- console . log ( "" ) ;
172
-
173
167
while ( queue . length > 0 ) {
174
168
let poped = queue . shift ( ) ;
175
169
if ( poped . x == destX && poped . y == destY ) {
@@ -225,7 +219,7 @@ class Ghost {
225
219
) {
226
220
let tempMoves = poped . moves . slice ( ) ;
227
221
tempMoves . push ( DIRECTION_BOTTOM ) ;
228
- queue . push ( { x : poped . x - 1 , y : poped . y + 1 , moves : tempMoves } ) ;
222
+ queue . push ( { x : poped . x , y : poped . y + 1 , moves : tempMoves } ) ;
229
223
}
230
224
return queue ;
231
225
}
@@ -269,6 +263,16 @@ class Ghost {
269
263
this . height
270
264
) ;
271
265
canvasContext . restore ( ) ;
266
+ canvasContext . beginPath ( ) ;
267
+ canvasContext . strokeStyle = "red" ;
268
+ canvasContext . arc (
269
+ this . x + oneBlockSize / 2 ,
270
+ this . y + oneBlockSize / 2 ,
271
+ this . range * oneBlockSize ,
272
+ 0 ,
273
+ 2 * Math . PI
274
+ ) ;
275
+ canvasContext . stroke ( ) ;
272
276
}
273
277
}
274
278
0 commit comments