2019-04-28 23:45:03 -05:00
|
|
|
import {compose, EventEmitter} from '@avocado/core';
|
2019-04-10 21:02:42 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
2019-03-17 23:45:48 -05:00
|
|
|
|
2019-04-10 21:02:42 -05:00
|
|
|
import {Trait} from '../trait';
|
2019-09-30 01:36:02 -05:00
|
|
|
import {TraitUpdatePositionedPositionPacket} from '../packets/trait-update-positioned-position.packet';
|
2019-03-17 23:45:48 -05:00
|
|
|
|
|
|
|
const decorate = compose(
|
2019-04-10 21:02:42 -05:00
|
|
|
EventEmitter,
|
|
|
|
Vector.Mixin('_position', 'x', 'y', {
|
2019-03-18 20:05:00 -05:00
|
|
|
track: true,
|
|
|
|
}),
|
2019-04-12 09:48:43 -05:00
|
|
|
Vector.Mixin('serverPosition', 'serverX', 'serverY', {
|
|
|
|
track: true,
|
|
|
|
}),
|
2019-03-17 23:45:48 -05:00
|
|
|
);
|
|
|
|
|
2019-04-10 21:02:42 -05:00
|
|
|
// < 16768 will pack into 1 short per axe and give +/- 0.25 precision.
|
2019-05-05 04:26:35 -05:00
|
|
|
export class Positioned extends decorate(Trait) {
|
2019-03-17 23:45:48 -05:00
|
|
|
|
|
|
|
static defaultState() {
|
|
|
|
return {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:26:35 -05:00
|
|
|
static type() {
|
|
|
|
return 'positioned';
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:03:43 -05:00
|
|
|
constructor(entity, params, state) {
|
|
|
|
super(entity, params, state);
|
2019-04-30 17:11:41 -05:00
|
|
|
this.on('_positionChanged', this.on_positionChanged, this);
|
2019-09-30 01:36:02 -05:00
|
|
|
const x = this.state.x;
|
|
|
|
const y = this.state.y;
|
2019-04-10 21:02:42 -05:00
|
|
|
this._position = [x, y];
|
2019-05-08 23:54:30 -05:00
|
|
|
this.entity.position[0] = x;
|
|
|
|
this.entity.position[1] = y;
|
2019-04-23 16:57:42 -05:00
|
|
|
if (AVOCADO_CLIENT) {
|
|
|
|
this.serverPosition = this._position;
|
|
|
|
this.serverPositionDirty = false;
|
2019-04-30 17:11:41 -05:00
|
|
|
this.on('serverPositionChanged', this.onServerPositionChanged, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.off('_positionChanged', this.on_positionChanged);
|
|
|
|
if (AVOCADO_CLIENT) {
|
|
|
|
this.off('serverPositionChanged', this.onServerPositionChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:07:51 -05:00
|
|
|
acceptPacket(packet) {
|
2019-09-30 01:36:02 -05:00
|
|
|
if (packet instanceof TraitUpdatePositionedPositionPacket) {
|
|
|
|
[this.serverX, this.serverY] = packet.data.position;
|
2019-05-13 21:07:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:11:41 -05:00
|
|
|
on_positionChanged(oldPosition, newPosition) {
|
2019-05-08 23:54:30 -05:00
|
|
|
this.entity.position[0] = newPosition[0];
|
|
|
|
this.entity.position[1] = newPosition[1];
|
2019-04-30 17:11:41 -05:00
|
|
|
if (AVOCADO_SERVER) {
|
2019-09-30 01:36:02 -05:00
|
|
|
this.state.x = newPosition[0];
|
|
|
|
this.state.y = newPosition[1];
|
2019-10-03 00:53:56 -05:00
|
|
|
this.entity._fastDirtyCheck = true;
|
|
|
|
this._fastDirtyCheck = true;
|
2019-04-30 17:11:41 -05:00
|
|
|
}
|
|
|
|
this.entity.emit('positionChanged', oldPosition, newPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
onServerPositionChanged() {
|
|
|
|
this.serverPositionDirty = true;
|
2019-04-12 09:48:43 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 01:36:02 -05:00
|
|
|
packets(informed) {
|
2019-09-30 15:47:25 -05:00
|
|
|
const {x, y} = this.stateDifferences();
|
|
|
|
if (x || y) {
|
2019-09-30 15:35:57 -05:00
|
|
|
return new TraitUpdatePositionedPositionPacket({
|
2019-10-10 23:50:31 -05:00
|
|
|
position: this.entity.position,
|
2019-09-30 15:35:57 -05:00
|
|
|
});
|
2019-04-12 09:48:43 -05:00
|
|
|
}
|
2019-04-10 21:02:42 -05:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:48:43 -05:00
|
|
|
listeners() {
|
|
|
|
return {
|
|
|
|
|
2019-04-12 12:27:32 -05:00
|
|
|
isTickingChanged: () => {
|
|
|
|
// Snap position on ticking change.
|
2019-04-23 16:57:42 -05:00
|
|
|
if (AVOCADO_CLIENT) {
|
|
|
|
this._position = this.serverPosition;
|
|
|
|
}
|
2019-04-12 12:27:32 -05:00
|
|
|
},
|
|
|
|
|
2019-04-12 09:48:43 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-01 21:35:38 -05:00
|
|
|
methods() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
setPosition: (position) => {
|
|
|
|
this._position = position;
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-23 16:56:47 -05:00
|
|
|
renderTick(elapsed) {
|
2019-04-12 13:05:30 -05:00
|
|
|
if (!this.serverPositionDirty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Vector.equals(this._position, this.serverPosition)) {
|
2019-05-03 08:47:56 -05:00
|
|
|
this.serverPositionDirty = false;
|
2019-04-12 13:05:30 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Vector.equalsClose(this._position, this.serverPosition, 0.1)) {
|
|
|
|
this._position = this.serverPosition;
|
2019-05-03 08:47:56 -05:00
|
|
|
this.serverPositionDirty = false;
|
2019-04-12 13:05:30 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const diff = Vector.sub(this.serverPosition, this._position);
|
2019-10-10 23:50:31 -05:00
|
|
|
const lerp = .5;
|
2019-04-23 17:41:37 -05:00
|
|
|
this._position = Vector.add(this._position, Vector.scale(diff, lerp));
|
2019-04-12 13:05:30 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
}
|