initial commit
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { Resource, Resources, SerializedConverter, SerializedNodeBehaviour as SerializedBehaviour, SerializedNodeBehaviour, SerializedConverterOutput, SerializedConverterInput } from "../../types";
|
||||
|
||||
export type BehaviourResourcePin = {
|
||||
resource: Resource;
|
||||
}
|
||||
|
||||
export type BehaviourLink = {
|
||||
target: Behaviour['id'],
|
||||
resource: Resource;
|
||||
}
|
||||
|
||||
export type BehaviourInput = BehaviourResourcePin & {
|
||||
link: BehaviourLink;
|
||||
}
|
||||
|
||||
export type BehaviourInputs = {
|
||||
pins: BehaviourInput[];
|
||||
}
|
||||
|
||||
export type BehaviourOutput = BehaviourResourcePin & {
|
||||
}
|
||||
|
||||
export type BehaviourOutputs = {
|
||||
pins: BehaviourOutput[];
|
||||
}
|
||||
|
||||
export type BehaviourDisplayInfo = {
|
||||
storages: Record<string, Resources>;
|
||||
links: BehaviourLink[];
|
||||
}
|
||||
|
||||
export abstract class Behaviour {
|
||||
public readonly id: string;
|
||||
public readonly kind: string;
|
||||
public readonly inputs: BehaviourInputs;
|
||||
public readonly outputs: BehaviourOutputs;
|
||||
|
||||
constructor(source: SerializedBehaviour) {
|
||||
this.id = source.id;
|
||||
this.kind = source.kind;
|
||||
this.outputs = {
|
||||
pins: source.outputs.pins.map((input) => ({
|
||||
resource: input.resource,
|
||||
})),
|
||||
}
|
||||
this.inputs = {
|
||||
pins: source.inputs.pins.map((input) => ({
|
||||
resource: input.resource,
|
||||
link: input.link,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
abstract getDisplayInfo(): BehaviourDisplayInfo;
|
||||
|
||||
abstract tick(deltaTime: number): void;
|
||||
}
|
||||
|
||||
export type ResourceUsageRule = {
|
||||
resource: Resource;
|
||||
speed: number; // per tick
|
||||
}
|
||||
|
||||
export class Converter extends Behaviour {
|
||||
public outputRules: ResourceUsageRule[] = [];
|
||||
rules: { inputs: SerializedConverterInput[]; outputs: SerializedConverterOutput[]; };
|
||||
|
||||
constructor(source: SerializedNodeBehaviour & SerializedConverter) {
|
||||
if (source.kind !== 'converter')
|
||||
throw new Error('Invalid source');
|
||||
super(source);
|
||||
|
||||
this.rules = {
|
||||
inputs: source.inputs.pins,
|
||||
outputs: source.outputs.pins,
|
||||
};
|
||||
}
|
||||
|
||||
public getDisplayInfo(): BehaviourDisplayInfo {
|
||||
return {
|
||||
storages: {},
|
||||
links: this.inputs.pins.map((input) => input.link),
|
||||
}
|
||||
}
|
||||
|
||||
tick(deltaTime: number): void {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// export class Storage extends Behaviour {
|
||||
// public static type: string = 'storage';
|
||||
// }
|
||||
|
||||
export const behaviours = {
|
||||
'converter': Converter,
|
||||
// 'storage': ,
|
||||
} satisfies Record<string, { new(source: never): Behaviour }>;
|
||||
Reference in New Issue
Block a user