Skip to content

Commit

Permalink
Associated slots with their resource and forbidden to connect differe…
Browse files Browse the repository at this point in the history
…nt resources
  • Loading branch information
andev0 committed Aug 19, 2024
1 parent 6a6aa1a commit 3faaf17
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/MouseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
10 changes: 8 additions & 2 deletions src/Sankey/SankeyNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/Sankey/Slots/InputSankeySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Sankey/Slots/OutputSankeySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 12 additions & 7 deletions src/Sankey/Slots/SankeySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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;
Expand All @@ -64,7 +69,7 @@ export abstract class SankeySlot extends EventTarget
return this._parentGroup;
}

private _resourcesAmount: number;
private readonly _resource: RecipeResource;

private readonly _slotSvgRect: SVGRectElement;

Expand Down
4 changes: 2 additions & 2 deletions src/Sankey/Slots/SankeySlotExceeding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Sankey/Slots/SankeySlotMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down
34 changes: 23 additions & 11 deletions src/Sankey/SlotsGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ 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)
: new Point(SankeyNode.nodeWidth + SankeySlot.slotWidth, startY);

this.groupSvg = SvgFactory.createSvgGroup(position, `${type}-slots`);

this.lastSlot = this.createLastSlot();
this.lastSlot = this.initializeLastSlot();

node.nodeSvgGroup.appendChild(this.groupSvg);

Expand All @@ -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
{
Expand All @@ -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)
Expand All @@ -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
{
Expand Down

0 comments on commit 3faaf17

Please sign in to comment.