minor converter rates refactoring

This commit is contained in:
azykov@mail.ru
2026-08-31 22:10:16 +03:00
parent d96a6e2e92
commit 90501176ef
2 changed files with 44 additions and 24 deletions
+31 -19
View File
@@ -16,7 +16,6 @@ export type BehaviourLink = {
}
export type BehaviourInput = BehaviourResourcePin & {
rate: number; // per second of tick deltaTime
link: BehaviourLink;
}
@@ -25,7 +24,6 @@ export type BehaviourInputs = {
}
export type BehaviourOutput = BehaviourResourcePin & {
rate: number; // per second of tick deltaTime
}
export type BehaviourOutputs = {
@@ -51,13 +49,11 @@ export abstract class Behaviour {
this.outputs = {
pins: source.outputs.pins.map((output) => ({
resource: output.resource,
rate: output.rate,
})),
}
this.inputs = {
pins: source.inputs.pins.map((input) => ({
resource: input.resource,
rate: input.rate,
link: input.link,
})),
}
@@ -71,7 +67,7 @@ export abstract class Behaviour {
abstract getDisplayInfo(): BehaviourDisplayInfo;
/** Withdraws up to `amount` of `resource` from this behaviour's storage, returning how much was actually taken. */
protected take(resource: Resource, amount: number): number {
public take(resource: Resource, amount: number): number {
const available = this.storage[resource] ?? 0;
const taken = Math.min(available, amount);
this.storage[resource] = available - taken;
@@ -82,30 +78,46 @@ export abstract class Behaviour {
this.storage[resource] = (this.storage[resource] ?? 0) + amount;
}
/** Fills this behaviour's own storage from its outputs' production rates. */
public produce(deltaTime: number): void {
for (const output of this.outputs.pins)
this.give(output.resource, output.rate * deltaTime);
abstract produce(deltaTime: number): void;
abstract pull(deltaTime: number, resolve: (id: string) => Behaviour | undefined): void;
}
export class Converter extends Behaviour {
public readonly inputRates: Partial<Resources>;
public readonly outputRates: Partial<Resources>;
constructor(source: SerializedNodeBehaviour & SerializedConverter) {
if (source.kind !== 'converter')
throw new Error('Invalid source');
super(source);
this.inputRates = {};
for (const pin of source.inputs.pins)
this.inputRates[pin.resource] = pin.rate;
this.outputRates = {};
for (const pin of source.outputs.pins)
this.outputRates[pin.resource] = pin.rate;
}
/** Pulls resources into this behaviour's storage from whatever each input pin is linked to. */
/** Fills this converter's own storage from its outputs' production rates. */
public produce(deltaTime: number): void {
for (const output of this.outputs.pins)
this.give(output.resource, (this.outputRates[output.resource] ?? 0) * deltaTime);
}
/** Pulls resources into this converter's storage from whatever each input pin is linked to. */
public pull(deltaTime: number, resolve: (id: string) => Behaviour | undefined): void {
for (const input of this.inputs.pins) {
const source = resolve(input.link.target);
if (!source)
continue;
const taken = source.take(input.link.resource, input.rate * deltaTime);
const rate = this.inputRates[input.resource] ?? 0;
const taken = source.take(input.link.resource, rate * deltaTime);
this.give(input.resource, taken);
}
}
}
export class Converter extends Behaviour {
constructor(source: SerializedNodeBehaviour & SerializedConverter) {
if (source.kind !== 'converter')
throw new Error('Invalid source');
super(source);
}
public getDisplayInfo(): BehaviourDisplayInfo {
return {