98 lines
1.9 KiB
JavaScript
98 lines
1.9 KiB
JavaScript
import {compose} from '@avocado/core';
|
|
import {Vector} from '@avocado/math';
|
|
|
|
import {StateProperty, Trait} from '../trait';
|
|
import TraitUpdateDirectionalDirectionPacket from '../packets/trait-update-directional-direction.packet';
|
|
|
|
const decorate = compose(
|
|
StateProperty('direction', {
|
|
label: 'Direction',
|
|
track: true,
|
|
type: 'number',
|
|
}),
|
|
);
|
|
|
|
export default class Directional extends decorate(Trait) {
|
|
|
|
static defaultParams() {
|
|
return {
|
|
directionCount: 1,
|
|
trackMovement: true,
|
|
};
|
|
}
|
|
|
|
static defaultState() {
|
|
return {
|
|
direction: 0,
|
|
};
|
|
}
|
|
|
|
static describeParams() {
|
|
return {
|
|
directionCount: {
|
|
type: 'number',
|
|
label: '# of directions',
|
|
options: {
|
|
1: 1,
|
|
4: 4,
|
|
},
|
|
},
|
|
trackMovement: {
|
|
type: 'bool',
|
|
label: 'Track movement',
|
|
},
|
|
};
|
|
}
|
|
|
|
static describeState() {
|
|
return {
|
|
direction: {
|
|
type: 'number',
|
|
label: 'Direction',
|
|
options: {
|
|
0: 'Up',
|
|
1: 'Right',
|
|
2: 'Down',
|
|
3: 'Left',
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
static type() {
|
|
return 'directional';
|
|
}
|
|
|
|
constructor(entity, params, state) {
|
|
super(entity, params, state);
|
|
this.directionCount = this.params.directionCount;
|
|
}
|
|
|
|
acceptPacket(packet) {
|
|
if (packet instanceof TraitUpdateDirectionalDirectionPacket) {
|
|
this.entity.direction = packet.data;
|
|
}
|
|
}
|
|
|
|
packets(informed) {
|
|
const {direction} = this.stateDifferences();
|
|
if (direction) {
|
|
return new TraitUpdateDirectionalDirectionPacket(direction.value);
|
|
}
|
|
}
|
|
|
|
listeners() {
|
|
const listeners = {};
|
|
if (this.params.trackMovement) {
|
|
listeners.movementRequest = (vector) => {
|
|
if (Vector.isZero(vector)) {
|
|
return;
|
|
}
|
|
this.entity.direction = Vector.toDirection(vector, this.directionCount);
|
|
};
|
|
}
|
|
return listeners;
|
|
}
|
|
|
|
}
|