fix: Controllable now first class

This commit is contained in:
cha0s 2019-03-20 21:08:20 -05:00
parent d959dcdacf
commit c088ea06b9
2 changed files with 1 additions and 49 deletions

View File

@ -10,8 +10,6 @@ import {
} from '@avocado/entity';
import {StateSynchronizer} from '@avocado/state';
// Traits.
import {Controllable} from './traits/controllable';
registerTrait(Controllable);
import {Informed} from './traits/informed';
registerTrait(Informed);
// Create game.
@ -47,6 +45,7 @@ function createConnectionListener(avocadoServer) {
// Send current state.
const currentState = stateSynchronizer.state().toJS();
const reduced = entity.reduceStateDiff(currentState);
reduced.selfEntity = entity.instanceUuid;
entity.inform(reduced);
// Listen for events.
socket.on('message', createMessageListener(avocadoServer, socket));

View File

@ -1,47 +0,0 @@
import * as I from 'immutable';
import {Trait} from '@avocado/entity';
// Input handling.
export class Controllable extends Trait {
initialize() {
this._inputState = I.Set();
}
listeners() {
return {
tick: (elapsed) => {
const {_inputState: inputState} = this;
if (0 === inputState.size) {
this.entity.currentAnimation = 'idle';
return;
}
const movementVector = [0, 0];
if (inputState.has('MoveUp')) {
movementVector[1] -= 1;
}
if (inputState.has('MoveRight')) {
movementVector[0] += 1;
}
if (inputState.has('MoveDown')) {
movementVector[1] += 1;
}
if (inputState.has('MoveLeft')) {
movementVector[0] -= 1;
}
if (0 === movementVector[0] && 0 === movementVector[1]) {
return;
}
this.entity.requestMovement(movementVector);
this.entity.currentAnimation = 'moving';
},
}
}
set inputState(inputState) {
this._inputState = I.Set(inputState);
}
}