avocado-old/packages/entity/traits/directional.trait.js

44 lines
829 B
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import {compose} from '@avocado/core';
import {Vector} from '@avocado/math';
2019-03-23 23:24:18 -05:00
import {StateProperty, Trait} from '../trait';
2019-03-17 23:45:48 -05:00
const decorate = compose(
2019-03-23 23:24:18 -05:00
StateProperty('direction', {
2019-03-18 20:05:11 -05:00
track: true,
}),
2019-03-17 23:45:48 -05:00
);
class DirectionalBase extends Trait {
static defaultParams() {
return {
directionCount: 1,
trackMovement: true,
2019-03-17 23:45:48 -05:00
};
}
static defaultState() {
return {
direction: 0,
};
}
2019-03-19 11:25:42 -05:00
initialize() {
this.directionCount = this.params.get('directionCount');
}
2019-03-17 23:45:48 -05:00
listeners() {
const listeners = {};
if (this.params.get('trackMovement')) {
listeners.movementRequest = (vector) => {
this.entity.direction = Vector.toDirection(vector, this.directionCount);
};
2019-03-17 23:45:48 -05:00
}
return listeners;
2019-03-17 23:45:48 -05:00
}
}
export class Directional extends decorate(DirectionalBase) {}