refactor: break up entity/room ops

This commit is contained in:
cha0s 2019-03-27 15:51:58 -05:00
parent 1ebcb177bf
commit aa1a69d0b9
3 changed files with 136 additions and 136 deletions

View File

@ -0,0 +1,55 @@
import {create as createEntity} from '@avocado/entity';
// Create an entity for a new connection.
export function createEntityForConnection(socket) {
let entity = createEntity();
entity = entity.fromJSON({
traits: {
animated: {
params: {
animations: {
idle: {
offset: [0, -12],
uri: '/idle.animation.json',
},
moving: {
offset: [0, -12],
uri: '/moving.animation.json',
},
}
},
},
directional: {
params: {
directionCount: 4,
},
},
existent: {},
graphical: {},
mobile: {
state: {
speed: 100,
},
},
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [8, 8],
}
}
},
positioned: {
state: {
x: 100,
y: 100,
},
},
},
});
entity.addTrait('controllable');
entity.addTrait('informed');
// Embed socket.
entity.socket = socket;
return entity;
}

View File

@ -0,0 +1,67 @@
import {World} from '@avocado/physics/matter/world';
import {Room} from '@avocado/topdown';
// A flower barrel.
function flowerBarrelJSON(position) {
return {
traits: {
pictured: {
params: {
images: {
initial: {
offset: [0, -8],
size: [32, 32], // Derive?
uri: '/flower-barrel.png',
},
}
},
},
existent: {},
graphical: {},
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [20, 10],
}
}
},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
},
};
}
// Room.
const roomJSON = {
layers: [
{
entities: [],
tiles: {
size: [12, 6],
data: [
1, 5, 6, 7, 1, 2, 3, 4, 1, 5, 6, 7,
1, 5, 6, 7, 1, 66, 67, 68, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
],
},
tilesetUri: '/tileset.json',
},
],
};
for (let i = 0; i < 10; ++i) {
const x = Math.floor(Math.random() * 200) + 50;
const y = Math.floor(Math.random() * 75) + 50;
roomJSON.layers[0].entities.push(flowerBarrelJSON([x, y]));
}
export function createRoom() {
const room = new Room()
room.world = new World();
return room.fromJSON(roomJSON);
}

View File

@ -3,47 +3,17 @@ import {performance} from 'perf_hooks';
// 3rd party.
import immutablediff from 'immutablediff';
// 2nd party.
import {
create as createEntity,
EntityList,
registerTrait,
} from '@avocado/entity';
import {World} from '@avocado/physics/matter/world';
import {StateSynchronizer} from '@avocado/state';
import {Room} from '@avocado/topdown';
// 1st party.
import {createEntityForConnection} from './create-entity-for-connection';
import {createRoom} from './create-server-room';
// Create game.
export default function(avocadoServer) {
avocadoServer.on('connect', createConnectionListener(avocadoServer));
setInterval(createMainLoop(avocadoServer), 1000 / 80);
}
// Room.
const roomJSON = {
layers: [
{
entities: [],
tiles: {
size: [12, 6],
data: [
1, 5, 6, 7, 1, 2, 3, 4, 1, 5, 6, 7,
1, 5, 6, 7, 1, 66, 67, 68, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
1, 5, 6, 7, 1, 5, 6, 7, 1, 5, 6, 7,
],
},
tilesetUri: '/tileset.json',
},
],
};
for (let i = 0; i < 10; ++i) {
const x = Math.floor(Math.random() * 200) + 50;
const y = Math.floor(Math.random() * 75) + 50;
roomJSON.layers[0].entities.push(flowerBarrelJSON([x, y]));
}
const room = new Room()
room.world = new World();
room.fromJSON(roomJSON);
// Create room.
const room = createRoom();
// Entity tracking.
const informables = [];
const stateSynchronizer = new StateSynchronizer({
@ -54,6 +24,15 @@ function createConnectionListener(avocadoServer) {
return (socket) => {
// Create and track a new entity for the connection.
const entity = createEntityForConnection(socket);
// Track informables.
informables.push(entity);
entity.on('destroyed', () => {
const index = informables.indexOf(entity);
if (-1 !== index) {
informables.splice(index, 1);
}
});
// Add entity to room.
room.addEntityToLayer(entity, 0);
// Listen for events.
socket.on('message', createMessageListener(avocadoServer, socket));
@ -78,107 +57,6 @@ function createDisconnectionListener(avocadoServer, socket) {
entity.destroy();
};
}
// Create an entity for a new connection.
function createEntityForConnection(socket) {
let entity = createEntity();
entity = entity.fromJSON({
traits: {
animated: {
params: {
animations: {
idle: {
offset: [0, -12],
uri: '/idle.animation.json',
},
moving: {
offset: [0, -12],
uri: '/moving.animation.json',
},
}
},
},
controllable: {},
directional: {
params: {
directionCount: 4,
},
},
existent: {},
graphical: {},
informed: {},
mobile: {
state: {
speed: 100,
},
},
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [8, 8],
}
}
},
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;
}
function flowerBarrelJSON(position) {
return {
traits: {
pictured: {
params: {
images: {
initial: {
offset: [0, -8],
size: [32, 32], // Derive?
uri: '/flower-barrel.png',
},
}
},
},
existent: {},
graphical: {},
physical: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [20, 10],
}
}
},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
},
};
}
// Create a flower barrel.
function createFlowerBarrelEntity(position) {
const entity = createEntity();
return entity.fromJSON(flowerBarrelJSON(position));
}
// Main loop.
let lastTime = performance.now();
function createMainLoop(avocadoServer) {