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