fix: Controllable now first class
This commit is contained in:
parent
d959dcdacf
commit
c088ea06b9
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user