48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
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);
|
|
}
|
|
|
|
}
|
|
|