96 lines
2.3 KiB
JavaScript
96 lines
2.3 KiB
JavaScript
class Player extends MovingObject {
|
|
constructor(game, x, y) {
|
|
super(game, x, y);
|
|
this.speed = 3;
|
|
|
|
this.score = 0;
|
|
|
|
this.mouthAngle = 90; // = 90°
|
|
this.mouthDecreasing = true;
|
|
}
|
|
|
|
draw(ctx, conf) {
|
|
const radius = conf.tileSize * 0.5;
|
|
const cx = this.x * conf.tileSize;
|
|
const cy = this.y * conf.tileSize;
|
|
const mouthAngle = this.mouthAngle / 180 * Math.PI;
|
|
|
|
ctx.fillStyle = '#ff0';
|
|
|
|
ctx.save();
|
|
ctx.translate(cx, cy);
|
|
ctx.rotate(this.getRotateDir());
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(0, 0);
|
|
ctx.arc(0, 0, radius, mouthAngle / 2, 2 * Math.PI - mouthAngle / 2, false);
|
|
ctx.lineTo(0, 0);
|
|
|
|
ctx.fill();
|
|
|
|
ctx.restore();
|
|
}
|
|
|
|
update(deltaTime) {
|
|
super.update(deltaTime);
|
|
|
|
this.checkForCollisions();
|
|
|
|
// Collect coins
|
|
const currentTile = this.getNextTile(0, 0);
|
|
|
|
if(currentTile.tile.coin) {
|
|
if(currentTile.distanceX <= 0.5 || currentTile.distanceY <= 0.5) {
|
|
currentTile.tile.coin = false;
|
|
this.score += 10;
|
|
}
|
|
}
|
|
|
|
if(this.mouthDecreasing) {
|
|
this.mouthAngle -= this.speed * 4;
|
|
if(this.mouthAngle <= 0) {
|
|
this.mouthAngle = 0;
|
|
this.mouthDecreasing = false;
|
|
}
|
|
} else {
|
|
this.mouthAngle += this.speed * 4;
|
|
if(this.mouthAngle >= 90) {
|
|
this.mouthAngle = 90;
|
|
this.mouthDecreasing = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
checkForCollisions() {
|
|
this.game.gameObjects.forEach(object => {
|
|
if(object === this) {
|
|
return;
|
|
}
|
|
|
|
if(Math.abs(object.x - this.x) < 1 && Math.abs(object.y - this.y) < 1) {
|
|
if(object instanceof Ghost) {
|
|
this.gameOver();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
getRotateDir() {
|
|
if(this.dir.x === -1) {
|
|
return Math.PI;
|
|
}
|
|
if(this.dir.y === 1) {
|
|
return Math.PI / 2;
|
|
}
|
|
if(this.dir.y === -1) {
|
|
return 3 / 2 * Math.PI;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
gameOver() {
|
|
this.game.gameOver = true;
|
|
}
|
|
} |