optimizations, NPC movement fixes again
This commit is contained in:
parent
55802223c5
commit
37dc747033
3 changed files with 25 additions and 21 deletions
|
@ -16,9 +16,12 @@ class GameBasic {
|
||||||
}
|
}
|
||||||
main() {
|
main() {
|
||||||
let { entities } = this;
|
let { entities } = this;
|
||||||
|
let that = this;
|
||||||
|
|
||||||
for (let ent of entities) {
|
for (let ent of entities) {
|
||||||
ent.handleTick(this);
|
(async function() {
|
||||||
|
ent.handleTick(that)
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
init() {
|
init() {
|
||||||
|
|
|
@ -14,34 +14,35 @@ class NPC {
|
||||||
|
|
||||||
this.immortal = true;
|
this.immortal = true;
|
||||||
|
|
||||||
this.posOff = { x: 0, y: 0 };
|
|
||||||
|
|
||||||
this.serverProps = [
|
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) {
|
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) {
|
this.destX = Math.sin(this.rFac) * ((this.rFac / 100) ** 4) * game.width;
|
||||||
rEntity = entities[Math.floor(Math.random() * entities.length)];
|
this.destY = Math.cos(this.rFac) * ((this.rFac / 100) ** 4) * game.height;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.vel.x += (distX + distY * this.rFac / 1000) * 0.001;
|
||||||
this.posOff.x = (Math.random() * 50 - 25) * jitter;
|
this.vel.y += (distY - distX * this.rFac / 1000) * 0.001;
|
||||||
this.posOff.y = (Math.random() * 50 - 25) * jitter;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.vel.x += (rEntity.pos.x - this.pos.x + this.posOff.x) * 0.0002;
|
this.vel.x *= 0.8;
|
||||||
this.vel.y += (rEntity.pos.y - this.pos.y + this.posOff.y) * 0.0002;
|
this.vel.y *= 0.8;
|
||||||
|
|
||||||
this.vel.x *= 0.995;
|
|
||||||
this.vel.y *= 0.995;
|
|
||||||
|
|
||||||
this.pos.x += this.vel.x;
|
this.pos.x += this.vel.x;
|
||||||
this.pos.y += this.vel.y;
|
this.pos.y += this.vel.y;
|
||||||
|
|
2
game.js
2
game.js
|
@ -64,7 +64,7 @@ class Game extends GameBasic {
|
||||||
let that = this;
|
let that = this;
|
||||||
that.entities = [];
|
that.entities = [];
|
||||||
|
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 20; i++) {
|
||||||
that.entities.push(new NPC())
|
that.entities.push(new NPC())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue