refactor: more sensible implementation

This commit is contained in:
cha0s 2019-03-21 01:14:39 -05:00
parent 6d5b00b44c
commit 4744df90d9
2 changed files with 7 additions and 12 deletions

View File

@ -32,8 +32,6 @@ function createConnectionListener(avocadoServer) {
// Create and track a new entity for the connection. // Create and track a new entity for the connection.
const entity = createEntityForConnection(socket); const entity = createEntityForConnection(socket);
entityList.addEntity(entity); entityList.addEntity(entity);
// Right in the middle. TODO: camera.
entity.listLocator = () => [640, 360];
// Listen for events. // Listen for events.
socket.on('message', createMessageListener(avocadoServer, socket)); socket.on('message', createMessageListener(avocadoServer, socket));
socket.on('disconnect', createDisconnectionListener(avocadoServer, socket)); socket.on('disconnect', createDisconnectionListener(avocadoServer, socket));
@ -150,7 +148,8 @@ function createMainLoop(avocadoServer) {
} }
// All informed entities get their own slice. // All informed entities get their own slice.
for (const entity of informables) { 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); entity.inform(reduced);
} }
} }

View File

@ -26,9 +26,10 @@ class InformedBase extends Trait {
delete this._socket; delete this._socket;
} }
reduceStateDiffForEntityList(diff) { reduceStateDiffForEntityList(diff, position) {
// Reduce the entity list. // Reduce the entity list.
const nearbyEntities = this.entity.entitiesToInform(); const informSize = this.entity.informSize.toJS();
const nearbyEntities = this.entity.nearbyEntities(informSize, position);
const reducedEntityList = {}; const reducedEntityList = {};
let nearby = I.Set(); let nearby = I.Set();
for (const entity of nearbyEntities) { for (const entity of nearbyEntities) {
@ -78,11 +79,6 @@ class InformedBase extends Trait {
methods() { methods() {
return { return {
entitiesToInform: () => {
const informSize = this.entity.informSize.toJS();
return this.entity.nearbyEntities(informSize);
},
inform: (diff) => { inform: (diff) => {
// Remove dead updates. // Remove dead updates.
if (0 === Object.keys(diff).length) { if (0 === Object.keys(diff).length) {
@ -98,8 +94,8 @@ class InformedBase extends Trait {
}); });
}, },
reduceStateDiff: (diff) => { reduceStateDiff: (diff, position) => {
return this.reduceStateDiffForEntityList(diff); return this.reduceStateDiffForEntityList(diff, position);
}, },
}; };