add death

This commit is contained in:
biglyderv 2024-11-25 14:14:20 -05:00
parent b6ffe5cf32
commit b88d04a556
4 changed files with 44 additions and 16 deletions

View file

@ -30,14 +30,21 @@ function Game() {
}
Game.prototype.main = function () {
let { entities, player } = this;
if (player.health <= 0) {
document.querySelector('.message').style.display = 'flex';
return;
} else {
}
for (let ent of entities) {
ent.pos.x += ent.vel.x;
ent.pos.y += ent.vel.y;
ent.vel.x *= 0.8;
ent.vel.y *= 0.8;
ent.vel.x *= 0.9;
ent.vel.y *= 0.9;
ent.rot += 0.03 * ent.dir;
ent.rot = ent.rot % (Math.PI * 10);
@ -47,16 +54,24 @@ Game.prototype.main = function () {
ent.ticks++;
let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2);
let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x))
- (Math.cos(ent.rot) * (ent.pos.y - player.pos.y));
dp /= ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2);
dp /= dist;
dp *= 30;
dp *= 10;
if (Math.random() < -dp && Math.random() > 5 / (ent.ticks+2)) {
if (ent == player) continue;
if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) {
ent.bump();
}
if (Math.sqrt(dist) < 96 / 2) {
player.health --;
}
}
}
@ -85,6 +100,10 @@ Game.prototype.render = function () {
ctx.restore();
}
Game.prototype.ui = function() {
document.querySelector('.ui-text').textContent = `HP: ${this.player.health}`
}
Game.prototype.click = function () {
let { player } = this;
@ -95,5 +114,6 @@ var game = new Game();
setInterval(function () { game.main() }, 1000 / 60);
setInterval(function () { game.render() }, 1000 / 60);
setInterval(function () { game.ui() }, 1000 / 10);
game.canvas.onclick = () => game.click();