refactor: simpler

This commit is contained in:
cha0s 2021-02-06 01:37:32 -06:00
parent e05a84b643
commit 114e361123

View File

@ -91,13 +91,10 @@ export default (latus) => class Tool extends Trait {
calculateTargets() {
this.targets = [];
const {wielder} = this.entity;
if (!wielder || !wielder.is('Directional') || !wielder.is('Layered')) {
return;
}
const {direction, layer} = wielder;
if (!layer) {
if (!wielder || !wielder.is('Directional')) {
return;
}
const {direction} = wielder;
const start = this.calculateTargetStart();
const width = (this.params.target.width - 1) / 2;
for (let i = -width; i < (width + 1); ++i) {
@ -136,34 +133,18 @@ export default (latus) => class Tool extends Trait {
onWielderActiveSlotIndexChanged() {
this.calculateTargets();
if ('client' === process.env.SIDE) {
this.guidePrimitives.visible = false;
const {wielder} = this.entity;
if (!wielder) {
if (!wielder || !wielder.container || !wielder.is('Receptacle')) {
return;
}
if (!wielder.container) {
return;
}
if (this.entity === wielder.itemInActiveSlot()) {
wielder.container.addChild(this.guidePrimitives);
}
else {
wielder.container.removeChild(this.guidePrimitives);
}
this.guidePrimitives.visible = this.entity === wielder.itemInActiveSlot();
}
}
onWielderDirectionChanged() {
refreshGuide() {
this.calculateTargets();
this.renderPrimitives();
}
onWielderAddedToLayer() {
this.calculateTargets();
this.renderPrimitives();
}
onWielderPositionChanged() {
this.calculateTargets();
this.repositionPrimitives();
}
@ -209,66 +190,48 @@ export default (latus) => class Tool extends Trait {
}
}
// eslint-disable-next-line class-methods-use-this
hooks() {
return {
contextTypeHints: () => [
['item', 'entity'],
['target', 'vector'],
['wielder', 'entity'],
],
};
}
listeners() {
return {
wielderChanged: (oldWielder, newWielder) => {
if (oldWielder && oldWielder.is('Visible')) {
if ('client' === process.env.SIDE) {
oldWielder.container.removeChild(this.guidePrimitives);
}
oldWielder.off(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
);
oldWielder.off(
'directionChanged',
this.onWielderDirectionChanged,
);
oldWielder.off(
'addedToLayer',
this.onWielderAddedToLayer,
);
oldWielder.off(
'positionChanged',
this.onWielderPositionChanged,
[
'addedToLayer',
'directionChanged',
'tileOffsetChanged',
],
this.refreshGuide,
);
}
if (newWielder && newWielder.is('Visible')) {
if ('client' === process.env.SIDE) {
newWielder.container.addChild(this.guidePrimitives);
}
newWielder.on(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
this,
);
newWielder.on(
'directionChanged',
this.onWielderDirectionChanged,
this,
);
newWielder.on(
'addedToLayer',
this.onWielderAddedToLayer,
this,
);
newWielder.on(
'positionChanged',
this.onWielderPositionChanged,
[
'addedToLayer',
'directionChanged',
'tileOffsetChanged',
],
this.refreshGuide,
this,
);
}
this.calculateTargets();
this.renderPrimitives();
this.repositionPrimitives();
this.refreshGuide();
this.onWielderActiveSlotIndexChanged();
},
};
@ -278,8 +241,6 @@ export default (latus) => class Tool extends Trait {
return {
useTool: () => {
// Recalculate targets. TODO: Should already be done. This is a hammer.
this.calculateTargets();
// Each target gets a promise.
const promises = this.targets.map((target) => {
// Set up.