|
| 1 | +class Ghost { |
| 2 | + constructor( |
| 3 | + x, |
| 4 | + y, |
| 5 | + width, |
| 6 | + height, |
| 7 | + speed, |
| 8 | + imageX, |
| 9 | + imageY, |
| 10 | + imageWidth, |
| 11 | + imageHeight, |
| 12 | + range |
| 13 | + ) { |
| 14 | + this.x = x; |
| 15 | + this.y = y; |
| 16 | + this.width = width; |
| 17 | + this.height = height; |
| 18 | + this.speed = speed; |
| 19 | + this.direction = DIRECTION_RIGHT; |
| 20 | + this.imageX = imageX; |
| 21 | + this.imageY = imageY; |
| 22 | + this.imageHeight = imageHeight; |
| 23 | + this.imageWidth = imageWidth; |
| 24 | + this.loopCounter = 1; |
| 25 | + this.range = range; |
| 26 | + this.randomDirection = DIRECTION_BOTTOM; |
| 27 | + setInterval(() => { |
| 28 | + this.changeRandomDirection(); |
| 29 | + }, 5000); |
| 30 | + } |
| 31 | + |
| 32 | + isInRange() { |
| 33 | + let xDistance = Math.abs(pacman.getMapX() - this.getMapX()); |
| 34 | + let yDistance = Math.abs(pacman.getMapY() - this.getMapY()); |
| 35 | + if ( |
| 36 | + Math.sqrt(xDistance * xDistance + yDistance * yDistance) <= |
| 37 | + this.range |
| 38 | + ) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 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"); |
| 49 | + } |
| 50 | + |
| 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 | + } |
| 61 | + |
| 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 | + if (this.isInRange()) { |
| 68 | + this.changeDirectionIfPossible(); |
| 69 | + } else { |
| 70 | + this.playRandomMove(); |
| 71 | + } |
| 72 | + |
| 73 | + this.moveForwards(); |
| 74 | + |
| 75 | + if (this.checkCollisions()) { |
| 76 | + this.moveBackwards(); |
| 77 | + return; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + moveBackwards() { |
| 82 | + switch (this.direction) { |
| 83 | + case 4: // Right |
| 84 | + this.x -= this.speed; |
| 85 | + break; |
| 86 | + case 3: // Up |
| 87 | + this.y += this.speed; |
| 88 | + break; |
| 89 | + case 2: // Left |
| 90 | + this.x += this.speed; |
| 91 | + break; |
| 92 | + case 1: // Bottom |
| 93 | + this.y -= this.speed; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + moveForwards() { |
| 99 | + switch (this.direction) { |
| 100 | + case 4: // Right |
| 101 | + this.x += this.speed; |
| 102 | + break; |
| 103 | + case 3: // Up |
| 104 | + this.y -= this.speed; |
| 105 | + break; |
| 106 | + case 2: // Left |
| 107 | + this.x -= this.speed; |
| 108 | + break; |
| 109 | + case 1: // Bottom |
| 110 | + this.y += this.speed; |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + checkCollisions() { |
| 116 | + let isCollided = false; |
| 117 | + if ( |
| 118 | + map[parseInt(this.y / oneBlockSize)][ |
| 119 | + parseInt(this.x / oneBlockSize) |
| 120 | + ] == 1 || |
| 121 | + map[parseInt(this.y / oneBlockSize + 0.999)][ |
| 122 | + parseInt(this.x / oneBlockSize) |
| 123 | + ] == 1 || |
| 124 | + map[parseInt(this.y / oneBlockSize)][ |
| 125 | + parseInt(this.x / oneBlockSize + 0.9999) |
| 126 | + ] == 1 || |
| 127 | + map[parseInt(this.y / oneBlockSize + 0.9999)][ |
| 128 | + parseInt(this.x / oneBlockSize + 0.9999) |
| 129 | + ] == 1 |
| 130 | + ) { |
| 131 | + isCollided = true; |
| 132 | + } |
| 133 | + return isCollided; |
| 134 | + } |
| 135 | + |
| 136 | + changeDirectionIfPossible() { |
| 137 | + let tempDirection = this.direction; |
| 138 | + this.direction = this.calculateNewDirection( |
| 139 | + map, |
| 140 | + parseInt(pacman.x / oneBlockSize), |
| 141 | + parseInt(pacman.y / oneBlockSize) |
| 142 | + ); |
| 143 | + if (typeof this.direction == "undefined") { |
| 144 | + console.log("undefined"); |
| 145 | + this.direction = tempDirection; |
| 146 | + return; |
| 147 | + } |
| 148 | + this.loopCounter++; |
| 149 | + this.moveForwards(); |
| 150 | + if (this.checkCollisions()) { |
| 151 | + this.moveBackwards(); |
| 152 | + this.direction = tempDirection; |
| 153 | + } else { |
| 154 | + this.moveBackwards(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + calculateNewDirection(map, destX, destY) { |
| 159 | + let mp = []; |
| 160 | + for (let i = 0; i < map.length; i++) { |
| 161 | + mp[i] = map[i].slice(); |
| 162 | + } |
| 163 | + |
| 164 | + let queue = [ |
| 165 | + { |
| 166 | + x: parseInt(this.x / oneBlockSize), |
| 167 | + y: parseInt(this.y / oneBlockSize), |
| 168 | + moves: [], |
| 169 | + }, |
| 170 | + ]; |
| 171 | + |
| 172 | + while (queue.length > 0) { |
| 173 | + let poped = queue.shift(); |
| 174 | + if (poped.x == destX && poped.y == destY) { |
| 175 | + return poped.moves[0]; |
| 176 | + } else { |
| 177 | + mp[poped.y][poped.x] = 1; |
| 178 | + let neighborList = this.addNeighbors(poped, mp); |
| 179 | + for (let i = 0; i < neighborList.length; i++) { |
| 180 | + queue.push(neighborList[i]); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return 1; // direction |
| 186 | + } |
| 187 | + |
| 188 | + addNeighbors(poped, mp) { |
| 189 | + let queue = []; |
| 190 | + let numOfRows = mp.length; |
| 191 | + let numOfColumns = mp[0].length; |
| 192 | + |
| 193 | + if ( |
| 194 | + poped.x - 1 >= 0 && |
| 195 | + poped.x - 1 < numOfRows && |
| 196 | + mp[poped.y][poped.x - 1] != 1 |
| 197 | + ) { |
| 198 | + let tempMoves = poped.moves.slice(); |
| 199 | + tempMoves.push(DIRECTION_LEFT); |
| 200 | + queue.push({ x: poped.x - 1, y: poped.y, moves: tempMoves }); |
| 201 | + } |
| 202 | + if ( |
| 203 | + poped.x + 1 >= 0 && |
| 204 | + poped.x + 1 < numOfRows && |
| 205 | + mp[poped.y][poped.x + 1] != 1 |
| 206 | + ) { |
| 207 | + let tempMoves = poped.moves.slice(); |
| 208 | + tempMoves.push(DIRECTION_RIGHT); |
| 209 | + queue.push({ x: poped.x + 1, y: poped.y, moves: tempMoves }); |
| 210 | + } |
| 211 | + if ( |
| 212 | + poped.y - 1 >= 0 && |
| 213 | + poped.y - 1 < numOfColumns && |
| 214 | + mp[poped.y - 1][poped.x] != 1 |
| 215 | + ) { |
| 216 | + let tempMoves = poped.moves.slice(); |
| 217 | + tempMoves.push(DIRECTION_UP); |
| 218 | + queue.push({ x: poped.x, y: poped.y - 1, moves: tempMoves }); |
| 219 | + } |
| 220 | + if ( |
| 221 | + poped.y + 1 >= 0 && |
| 222 | + poped.y + 1 < numOfColumns && |
| 223 | + mp[poped.y + 1][poped.x] != 1 |
| 224 | + ) { |
| 225 | + let tempMoves = poped.moves.slice(); |
| 226 | + tempMoves.push(DIRECTION_BOTTOM); |
| 227 | + queue.push({ x: poped.x - 1, y: poped.y + 1, moves: tempMoves }); |
| 228 | + } |
| 229 | + return queue; |
| 230 | + } |
| 231 | + |
| 232 | + getMapX() { |
| 233 | + let mapX = parseInt(this.x / oneBlockSize); |
| 234 | + return mapX; |
| 235 | + } |
| 236 | + |
| 237 | + getMapY() { |
| 238 | + let mapY = parseInt(this.y / oneBlockSize); |
| 239 | + return mapY; |
| 240 | + } |
| 241 | + |
| 242 | + getMapXRightSide() { |
| 243 | + let mapX = parseInt((this.x * 0.99 + oneBlockSize) / oneBlockSize); |
| 244 | + return mapX; |
| 245 | + } |
| 246 | + |
| 247 | + getMapYRightSide() { |
| 248 | + let mapY = parseInt((this.y * 0.99 + oneBlockSize) / oneBlockSize); |
| 249 | + return mapY; |
| 250 | + } |
| 251 | + |
| 252 | + changeAnimation() { |
| 253 | + this.currentFrame = |
| 254 | + this.currentFrame == this.frameCount ? 1 : this.currentFrame + 1; |
| 255 | + } |
| 256 | + |
| 257 | + draw() { |
| 258 | + canvasContext.save(); |
| 259 | + canvasContext.drawImage( |
| 260 | + ghostFrames, |
| 261 | + this.imageX, |
| 262 | + this.imageY, |
| 263 | + this.imageWidth, |
| 264 | + this.imageHeight, |
| 265 | + this.x, |
| 266 | + this.y, |
| 267 | + this.width, |
| 268 | + this.height |
| 269 | + ); |
| 270 | + canvasContext.restore(); |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +let updateGhosts = () => { |
| 275 | + for (let i = 0; i < ghosts.length; i++) { |
| 276 | + ghosts[i].moveProcess(); |
| 277 | + } |
| 278 | +}; |
| 279 | + |
| 280 | +let drawGhosts = () => { |
| 281 | + for (let i = 0; i < ghosts.length; i++) { |
| 282 | + ghosts[i].draw(); |
| 283 | + } |
| 284 | +}; |
0 commit comments