make npcs less annoying

This commit is contained in:
biglyderv 2024-10-18 19:30:00 -04:00
parent c330f0d31a
commit a1156f1f8e
6 changed files with 45 additions and 30 deletions

23
common/entity.js Normal file
View file

@ -0,0 +1,23 @@
class Entity {
constructor(game) {
let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y: (Math.random() - 0.5) * (game ? game.height : 1000) };
let vel = { x: 0, y: 0 };
this.pos = pos;
this.vel = vel;
}
bounce(game) {
let ent = this;
let { width, height } = game;
if (Math.abs(ent.pos.x) >= width / 2) {
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
}
if (Math.abs(ent.pos.y) >= height / 2) {
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
}
}
}
export default Entity;

View file

@ -2,7 +2,7 @@ import Player from "./player.js";
class GameBasic {
constructor(width = 7500, height = 7500) {
let player = new Player(false, true);
let player = new Player(false, true, this);
let entities = [player];
/*for (let i = 0; i < 50; i++) {

View file

@ -1,12 +1,9 @@
import { distF, uuidv4 } from './util.js'
import Entity from "./entity.js";
class NPC {
constructor(you) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let vel = { x: 0, y: 0 };
this.pos = pos;
this.vel = vel;
class NPC extends Entity {
constructor(you, game = false) {
super(game);
this.you = you || uuidv4();
@ -38,14 +35,16 @@ class NPC {
let distX = (this.destX - this.pos.x);
let distY = (this.destY - this.pos.y)
this.vel.x += (distX + distY * this.rFac / 1000) * 0.001;
this.vel.y += (distY - distX * this.rFac / 1000) * 0.001;
this.vel.x += (distX + distY * this.rFac / 1000) * 0.0003;
this.vel.y += (distY - distX * this.rFac / 1000) * 0.0003;
this.vel.x *= 0.8;
this.vel.y *= 0.8;
this.vel.x *= 0.98;
this.vel.y *= 0.98;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
this.bounce(game);
}
}

View file

@ -1,14 +1,12 @@
import { distF, uuidv4 } from './util.js'
import Entity from './entity.js';
class Player {
class Player extends Entity {
constructor(you, isPlayer, game = false) {
let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y:(Math.random() - 0.5) * (game ? game.height : 1000) };
let camera = { x: -pos.x, y: -pos.y };
let vel = { x: 0, y: 0 };
super(game);
this.camera = { x: -this.pos.x, y: -this.pos.y };
this.camera = camera;
this.pos = pos;
this.vel = vel;
this.rot = 0;
this.dir = 1;
this.ticks = 0;
@ -65,13 +63,7 @@ class Player {
ent.pos.x = Math.max(Math.min(ent.pos.x, width / 2), - width / 2);
ent.pos.y = Math.max(Math.min(ent.pos.y, height / 2), - height / 2);
if (Math.abs(ent.pos.x) >= width / 2 ) {
ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x);
}
if (Math.abs(ent.pos.y) >= height / 2) {
ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y);
}
ent.bounce(game);
ent.vel.x *= 0.9;
ent.vel.y *= 0.9;
@ -97,7 +89,7 @@ class Player {
dp /= Math.sqrt(dist + 0.1);
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
if (target.type == 'NPC' && velness > 0.2) {
if (target.type == 'NPC' && velness > 0.2 && ent.ticks >= 15) {
ent.isMenu = true;
ent.rot = 1.2;
}

View file

@ -99,7 +99,7 @@ class Game extends GameBasic {
that.entities = [];
for (let i = 0; i < 20; i++) {
that.entities.push(new NPC())
that.entities.push(new NPC(false,that))
}
setInterval(function () { that.sync(false) }, 1000 / 10);

View file

@ -175,6 +175,7 @@ class Game extends GameBasic {
}
x.handleTick = type.prototype.handleTick;
x.bounce = type.prototype.bounce;
if (x.r != 1 && type == Player) {
let a = new Audio(`sfx/${emojis[x.r]}.wav`);
a.addEventListener('ended', () => {