diff --git a/server/game.js b/server/game.js index 3d67194..5b93ebe 100644 --- a/server/game.js +++ b/server/game.js @@ -16,6 +16,7 @@ export default function(avocadoServer) { } // Entity tracking. const entityList = new EntityList(); +const informables = []; const stateSynchronizer = new StateSynchronizer({ entityList, }); @@ -29,11 +30,10 @@ for (let i = 0; i < 20; ++i) { function createConnectionListener(avocadoServer) { return (socket) => { // Create and track a new entity for the connection. - const entity = createEntityForConnection(); + const entity = createEntityForConnection(socket); entityList.addEntity(entity); // Right in the middle. TODO: camera. entity.listLocator = () => [640, 360]; - entity.socket = socket; // Listen for events. socket.on('message', createMessageListener(avocadoServer, socket)); socket.on('disconnect', createDisconnectionListener(avocadoServer, socket)); @@ -58,9 +58,9 @@ function createDisconnectionListener(avocadoServer, socket) { }; } // Create an entity for a new connection. -function createEntityForConnection() { - const entity = createEntity(); - return entity.fromJSON({ +function createEntityForConnection(socket) { + let entity = createEntity(); + entity = entity.fromJSON({ traits: { animated: { params: { @@ -98,6 +98,17 @@ function createEntityForConnection() { }, }, }); + // Track informables. + informables.push(entity); + entity.on('destroyed', () => { + const index = informables.indexOf(entity); + if (-1 !== index) { + informables.splice(index, 1); + } + }); + // Embed socket. + entity.socket = socket; + return entity; } // Create a flower barrel. function createFlowerBarrelEntity(position) { @@ -138,10 +149,7 @@ function createMainLoop(avocadoServer) { return; } // All informed entities get their own slice. - for (const entity of entityList) { - if (!('inform' in entity)) { - continue; - } + for (const entity of informables) { const reduced = entity.reduceStateDiff(diff); entity.inform(reduced); }