From 37dc7470334e6936ec390a2bc29d46f2bf68cb44 Mon Sep 17 00:00:00 2001 From: onezDerv Date: Fri, 4 Oct 2024 02:56:58 -0400 Subject: [PATCH] optimizations, NPC movement fixes again --- common/game_basic.js | 5 ++++- common/npc.js | 39 ++++++++++++++++++++------------------- game.js | 2 +- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/common/game_basic.js b/common/game_basic.js index 65fd2b0..98c49ea 100644 --- a/common/game_basic.js +++ b/common/game_basic.js @@ -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() { diff --git a/common/npc.js b/common/npc.js index ae653c8..3fbd087 100644 --- a/common/npc.js +++ b/common/npc.js @@ -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; - - while ((!rEntity || rEntity.type != 'Player' || rEntity.health <= 0) && i < 100) { - rEntity = entities[Math.floor(Math.random() * entities.length)]; - i++; + 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; } - 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; diff --git a/game.js b/game.js index 8db4553..2a4e027 100644 --- a/game.js +++ b/game.js @@ -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()) }