From 4744df90d90d0c646bdeec5cbb38107ad8650da3 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 21 Mar 2019 01:14:39 -0500 Subject: [PATCH] refactor: more sensible implementation --- server/game.js | 5 ++--- traits/informed.js | 14 +++++--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/server/game.js b/server/game.js index 5b93ebe..5540f8d 100644 --- a/server/game.js +++ b/server/game.js @@ -32,8 +32,6 @@ function createConnectionListener(avocadoServer) { // Create and track a new entity for the connection. const entity = createEntityForConnection(socket); entityList.addEntity(entity); - // Right in the middle. TODO: camera. - entity.listLocator = () => [640, 360]; // Listen for events. socket.on('message', createMessageListener(avocadoServer, socket)); socket.on('disconnect', createDisconnectionListener(avocadoServer, socket)); @@ -150,7 +148,8 @@ function createMainLoop(avocadoServer) { } // All informed entities get their own slice. for (const entity of informables) { - const reduced = entity.reduceStateDiff(diff); + const CAMERA_LOCATION = [640, 360]; + const reduced = entity.reduceStateDiff(diff, CAMERA_LOCATION); entity.inform(reduced); } } diff --git a/traits/informed.js b/traits/informed.js index bb3ef38..fb304c4 100644 --- a/traits/informed.js +++ b/traits/informed.js @@ -26,9 +26,10 @@ class InformedBase extends Trait { delete this._socket; } - reduceStateDiffForEntityList(diff) { + reduceStateDiffForEntityList(diff, position) { // Reduce the entity list. - const nearbyEntities = this.entity.entitiesToInform(); + const informSize = this.entity.informSize.toJS(); + const nearbyEntities = this.entity.nearbyEntities(informSize, position); const reducedEntityList = {}; let nearby = I.Set(); for (const entity of nearbyEntities) { @@ -78,11 +79,6 @@ class InformedBase extends Trait { methods() { return { - entitiesToInform: () => { - const informSize = this.entity.informSize.toJS(); - return this.entity.nearbyEntities(informSize); - }, - inform: (diff) => { // Remove dead updates. if (0 === Object.keys(diff).length) { @@ -98,8 +94,8 @@ class InformedBase extends Trait { }); }, - reduceStateDiff: (diff) => { - return this.reduceStateDiffForEntityList(diff); + reduceStateDiff: (diff, position) => { + return this.reduceStateDiffForEntityList(diff, position); }, };