From 114e361123e9410e926e820acd4d48a79e4bb30c Mon Sep 17 00:00:00 2001 From: cha0s Date: Sat, 6 Feb 2021 01:37:32 -0600 Subject: [PATCH] refactor: simpler --- packages/farm/src/traits/tool.js | 91 +++++++++----------------------- 1 file changed, 26 insertions(+), 65 deletions(-) diff --git a/packages/farm/src/traits/tool.js b/packages/farm/src/traits/tool.js index 026562f..c91e9dd 100644 --- a/packages/farm/src/traits/tool.js +++ b/packages/farm/src/traits/tool.js @@ -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.