some fixes
This commit is contained in:
parent
3d4f76d6c8
commit
be75df1d23
3 changed files with 31 additions and 19 deletions
|
@ -17,7 +17,12 @@ class NPC {
|
||||||
handleTick(game) {
|
handleTick(game) {
|
||||||
let { entities } = game;
|
let { entities } = game;
|
||||||
|
|
||||||
let rEntity = entities[Math.floor(Math.random() * entities.length)];
|
let rEntity, i = 0;
|
||||||
|
|
||||||
|
while ((!rEntity || rEntity.type != 'Player') && i < 100) {
|
||||||
|
rEntity = entities[Math.floor(Math.random() * entities.length)];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
this.vel.x += (rEntity.pos.x - this.pos.x) * 0.0001;
|
this.vel.x += (rEntity.pos.x - this.pos.x) * 0.0001;
|
||||||
this.vel.y += (rEntity.pos.y - this.pos.y) * 0.0001;
|
this.vel.y += (rEntity.pos.y - this.pos.y) * 0.0001;
|
||||||
|
|
|
@ -68,6 +68,8 @@ class Player {
|
||||||
|
|
||||||
ent.ticks++;
|
ent.ticks++;
|
||||||
|
|
||||||
|
let velness = Math.sqrt(ent.vel.x ** 2 + ent.vel.y ** 2);
|
||||||
|
|
||||||
for (let target of entities) {
|
for (let target of entities) {
|
||||||
if (target.you == ent.you) continue;
|
if (target.you == ent.you) continue;
|
||||||
|
|
||||||
|
@ -79,7 +81,7 @@ class Player {
|
||||||
dp /= Math.sqrt(dist + 0.1);
|
dp /= Math.sqrt(dist + 0.1);
|
||||||
|
|
||||||
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
|
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
|
||||||
if (target.type == 'NPC') {
|
if (target.type == 'NPC' && velness > 0.2) {
|
||||||
ent.isMenu = true;
|
ent.isMenu = true;
|
||||||
}
|
}
|
||||||
if (target.immortal) continue;
|
if (target.immortal) continue;
|
||||||
|
|
|
@ -13,7 +13,7 @@ const legalTypes = {
|
||||||
Player,
|
Player,
|
||||||
NPC
|
NPC
|
||||||
}
|
}
|
||||||
const options = [
|
const emojis = [
|
||||||
'Troll',
|
'Troll',
|
||||||
'Exit',
|
'Exit',
|
||||||
'Elec Piano Loop',
|
'Elec Piano Loop',
|
||||||
|
@ -98,19 +98,24 @@ class Game extends GameBasic {
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
|
|
||||||
if (player.health <= 0 || player.isMenu) {
|
if (player.health <= 0 || player.isMenu) {
|
||||||
ctx.fillStyle = 'rgba(0,0,0,0.5)';
|
this.doMenu();
|
||||||
ctx.fillRect(0, 0, cs, cs);
|
}
|
||||||
ctx.fillStyle = 'rgb(255,255,255)';
|
}
|
||||||
ctx.textAlign = "center";
|
doMenu() {
|
||||||
ctx.textBaseline = "middle";
|
let { ctx, player } = this;
|
||||||
ctx.font = "bold 48px sans-serif";
|
|
||||||
if (player.health <= 0) ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
|
ctx.fillStyle = 'rgba(0,0,0,0.5)';
|
||||||
if (player.isMenu) {
|
ctx.fillRect(0, 0, cs, cs);
|
||||||
let r = Math.floor(Math.abs(player.rot / 1.2) % options.length);
|
ctx.fillStyle = 'rgb(255,255,255)';
|
||||||
ctx.fillText(`Click to react ${options[r]}`, cs / 2, cs / 2)
|
ctx.textAlign = "center";
|
||||||
ctx.font = "bold 16px sans-serif";
|
ctx.textBaseline = "middle";
|
||||||
ctx.fillText(`Wait for options ${options.join(', ')}`, cs / 2, cs / 2 + 50)
|
ctx.font = "bold 48px sans-serif";
|
||||||
}
|
if (player.health <= 0) ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
|
||||||
|
if (player.isMenu) {
|
||||||
|
let r = Math.floor(Math.abs(player.rot / 1.2) % emojis.length);
|
||||||
|
ctx.fillText(`Click to react ${emojis[r]}`, cs / 2, cs / 2)
|
||||||
|
ctx.font = "bold 16px sans-serif";
|
||||||
|
ctx.fillText(`Wait for emojis ${emojis.join(', ')}`, cs / 2, cs / 2 + 50)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
click() {
|
click() {
|
||||||
|
@ -121,7 +126,7 @@ class Game extends GameBasic {
|
||||||
this.player = new Player(false, true);
|
this.player = new Player(false, true);
|
||||||
this.entities.push(this.player);
|
this.entities.push(this.player);
|
||||||
} else if (player.isMenu) {
|
} else if (player.isMenu) {
|
||||||
player.r = Math.floor(Math.abs(player.rot / 1.2) % options.length);
|
player.r = Math.floor(Math.abs(player.rot / 1.2) % emojis.length);
|
||||||
player.isMenu = false;
|
player.isMenu = false;
|
||||||
this.sync();
|
this.sync();
|
||||||
} else {
|
} else {
|
||||||
|
@ -147,7 +152,7 @@ class Game extends GameBasic {
|
||||||
let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type];
|
let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type];
|
||||||
x.handleTick = type.prototype.handleTick;
|
x.handleTick = type.prototype.handleTick;
|
||||||
if (x.r != 1) {
|
if (x.r != 1) {
|
||||||
let a = new Audio(`assets/${options[x.r]}.wav`);
|
let a = new Audio(`assets/${emojis[x.r]}.wav`);
|
||||||
a.addEventListener('ended', () => {
|
a.addEventListener('ended', () => {
|
||||||
if (x.you == you) {
|
if (x.you == you) {
|
||||||
that.player.playing = false;
|
that.player.playing = false;
|
||||||
|
|
Loading…
Reference in a new issue