feat: room controller, no predic

This commit is contained in:
cha0s 2019-03-27 01:53:10 -05:00
parent 1c686d7b67
commit 5f53f6e87f

View File

@ -4,27 +4,29 @@ import {ActionRegistry} from '@avocado/input';
import {Container, Renderer} from '@avocado/graphics';
import {World} from '@avocado/physics/matter/world';
import {StateSynchronizer} from '@avocado/state';
import {Room, RoomView} from '@avocado/topdown';
const renderer = new Renderer([640, 360]);
const stage = new Container();
stage.scale = [2, 2];
const room = new Room();
room.world = new World();
const roomView = new RoomView(room, renderer);
stage.addChild(roomView);
const entityList = new EntityList();
entityList.world = new World();
const stateSynchronizer = new StateSynchronizer({
entityList,
room,
});
let selfEntity;
function hasSelfEntity() {
return selfEntity && 'string' !== typeof selfEntity;
}
entityList.on('entityAdded', (entity) => {
// Add to graphics.
if ('container' in entity) {
stage.addChild(entity.container);
}
room.on('entityAdded', (entity) => {
// Set self entity.
if ('string' === typeof selfEntity) {
const entityList = room.layers.layers[0].entityList;
const mappedUuid = entityList.mappedUuid(selfEntity);
if (entity.instanceUuid === mappedUuid) {
selfEntity = entity;
@ -35,12 +37,6 @@ entityList.on('entityAdded', (entity) => {
entity.removeTrait('controllable');
}
});
entityList.on('entityRemoved', (entity) => {
// Remove from graphics.
if ('container' in entity) {
stage.removeChild(entity.container);
}
});
// Accept input.
const actionRegistry = new ActionRegistry();
actionRegistry.mapKeysToActions({
@ -72,10 +68,10 @@ const predictionHandle = setInterval(() => {
const now = performance.now();
const elapsed = (now - lastTime) / 1000;
lastTime = now;
if (hasSelfEntity()) {
selfEntity.inputState = actionState.toJS();
}
entityList.tick(elapsed);
// if (hasSelfEntity()) {
// selfEntity.inputState = actionState.toJS();
// }
// room.tick(elapsed);
}, 1000 / 80);
// State updates.
let dirty = false;