2024-11-25 14:14:20 -05:00
|
|
|
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';
|
2024-11-25 14:14:20 -05:00
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
this.immortal = true;
|
|
|
|
}
|
|
|
|
handleTick(game) {
|
|
|
|
let { entities } = game;
|
|
|
|
|
2024-11-25 14:14:20 -05:00
|
|
|
let rEntity, i = 0;
|
|
|
|
|
|
|
|
while ((!rEntity || rEntity.type != 'Player') && i < 100) {
|
|
|
|
rEntity = entities[Math.floor(Math.random() * entities.length)];
|
|
|
|
i++;
|
|
|
|
}
|
2024-11-25 14:14:20 -05:00
|
|
|
|
|
|
|
this.vel.x += (rEntity.pos.x - this.pos.x) * 0.0001;
|
|
|
|
this.vel.y += (rEntity.pos.y - this.pos.y) * 0.0001;
|
|
|
|
|
|
|
|
this.pos.x += this.vel.x;
|
|
|
|
this.pos.y += this.vel.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default NPC;
|