break_eternity
This commit is contained in:
Generated
+7
@@ -8,6 +8,7 @@
|
||||
"name": "resource-graph-sim",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"break_eternity.js": "^2.1.3",
|
||||
"elkjs": "^0.12.0",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1"
|
||||
@@ -1231,6 +1232,12 @@
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/break_eternity.js": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/break_eternity.js/-/break_eternity.js-2.1.3.tgz",
|
||||
"integrity": "sha512-4tg4j0wc0lhaYAnOHubN5mAyHbhMfUI7adQLO8l/loKqtylZ/kHWp8WYqG2EC0TinSesKvpCi3XeVFcKRUBJsQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/browserslist": {
|
||||
"version": "4.28.8",
|
||||
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.28.8.tgz",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"break_eternity.js": "^2.1.3",
|
||||
"elkjs": "^0.12.0",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1"
|
||||
|
||||
@@ -63,7 +63,7 @@ const NodeCard = forwardRef<HTMLDivElement, Props>(function NodeCard({ node2d, s
|
||||
<div>{name}</div>
|
||||
{
|
||||
Object.entries(storage)
|
||||
.map(([rt, v]) => `${ResourceDescriptions[rt as Resource].icon}${v}`).join(' ')
|
||||
.map(([rt, v]) => `${ResourceDescriptions[rt as Resource].icon}${v.toStringWithDecimalPlaces(4)}`).join(' ')
|
||||
}
|
||||
</div>
|
||||
))
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type Decimal from 'break_eternity.js';
|
||||
|
||||
interface Props {
|
||||
time: number;
|
||||
time: Decimal;
|
||||
onTick: () => void;
|
||||
onReset: () => void;
|
||||
onInitialize: () => void;
|
||||
@@ -13,7 +15,7 @@ export default function Topbar({ time, onTick, onReset, onInitialize }: Props) {
|
||||
<span>детерминированная тиковая симуляция</span>
|
||||
</h1>
|
||||
<div className="tick-counter">
|
||||
Время <b>{time}</b>
|
||||
Время <b>{time.toStringWithDecimalPlaces(4)}</b>
|
||||
</div>
|
||||
<div className="spacer" />
|
||||
<button className="btn" onClick={onInitialize}>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Decimal, { DecimalSource } from "break_eternity.js";
|
||||
import { Resource, Resources, SerializedConverter, SerializedNodeBehaviour as SerializedBehaviour, SerializedNodeBehaviour } from "../../types";
|
||||
|
||||
export type BehaviourResourcePin = {
|
||||
@@ -67,15 +68,15 @@ export abstract class Behaviour {
|
||||
abstract getDisplayInfo(): BehaviourDisplayInfo;
|
||||
|
||||
/** Withdraws up to `amount` of `resource` from this behaviour's storage, returning how much was actually taken. */
|
||||
public take(resource: Resource, amount: number): number {
|
||||
const available = this.storage[resource] ?? 0;
|
||||
const taken = Math.min(available, amount);
|
||||
this.storage[resource] = available - taken;
|
||||
public take(resource: Resource, amount: DecimalSource): Decimal {
|
||||
const available = this.storage[resource] ?? Decimal.dZero;
|
||||
const taken = available.min(amount);
|
||||
this.storage[resource] = available.sub(taken);
|
||||
return taken;
|
||||
}
|
||||
|
||||
protected give(resource: Resource, amount: number): void {
|
||||
this.storage[resource] = (this.storage[resource] ?? 0) + amount;
|
||||
protected give(resource: Resource, amount: DecimalSource): void {
|
||||
this.storage[resource] = (this.storage[resource] ?? Decimal.dZero).add(amount);
|
||||
}
|
||||
|
||||
abstract produce(deltaTime: number): void;
|
||||
@@ -94,17 +95,17 @@ export class Converter extends Behaviour {
|
||||
|
||||
this.inputRates = {};
|
||||
for (const pin of source.inputs.pins)
|
||||
this.inputRates[pin.resource] = pin.rate;
|
||||
this.inputRates[pin.resource] = new Decimal(pin.rate);
|
||||
|
||||
this.outputRates = {};
|
||||
for (const pin of source.outputs.pins)
|
||||
this.outputRates[pin.resource] = pin.rate;
|
||||
this.outputRates[pin.resource] = new Decimal(pin.rate);
|
||||
}
|
||||
|
||||
/** 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);
|
||||
this.give(output.resource, (this.outputRates[output.resource] ?? Decimal.dZero).mul(deltaTime));
|
||||
}
|
||||
|
||||
/** Pulls resources into this converter's storage from whatever each input pin is linked to. */
|
||||
@@ -113,8 +114,8 @@ export class Converter extends Behaviour {
|
||||
const source = resolve(input.link.target);
|
||||
if (!source)
|
||||
continue;
|
||||
const rate = this.inputRates[input.resource] ?? 0;
|
||||
const taken = source.take(input.link.resource, rate * deltaTime);
|
||||
const rate = this.inputRates[input.resource] ?? Decimal.dZero;
|
||||
const taken = source.take(input.link.resource, rate.mul(deltaTime));
|
||||
this.give(input.resource, taken);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-8
@@ -1,9 +1,10 @@
|
||||
import Decimal, { DecimalSource } from "break_eternity.js";
|
||||
import { Behaviour } from "../engine/functions";
|
||||
import { deserializeBehaviour } from "../engine/saveLoad";
|
||||
|
||||
export type Resource = 'wood' | 'plank' | 'stone';
|
||||
|
||||
export type Resources = Record<Resource, number>;
|
||||
export type Resources = Record<Resource, Decimal>;
|
||||
|
||||
export type Node = {
|
||||
id: string;
|
||||
@@ -12,11 +13,11 @@ export type Node = {
|
||||
}
|
||||
|
||||
export class Game {
|
||||
public time: number;
|
||||
public time: Decimal;
|
||||
public readonly nodes: Node[];
|
||||
|
||||
constructor(source: SerializedGame) {
|
||||
this.time = source.time;
|
||||
this.time = new Decimal(source.time);
|
||||
this.nodes = source.nodes.map((node) => ({
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
@@ -25,7 +26,7 @@ export class Game {
|
||||
}
|
||||
|
||||
public tick(deltaTime: number) {
|
||||
this.time += deltaTime;
|
||||
this.time = this.time.add(deltaTime);
|
||||
|
||||
const behavioursById = new Map(this.nodes.map((node) => [node.behaviour.id, node.behaviour]));
|
||||
|
||||
@@ -36,7 +37,7 @@ export class Game {
|
||||
}
|
||||
|
||||
public reset() {
|
||||
this.time = 0;
|
||||
this.time = Decimal.dZero;
|
||||
for (const node of this.nodes)
|
||||
node.behaviour.reset();
|
||||
}
|
||||
@@ -49,7 +50,7 @@ export class Game {
|
||||
|
||||
export type SerializedConverterInput = {
|
||||
resource: Resource;
|
||||
rate: number;
|
||||
rate: DecimalSource;
|
||||
link: {
|
||||
target: SerializedNodeBehaviour['id'],
|
||||
resource: Resource,
|
||||
@@ -58,7 +59,7 @@ export type SerializedConverterInput = {
|
||||
|
||||
export type SerializedConverterOutput = {
|
||||
resource: Resource;
|
||||
rate: number;
|
||||
rate: DecimalSource;
|
||||
}
|
||||
|
||||
export type SerializedConverterInputs = {
|
||||
@@ -90,6 +91,6 @@ export type SerializedNode = {
|
||||
}
|
||||
|
||||
export type SerializedGame = {
|
||||
time: number;
|
||||
time: DecimalSource;
|
||||
nodes: SerializedNode[];
|
||||
}
|
||||
Reference in New Issue
Block a user