refactor: controllable is app level
This commit is contained in:
parent
a7a342cc36
commit
d2c850ed32
47
traits/controllable.js
Normal file
47
traits/controllable.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
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