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