unibutton/common/npc.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

import { distF, uuidv4 } from './util.js'
2024-10-18 19:30:00 -04:00
import Entity from "./entity.js";
2024-10-18 19:30:00 -04:00
class NPC extends Entity {
constructor(you, game = false) {
super(game);
this.you = you || uuidv4();
this.type = 'NPC';
2024-10-01 18:41:44 -04:00
this.immortal = true;
2024-10-02 04:14:56 -04:00
this.serverProps = [
'type', 'pos', 'vel', 'you', 'immortal', 'destX', 'destY', 'ticks', 'rFac'
2024-10-02 04:14:56 -04:00
];
this.ticks = 601;
this.rFac = Math.random() * 100;
2024-10-01 18:41:44 -04:00
this.destX = 0;
this.destY = 0;
}
handleTick(game) {
this.ticks++;
if (this.ticks >= this.rFac * 10) {
this.ticks = 0;
this.rFac = (this.rFac * 2385 + 293) % 100;
this.destX = Math.sin(this.rFac) * ((this.rFac / 100) ** 4) * game.width;
this.destY = Math.cos(this.rFac) * ((this.rFac / 100) ** 4) * game.height;
2024-10-01 18:41:44 -04:00
}
let distX = (this.destX - this.pos.x);
let distY = (this.destY - this.pos.y)
2024-10-02 19:35:20 -04:00
2024-10-18 19:30:00 -04:00
this.vel.x += (distX + distY * this.rFac / 1000) * 0.0003;
this.vel.y += (distY - distX * this.rFac / 1000) * 0.0003;
2024-10-01 18:54:04 -04:00
2024-10-18 19:30:00 -04:00
this.vel.x *= 0.98;
this.vel.y *= 0.98;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
2024-10-18 19:30:00 -04:00
this.bounce(game);
}
}
export default NPC;