(maze, c, _cellsize, onComplete, sprite = null)
| 322 | } |
| 323 | |
| 324 | function Player(maze, c, _cellsize, onComplete, sprite = null) { |
| 325 | var ctx = c.getContext("2d"); |
| 326 | var drawSprite; |
| 327 | var moves = 0; |
| 328 | drawSprite = drawSpriteCircle; |
| 329 | if (sprite != null) { |
| 330 | drawSprite = drawSpriteImg; |
| 331 | } |
| 332 | var player = this; |
| 333 | var map = maze.map(); |
| 334 | var cellCoords = { |
| 335 | x: maze.startCoord().x, |
| 336 | y: maze.startCoord().y |
| 337 | }; |
| 338 | var cellSize = _cellsize; |
| 339 | var halfCellSize = cellSize / 2; |
| 340 | |
| 341 | this.redrawPlayer = function (_cellsize) { |
| 342 | cellSize = _cellsize; |
| 343 | drawSpriteImg(cellCoords); |
| 344 | }; |
| 345 | |
| 346 | function drawSpriteCircle(coord) { |
| 347 | ctx.beginPath(); |
| 348 | ctx.fillStyle = "yellow"; |
| 349 | ctx.arc( |
| 350 | (coord.x + 1) * cellSize - halfCellSize, |
| 351 | (coord.y + 1) * cellSize - halfCellSize, |
| 352 | halfCellSize - 2, |
| 353 | 0, |
| 354 | 2 * Math.PI |
| 355 | ); |
| 356 | ctx.fill(); |
| 357 | if (coord.x === maze.endCoord().x && coord.y === maze.endCoord().y) { |
| 358 | onComplete(moves); |
| 359 | player.unbindKeyDown(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | function drawSpriteImg(coord) { |
| 364 | var offsetLeft = cellSize / 50; |
| 365 | var offsetRight = cellSize / 25; |
| 366 | ctx.drawImage( |
| 367 | sprite, |
| 368 | 0, |
| 369 | 0, |
| 370 | sprite.width, |
| 371 | sprite.height, |
| 372 | coord.x * cellSize + offsetLeft, |
| 373 | coord.y * cellSize + offsetLeft, |
| 374 | cellSize - offsetRight, |
| 375 | cellSize - offsetRight |
| 376 | ); |
| 377 | if (coord.x === maze.endCoord().x && coord.y === maze.endCoord().y) { |
| 378 | onComplete(moves); |
| 379 | player.unbindKeyDown(); |
| 380 | } |
| 381 | } |
nothing calls this directly
no test coverage detected