opt: less indirection

This commit is contained in:
cha0s 2022-04-01 10:35:33 -05:00
parent 8a92110505
commit 8afd4ac2a0

View File

@ -1,24 +1,15 @@
import {Vector} from '@avocado/math';
import {Trait} from '@avocado/traits';
import {compose, EventEmitter} from '@flecks/core';
import {compose} from '@flecks/core';
const decorate = compose(
EventEmitter,
Vector.Mixin('internalPosition', 'x', 'y', {
track: true,
}),
);
export default () => class Positioned extends decorate(Trait) {
constructor() {
super();
this.on('internalPositionChanged', this.onInternalPositionChanged, this);
}
acceptPacket(packet) {
if ('TraitUpdatePositionedPosition' === packet.constructor.type) {
this.internalPosition = packet.data;
this.onPositionChanged(packet.data);
}
}
@ -33,34 +24,31 @@ export default () => class Positioned extends decorate(Trait) {
super.cleanPackets();
}
destroy() {
super.destroy();
this.off('internalPositionChanged', this.onInternalPositionChanged);
}
async load(json) {
await super.load(json);
const {x, y} = this.state;
this.internalPosition = [x, y];
this.entity.position[0] = x;
this.entity.position[1] = y;
}
onInternalPositionChanged(oldPosition, newPosition) {
const xChanged = this.entity.position[0] !== newPosition[0];
const yChanged = this.entity.position[1] !== newPosition[1];
[this.entity.position[0], this.entity.position[1]] = newPosition;
if ('web' !== process.env.FLECKS_CORE_BUILD_TARGET) {
onPositionChanged(newPosition) {
const oldPosition = [this.entity.position[0], this.entity.position[1]];
const xChanged = oldPosition[0] !== newPosition[0];
const yChanged = oldPosition[1] !== newPosition[1];
if (xChanged || yChanged) {
[this.entity.position[0], this.entity.position[1]] = newPosition;
[this.state.x, this.state.y] = newPosition;
this.markAsDirty();
if ('web' !== process.env.FLECKS_CORE_BUILD_TARGET) {
this.markAsDirty();
}
if (xChanged) {
this.entity.emit('xChanged', oldPosition[0], newPosition[0]);
}
if (yChanged) {
this.entity.emit('yChanged', oldPosition[1], newPosition[1]);
}
this.entity.emit('positionChanged', oldPosition, newPosition);
}
if (xChanged) {
this.entity.emit('xChanged', oldPosition[0], newPosition[0]);
}
if (yChanged) {
this.entity.emit('yChanged', oldPosition[1], newPosition[1]);
}
this.entity.emit('positionChanged', oldPosition, newPosition);
}
packetsFor() {
@ -68,34 +56,23 @@ export default () => class Positioned extends decorate(Trait) {
if (x || y) {
return [[
'TraitUpdatePositionedPosition',
this.internalPosition,
this.entity.position,
]];
}
return [];
}
listeners() {
return {
positionChanged: () => {
if ('web' !== process.env.FLECKS_CORE_BUILD_TARGET) {
[this.state.x, this.state.y] = this.internalPosition;
this.markAsDirty();
}
},
};
}
methods() {
return {
distanceFrom: (other) => Vector.distance(this.internalPosition, other.position),
distanceFrom: (other) => Vector.distance(this.entity.position, other.position),
pointAround: (radius, angle) => {
if (!this.entity.is('Positioned')) {
return [0, 0];
}
return Vector.add(
this.internalPosition,
this.entity.position,
[
radius * Math.cos(angle),
radius * -Math.sin(angle),
@ -104,10 +81,26 @@ export default () => class Positioned extends decorate(Trait) {
},
setPosition: (position) => {
this.internalPosition = position;
this.onPositionChanged(position);
},
};
}
get x() {
return this.entity.position[0];
}
set x(x) {
this.onPositionChanged([x, this.entity.position[1]]);
}
get y() {
return this.entity.position[1];
}
set y(y) {
this.onPositionChanged([this.entity.position[0], y]);
}
};