unibutton/common/npc.js
2024-11-25 14:14:20 -05:00

49 lines
No EOL
1.2 KiB
JavaScript

import { distF, uuidv4 } from './util.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;
this.you = you || uuidv4();
this.type = 'NPC';
this.immortal = true;
this.posOff = { x: 0, y: 0 };
this.serverProps = [
'type', 'pos', 'vel', 'you', 'immortal', 'posOff'
];
}
handleTick(game) {
let { entities } = game;
let rEntity, i = 0;
while ((!rEntity || rEntity.type != 'Player') && i < 100) {
rEntity = entities[Math.floor(Math.random() * entities.length)];
i++;
}
if (Math.random() < 0.01) {
this.posOff.x = (Math.random() * 50 - 25) * entities.length;
this.posOff.y = (Math.random() * 50 - 25) * entities.length;
}
this.vel.x += (rEntity.pos.x - this.pos.x + this.posOff.x) * 0.0002;
this.vel.y += (rEntity.pos.y - this.pos.y + this.posOff.y) * 0.0002;
this.vel.x *= 0.995;
this.vel.y *= 0.995;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
}
}
export default NPC;