humus-old/common/traits/controllable.trait.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-03-21 00:31:44 -05:00
import * as I from 'immutable';
import {Trait} from '@avocado/entity';
// Input handling.
export class Controllable extends Trait {
initialize() {
2019-03-28 20:36:31 -05:00
this._inputState = I.Map();
2019-03-21 00:31:44 -05:00
}
set inputState(inputState) {
2019-03-28 20:36:31 -05:00
this._inputState = I.Map(inputState);
2019-03-21 00:31:44 -05:00
}
2019-04-12 13:05:39 -05:00
tick(elapsed) {
const {_inputState: inputState} = this;
if (0 === inputState.size) {
this.entity.currentAnimation = 'idle';
return;
}
const movementVector = [0, 0];
2019-04-13 19:16:42 -05:00
if (inputState.has('MoveToX')) {
movementVector[0] = inputState.get('MoveToX') / 127;
}
if (inputState.has('MoveToY')) {
movementVector[1] = inputState.get('MoveToY') / 127;
2019-04-12 13:05:39 -05:00
}
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';
}
2019-03-21 00:31:44 -05:00
}