From 3faaf173a34c4544d13a231ab8540dbc8615b7af Mon Sep 17 00:00:00 2001 From: andev0 Date: Mon, 19 Aug 2024 11:58:30 +0200 Subject: [PATCH] Associated slots with their resource and forbidden to connect different resources --- src/MouseHandler.ts | 10 ++++++++ src/Sankey/SankeyNode.ts | 10 ++++++-- src/Sankey/Slots/InputSankeySlot.ts | 4 +-- src/Sankey/Slots/OutputSankeySlot.ts | 4 +-- src/Sankey/Slots/SankeySlot.ts | 19 +++++++++----- src/Sankey/Slots/SankeySlotExceeding.ts | 4 +-- src/Sankey/Slots/SankeySlotMissing.ts | 4 +-- src/Sankey/SlotsGroup.ts | 34 +++++++++++++++++-------- 8 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/MouseHandler.ts b/src/MouseHandler.ts index 99fe0f2..66ef11e 100644 --- a/src/MouseHandler.ts +++ b/src/MouseHandler.ts @@ -131,6 +131,11 @@ export class MouseHandler throw Error("First connecting slot wasn't saved."); } + if (this.firstConnectingSlot.resourceId != targetSlot.resourceId) + { + return; + } + let resourcesAmount = Math.min(targetSlot.resourcesAmount, this.firstConnectingSlot.resourcesAmount); @@ -162,6 +167,11 @@ export class MouseHandler throw Error("First connecting slot wasn't saved."); } + if (this.firstConnectingSlot.resourceId != targetSlot.resourceId) + { + return; + } + let resourcesAmount = Math.min(targetSlot.resourcesAmount, this.firstConnectingSlot.resourcesAmount); diff --git a/src/Sankey/SankeyNode.ts b/src/Sankey/SankeyNode.ts index cf719a8..7aa0078 100644 --- a/src/Sankey/SankeyNode.ts +++ b/src/Sankey/SankeyNode.ts @@ -52,7 +52,10 @@ export class SankeyNode let newGroup = new SlotsGroup( this, "input", - toItemsInMinute(ingredient.amount, recipe.manufacturingDuration), + { + id: ingredient.id, + amount: toItemsInMinute(ingredient.amount, recipe.manufacturingDuration) + }, totalInputResourcesAmount, nextInputGroupY ); @@ -69,7 +72,10 @@ export class SankeyNode let newGroup = new SlotsGroup( this, "output", - toItemsInMinute(product.amount, recipe.manufacturingDuration), + { + id: product.id, + amount: toItemsInMinute(product.amount, recipe.manufacturingDuration) + }, totalOutputResourcesAmount, nextOutputGroupY); diff --git a/src/Sankey/Slots/InputSankeySlot.ts b/src/Sankey/Slots/InputSankeySlot.ts index 3ba6bb9..09fd600 100644 --- a/src/Sankey/Slots/InputSankeySlot.ts +++ b/src/Sankey/Slots/InputSankeySlot.ts @@ -6,9 +6,9 @@ export class InputSankeySlot extends SankeySlot constructor( slotsGroup: SlotsGroup, slotsGroupSvg: SVGGElement, - resourcesAmount: number, + resource: RecipeResource, ...classes: string[]) { - super(slotsGroup, slotsGroupSvg, resourcesAmount, "input-slot", ...classes); + super(slotsGroup, slotsGroupSvg, resource, "input-slot", ...classes); } } diff --git a/src/Sankey/Slots/OutputSankeySlot.ts b/src/Sankey/Slots/OutputSankeySlot.ts index b6414f6..15c8ff7 100644 --- a/src/Sankey/Slots/OutputSankeySlot.ts +++ b/src/Sankey/Slots/OutputSankeySlot.ts @@ -6,9 +6,9 @@ export class OutputSankeySlot extends SankeySlot constructor( slotsGroup: SlotsGroup, slotsGroupSvg: SVGGElement, - resourcesAmount: number, + resource: RecipeResource, ...classes: string[]) { - super(slotsGroup, slotsGroupSvg, resourcesAmount, "output-slot", ...classes); + super(slotsGroup, slotsGroupSvg, resource, "output-slot", ...classes); } } diff --git a/src/Sankey/Slots/SankeySlot.ts b/src/Sankey/Slots/SankeySlot.ts index f505837..f416f14 100644 --- a/src/Sankey/Slots/SankeySlot.ts +++ b/src/Sankey/Slots/SankeySlot.ts @@ -11,17 +11,17 @@ export abstract class SankeySlot extends EventTarget constructor( slotsGroup: SlotsGroup, slotsGroupSvg: SVGGElement, - resourcesAmount: number, + resource: RecipeResource, ...classes: string[]) { super(); - this._resourcesAmount = resourcesAmount; + this._resource = resource; this._parentGroup = slotsGroup; let dimensions: Rectangle = { width: SankeySlot.slotWidth, - height: slotsGroup.maxHeight * (resourcesAmount / slotsGroup.resourcesAmount), + height: slotsGroup.maxHeight * (this._resource.amount / slotsGroup.resource.amount), x: 0, y: 0 }; @@ -39,21 +39,26 @@ export abstract class SankeySlot extends EventTarget public get resourcesAmount() { - return this._resourcesAmount; + return this._resource.amount; } public set resourcesAmount(resourcesAmount: number) { - this._resourcesAmount = resourcesAmount; + this._resource.amount = resourcesAmount; this.slotSvgRect.setAttribute( "height", - `${this._parentGroup.maxHeight * (resourcesAmount / this._parentGroup.resourcesAmount)}` + `${this._parentGroup.maxHeight * (resourcesAmount / this._parentGroup.resource.amount)}` ); this.dispatchEvent(new Event(SankeySlot.boundsChangedEvent)); } + public get resourceId(): string + { + return this._resource.id; + } + public get slotSvgRect(): SVGRectElement { return this._slotSvgRect; @@ -64,7 +69,7 @@ export abstract class SankeySlot extends EventTarget return this._parentGroup; } - private _resourcesAmount: number; + private readonly _resource: RecipeResource; private readonly _slotSvgRect: SVGRectElement; diff --git a/src/Sankey/Slots/SankeySlotExceeding.ts b/src/Sankey/Slots/SankeySlotExceeding.ts index 1f7b935..b18dfb7 100644 --- a/src/Sankey/Slots/SankeySlotExceeding.ts +++ b/src/Sankey/Slots/SankeySlotExceeding.ts @@ -8,9 +8,9 @@ export class SankeySlotExceeding extends OutputSankeySlot constructor( slotsGroup: SlotsGroup, slotsGroupSvg: SVGGElement, - resourcesAmount: number) + resource: RecipeResource) { - super(slotsGroup, slotsGroupSvg, resourcesAmount, "exceeding"); + super(slotsGroup, slotsGroupSvg, resource, "exceeding"); this.slotSvgRect.addEventListener("click", (event) => { diff --git a/src/Sankey/Slots/SankeySlotMissing.ts b/src/Sankey/Slots/SankeySlotMissing.ts index fa65389..53599ce 100644 --- a/src/Sankey/Slots/SankeySlotMissing.ts +++ b/src/Sankey/Slots/SankeySlotMissing.ts @@ -8,9 +8,9 @@ export class SankeySlotMissing extends InputSankeySlot constructor( slotsGroup: SlotsGroup, slotsGroupSvg: SVGGElement, - resourcesAmount: number) + resource: RecipeResource) { - super(slotsGroup, slotsGroupSvg, resourcesAmount, "missing"); + super(slotsGroup, slotsGroupSvg, resource, "missing"); this.slotSvgRect.addEventListener("click", (event) => { diff --git a/src/Sankey/SlotsGroup.ts b/src/Sankey/SlotsGroup.ts index f0c38c4..0b21f04 100644 --- a/src/Sankey/SlotsGroup.ts +++ b/src/Sankey/SlotsGroup.ts @@ -16,22 +16,22 @@ export class SlotsGroup extends EventTarget public type: SlotsGroupType; public maxHeight: number; - public resourcesAmount: number; + public readonly resource: RecipeResource; constructor( node: SankeyNode, type: SlotsGroupType, - resourcesAmount: number, + resource: RecipeResource, nodeResourcesAmount: number, startY: number) { super(); this.type = type; - this.resourcesAmount = resourcesAmount; + this.resource = resource; let nodeHeight = +(node.nodeSvg.getAttribute("height") ?? 0); - this.maxHeight = nodeHeight * (resourcesAmount / nodeResourcesAmount); + this.maxHeight = nodeHeight * (resource.amount / nodeResourcesAmount); let position = type === "input" ? new Point(0, startY) @@ -39,7 +39,7 @@ export class SlotsGroup extends EventTarget this.groupSvg = SvgFactory.createSvgGroup(position, `${type}-slots`); - this.lastSlot = this.createLastSlot(); + this.lastSlot = this.initializeLastSlot(); node.nodeSvgGroup.appendChild(this.groupSvg); @@ -62,11 +62,17 @@ export class SlotsGroup extends EventTarget if (this.type === "input") { - newSlot = new InputSankeySlot(this, this.groupSvg, resourcesAmount); + newSlot = new InputSankeySlot(this, this.groupSvg, { + id: this.resource.id, + amount: resourcesAmount, + }); } else if (this.type === "output") { - newSlot = new OutputSankeySlot(this, this.groupSvg, resourcesAmount); + newSlot = new OutputSankeySlot(this, this.groupSvg, { + id: this.resource.id, + amount: resourcesAmount, + }); } else { @@ -81,7 +87,7 @@ export class SlotsGroup extends EventTarget private updateSlotPositions(): void { - let freeResourcesAmount = this.resourcesAmount; + let freeResourcesAmount = this.resource.amount; let nextYPosition = 0; for (const slot of this.slots) @@ -96,15 +102,21 @@ export class SlotsGroup extends EventTarget this.lastSlot.resourcesAmount = freeResourcesAmount; } - private createLastSlot(): SankeySlotMissing | SankeySlotExceeding + private initializeLastSlot(): SankeySlotMissing | SankeySlotExceeding { if (this.type === "input") { - return new SankeySlotMissing(this, this.groupSvg, this.resourcesAmount); + return new SankeySlotMissing(this, this.groupSvg, { + id: this.resource.id, + amount: this.resource.amount + }); } else if (this.type === "output") { - return new SankeySlotExceeding(this, this.groupSvg, this.resourcesAmount); + return new SankeySlotExceeding(this, this.groupSvg, { + id: this.resource.id, + amount: this.resource.amount + }); } else {