Files
pacman.js/MapTile.js
2019-06-11 18:46:14 +02:00

79 lines
1.9 KiB
JavaScript

class MapTile {
constructor(x, y, isSolid, hasCoin) {
this.x = x;
this.y = y;
this.solid = isSolid;
this.coin = hasCoin;
}
lookAtNeighbours(neighbours) {
this.borders = {};
if(neighbours.left && neighbours.left.solid) {
this.borders.left = true;
}
if(neighbours.top && neighbours.top.solid) {
this.borders.top = true;
}
if(neighbours.right && neighbours.right.solid) {
this.borders.right = true;
}
if(neighbours.bottom && neighbours.bottom.solid) {
this.borders.bottom = true;
}
}
draw(ctx, conf) {
if (this.solid) {
this.drawSolid(ctx, conf);
}
if(this.coin) {
ctx.save();
ctx.translate((this.x + 0.5) * conf.tileSize, (this.y + 0.5) * conf.tileSize);
ctx.fillStyle = "#fff";
ctx.beginPath();
ctx.arc(0, 0, 0.15 * conf.tileSize, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
}
}
drawSolid(ctx, conf) {
ctx.save();
ctx.translate((this.x + 0.5) * conf.tileSize, (this.y + 0.5) * conf.tileSize);
ctx.strokeStyle = "#00f";
ctx.lineWidth = conf.tileSize * .5;
ctx.beginPath();
if (this.borders.left) {
ctx.moveTo(0.25 * conf.tileSize, 0);
ctx.lineTo(-0.5 * conf.tileSize, 0);
}
if (this.borders.top) {
ctx.moveTo(0, 0.25 * conf.tileSize);
ctx.lineTo(0, -0.5 * conf.tileSize);
}
if (this.borders.right) {
ctx.moveTo(-0.25 * conf.tileSize, 0);
ctx.lineTo(0.5 * conf.tileSize, 0);
}
if (this.borders.bottom) {
ctx.moveTo(0, -0.25 * conf.tileSize);
ctx.lineTo(0, 0.5 * conf.tileSize);
}
ctx.stroke();
ctx.restore();
}
}