significant optimzations
This commit is contained in:
parent
3ce6ded308
commit
37b516faee
5 changed files with 71 additions and 20 deletions
|
@ -13,6 +13,10 @@ class NPC {
|
|||
this.type = 'NPC';
|
||||
|
||||
this.immortal = true;
|
||||
|
||||
this.serverProps = [
|
||||
'type', 'pos','vel','you','immortal'
|
||||
];
|
||||
}
|
||||
handleTick(game) {
|
||||
let { entities } = game;
|
||||
|
|
|
@ -26,11 +26,19 @@ class Player {
|
|||
this.isMenu = false;
|
||||
|
||||
this.r = 1;
|
||||
|
||||
this.serverProps = [
|
||||
'type', 'camera','pos','vel','rot','dir','ticks','health','you','isPlayer','headCount','isMenu','r'
|
||||
];
|
||||
|
||||
this.legalProps = [
|
||||
'vel','dir','camera','ticks','isMenu','r'
|
||||
];
|
||||
}
|
||||
bump() {
|
||||
let player = this;
|
||||
|
||||
if (player.ticks < 7) {
|
||||
if (player.ticks < 15) {
|
||||
player.dir *= -1;
|
||||
}
|
||||
|
||||
|
|
32
game.js
32
game.js
|
@ -6,19 +6,40 @@ class Game extends GameBasic {
|
|||
super();
|
||||
this.ws = [];
|
||||
}
|
||||
sync() {
|
||||
sync(full = false) {
|
||||
let { entities } = this;
|
||||
let entList = [];
|
||||
for (let entity of entities) {
|
||||
let { pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing } = entity;
|
||||
entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing });
|
||||
entity.pos = { x: Math.round(entity.pos.x), y: Math.round(entity.pos.y) };
|
||||
entity.vel = { x: Math.round(entity.vel.x), y: Math.round(entity.vel.y) };
|
||||
entList.push(entity);
|
||||
}
|
||||
|
||||
if (entList.length == 0) return;
|
||||
|
||||
for (let client of this.ws) {
|
||||
if (!client.active) continue;
|
||||
client.send(JSON.stringify(entList));
|
||||
|
||||
let wsEnt = client.ent;
|
||||
if (!wsEnt) continue;
|
||||
|
||||
let filtered;
|
||||
|
||||
if (!full) {
|
||||
filtered = entList.filter((ent) =>
|
||||
(Math.sqrt(((ent.pos.x - wsEnt.pos.x) ** 2) + ((ent.pos.y - wsEnt.pos.y) ** 2))) < 777
|
||||
);
|
||||
|
||||
} else {
|
||||
filtered = entList;
|
||||
}
|
||||
|
||||
filtered= filtered.map(x => {
|
||||
let props = x.serverProps;
|
||||
return props.map(prop => x[prop]);
|
||||
});
|
||||
|
||||
client.send(JSON.stringify(filtered));
|
||||
}
|
||||
|
||||
for (let entity of entities) {
|
||||
|
@ -35,7 +56,8 @@ class Game extends GameBasic {
|
|||
that.entities.push(new NPC())
|
||||
}
|
||||
|
||||
setInterval(function () { that.sync() }, 1000 / 5);
|
||||
setInterval(function () { that.sync(false) }, 1000 / 10);
|
||||
setInterval(function () { that.sync(true) }, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
22
index.js
22
index.js
|
@ -1,6 +1,7 @@
|
|||
import express from "express";
|
||||
import expressW from "express-ws";
|
||||
import Game from "./game.js";
|
||||
import NPC from "./common/npc.js";
|
||||
import Player from "./common/player.js";
|
||||
|
||||
var app = express();
|
||||
|
@ -21,6 +22,7 @@ app.ws('/', function (ws, req) {
|
|||
game.entities[playerI] = player;
|
||||
|
||||
ws.active = true;
|
||||
ws.ent = player;
|
||||
|
||||
// This will only work under NGINX.
|
||||
console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`)
|
||||
|
@ -31,18 +33,24 @@ app.ws('/', function (ws, req) {
|
|||
data = JSON.parse(msg);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
data = {};
|
||||
data = [];
|
||||
}
|
||||
let { vel, dir, you, ticks, isMenu, r, playing } = data;
|
||||
let data2 = { vel, dir, you, ticks, isMenu, r, playing };
|
||||
|
||||
let you2 = game.entities[playerI].you;
|
||||
let newEnt = game.entities[playerI];
|
||||
|
||||
let {you} = newEnt;
|
||||
|
||||
let props = player.legalProps;
|
||||
|
||||
for (let i in props) {
|
||||
newEnt[props[i]] = data[i];
|
||||
}
|
||||
|
||||
let you2 = newEnt.you;
|
||||
|
||||
if (you != you2) {
|
||||
console.log(`A player ${you2} now identifies as ${you}`);
|
||||
console.log(`A player ${you} now identifies as ${you2}`);
|
||||
}
|
||||
|
||||
game.entities[playerI] = Object.assign(game.entities[playerI], data2);
|
||||
})
|
||||
|
||||
ws.on('close', function () {
|
||||
|
|
|
@ -137,9 +137,7 @@ class Game extends GameBasic {
|
|||
sync() {
|
||||
let { player } = this;
|
||||
|
||||
let { vel, dir, you, ticks, isMenu, r, playing } = player;
|
||||
|
||||
this.ws.send(JSON.stringify({ vel, dir, you, ticks, isMenu, r, playing }));
|
||||
this.ws.send(JSON.stringify(player.legalProps.map(prop => player[prop])));
|
||||
}
|
||||
recv({ data }) {
|
||||
let { player } = this;
|
||||
|
@ -148,10 +146,19 @@ class Game extends GameBasic {
|
|||
let that = this;
|
||||
|
||||
let entList = JSON.parse(data);
|
||||
entList = entList.map(x => {
|
||||
let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type];
|
||||
entList = entList.map(y => {
|
||||
let type = (Object.keys(legalTypes).indexOf(y[0]) == -1) ? Player : legalTypes[y[0]];
|
||||
|
||||
let props = new type().serverProps;
|
||||
|
||||
let x = {};
|
||||
|
||||
for (let i in props) {
|
||||
x[props[i]] = y[i];
|
||||
}
|
||||
|
||||
x.handleTick = type.prototype.handleTick;
|
||||
if (x.r != 1) {
|
||||
if (x.r != 1 && type == 'Player') {
|
||||
let a = new Audio(`assets/${emojis[x.r]}.wav`);
|
||||
a.addEventListener('ended', () => {
|
||||
if (x.you == you) {
|
||||
|
@ -170,6 +177,8 @@ class Game extends GameBasic {
|
|||
|
||||
let matchingPlayer = entList.filter(x => x.you == you)
|
||||
|
||||
if (matchingPlayer.length == 0) matchingPlayer = entList.filter(x => x.type == 'Player');
|
||||
|
||||
this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]);
|
||||
|
||||
if (this.player.r != 1) {
|
||||
|
|
Loading…
Reference in a new issue