optimizations, NPC movement fixes again

This commit is contained in:
biglyderv 2024-10-04 02:56:58 -04:00
parent 55802223c5
commit 37dc747033
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
3 changed files with 25 additions and 21 deletions

View file

@ -16,9 +16,12 @@ class GameBasic {
}
main() {
let { entities } = this;
let that = this;
for (let ent of entities) {
ent.handleTick(this);
(async function() {
ent.handleTick(that)
})();
}
}
init() {

View file

@ -14,34 +14,35 @@ class NPC {
this.immortal = true;
this.posOff = { x: 0, y: 0 };
this.serverProps = [
'type', 'pos', 'vel', 'you', 'immortal', 'posOff'
'type', 'pos', 'vel', 'you', 'immortal', 'destX', 'destY', 'ticks', 'rFac'
];
this.ticks = 601;
this.rFac = Math.random() * 100;
this.destX = 0;
this.destY = 0;
}
handleTick(game) {
let { entities } = game;
this.ticks++;
let rEntity, i = 0;
if (this.ticks >= this.rFac * 10) {
this.ticks = 0;
this.rFac = (this.rFac * 2385 + 293) % 100;
while ((!rEntity || rEntity.type != 'Player' || rEntity.health <= 0) && i < 100) {
rEntity = entities[Math.floor(Math.random() * entities.length)];
i++;
this.destX = Math.sin(this.rFac) * ((this.rFac / 100) ** 4) * game.width;
this.destY = Math.cos(this.rFac) * ((this.rFac / 100) ** 4) * game.height;
}
let jitter = entities.filter(x => x.type == 'Player' && x && x.health > 0).length;
let distX = (this.destX - this.pos.x);
let distY = (this.destY - this.pos.y)
if (Math.random() < 0.01) {
this.posOff.x = (Math.random() * 50 - 25) * jitter;
this.posOff.y = (Math.random() * 50 - 25) * jitter;
}
this.vel.x += (distX + distY * this.rFac / 1000) * 0.001;
this.vel.y += (distY - distX * this.rFac / 1000) * 0.001;
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.vel.x *= 0.8;
this.vel.y *= 0.8;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;

View file

@ -64,7 +64,7 @@ class Game extends GameBasic {
let that = this;
that.entities = [];
for (let i = 0; i < 10; i++) {
for (let i = 0; i < 20; i++) {
that.entities.push(new NPC())
}