humus-old/server/game.js

178 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-03-20 15:28:18 -05:00
// Node.
import {performance} from 'perf_hooks';
// 3rd party.
import immutablediff from 'immutablediff';
// 2nd party.
import {
create as createEntity,
EntityList,
registerTrait,
} from '@avocado/entity';
2019-03-23 18:32:16 -05:00
import {World} from '@avocado/physics/dummy';
2019-03-20 15:28:18 -05:00
import {StateSynchronizer} from '@avocado/state';
// Create game.
export default function(avocadoServer) {
avocadoServer.on('connect', createConnectionListener(avocadoServer));
setInterval(createMainLoop(avocadoServer), 1000 / 80);
}
// Entity tracking.
const entityList = new EntityList();
2019-03-23 18:32:16 -05:00
entityList.world = new World();
const informables = [];
2019-03-20 15:28:18 -05:00
const stateSynchronizer = new StateSynchronizer({
entityList,
});
for (let i = 0; i < 20; ++i) {
const x = Math.floor(Math.random() * 1000) + 100;
const y = Math.floor(Math.random() * 500) + 100;
const flowerBarrel = createFlowerBarrelEntity([x, y]);
entityList.addEntity(flowerBarrel);
}
2019-03-20 15:28:18 -05:00
// Connection listener.
function createConnectionListener(avocadoServer) {
return (socket) => {
// Create and track a new entity for the connection.
const entity = createEntityForConnection(socket);
2019-03-20 15:28:18 -05:00
entityList.addEntity(entity);
// Listen for events.
socket.on('message', createMessageListener(avocadoServer, socket));
socket.on('disconnect', createDisconnectionListener(avocadoServer, socket));
}
}
// Handle incoming messages.
function createMessageListener(avocadoServer, socket) {
const {entity} = socket;
return ({type, payload}) => {
switch (type) {
case 'input':
entity.inputState = payload;
break;
}
};
}
// Handle disconnection.
function createDisconnectionListener(avocadoServer, socket) {
const {entity} = socket;
return () => {
entity.destroy();
2019-03-20 15:28:18 -05:00
};
}
// Create an entity for a new connection.
function createEntityForConnection(socket) {
let entity = createEntity();
entity = entity.fromJSON({
2019-03-20 15:28:18 -05:00
traits: {
animated: {
params: {
animations: {
idle: {
offset: [0, -48],
uri: '/idle.animation.json',
},
moving: {
offset: [0, -48],
uri: '/moving.animation.json',
},
}
},
},
controllable: {},
directional: {
params: {
directionCount: 4,
},
},
existent: {},
graphical: {},
informed: {},
2019-03-20 15:28:18 -05:00
mobile: {
state: {
speed: 400,
},
},
2019-03-22 13:40:44 -05:00
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [32, 32],
}
}
},
2019-03-20 15:28:18 -05:00
positioned: {
state: {
x: 100,
y: 100,
},
},
},
});
// 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;
2019-03-20 15:28:18 -05:00
}
2019-03-20 20:15:14 -05:00
// Create a flower barrel.
function createFlowerBarrelEntity(position) {
const entity = createEntity();
return entity.fromJSON({
traits: {
pictured: {
params: {
images: {
initial: {
offset: [0, -32],
2019-03-21 20:01:40 -05:00
size: [128, 128], // Derive?
2019-03-20 20:15:14 -05:00
uri: '/flower-barrel.png',
},
}
},
},
existent: {},
graphical: {},
2019-03-22 13:40:44 -05:00
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [32, 32],
}
}
},
2019-03-20 20:15:14 -05:00
positioned: {
state: {
x: position[0],
y: position[1],
},
},
},
});
}
2019-03-20 15:28:18 -05:00
// Main loop.
let lastTime = performance.now();
function createMainLoop(avocadoServer) {
return () => {
const now = performance.now();
const elapsed = (now - lastTime) / 1000;
lastTime = now;
entityList.tick(elapsed);
const diff = stateSynchronizer.diff();
if (StateSynchronizer.noChange === diff) {
return;
}
2019-03-20 18:35:59 -05:00
// All informed entities get their own slice.
for (const entity of informables) {
2019-03-21 01:14:39 -05:00
const CAMERA_LOCATION = [640, 360];
const reduced = entity.reduceStateDiff(diff, CAMERA_LOCATION);
2019-03-20 18:58:19 -05:00
entity.inform(reduced);
2019-03-20 18:35:59 -05:00
}
2019-03-20 15:28:18 -05:00
}
}