classes refactoring, converter input pull amount fix
This commit is contained in:
+14
-13
@@ -1,19 +1,20 @@
|
||||
import ELK, { ElkNode } from "elkjs/lib/elk.bundled.js";
|
||||
import { Node } from "../types";
|
||||
import { Unit } from "../types";
|
||||
import { Behaviour } from "../engine/behaviours/base";
|
||||
|
||||
const elk = new ELK();
|
||||
|
||||
const NODE_WIDTH = 100;
|
||||
const UNIT_WIDTH = 100;
|
||||
const NODE_HEIGHT = 90;
|
||||
|
||||
export type Node2D = {
|
||||
id: Node['id'];
|
||||
export type GraphNode = {
|
||||
position: { x: number, y: number };
|
||||
node: Node;
|
||||
kind: 'behaviour';
|
||||
entity: Behaviour;
|
||||
}
|
||||
|
||||
export async function build2dNodes(nodes: Node[], area: { width: number, height: number, padding: number }): Promise<Node2D[]> {
|
||||
const nodesById = new Map(nodes.map((node) => [node.behaviour.id, node]));
|
||||
export async function build2dNodes(units: Unit[], area: { width: number, height: number, padding: number }): Promise<GraphNode[]> {
|
||||
const entitiesById = new Map(units.map((unit) => [unit.behaviour.id, unit.behaviour]));
|
||||
|
||||
const graph: ElkNode = {
|
||||
id: "root",
|
||||
@@ -23,12 +24,12 @@ export async function build2dNodes(nodes: Node[], area: { width: number, height:
|
||||
"elk.spacing.nodeNode": "60",
|
||||
"elk.layered.spacing.nodeNodeBetweenLayers": "100",
|
||||
},
|
||||
children: nodes.map((node) => ({
|
||||
id: node.behaviour.id,
|
||||
width: NODE_WIDTH,
|
||||
children: units.map((unit) => ({
|
||||
id: unit.behaviour.id,
|
||||
width: UNIT_WIDTH,
|
||||
height: NODE_HEIGHT,
|
||||
})),
|
||||
edges: nodes.flatMap((node) =>
|
||||
edges: units.flatMap((node) =>
|
||||
node.behaviour.getDisplayInfo().links.map((link, idx) => ({
|
||||
id: `${node.behaviour.id}->${link.target}.${idx}`,
|
||||
sources: [node.behaviour.id],
|
||||
@@ -40,11 +41,11 @@ export async function build2dNodes(nodes: Node[], area: { width: number, height:
|
||||
const layout = await elk.layout(graph);
|
||||
|
||||
return (layout.children ?? []).map((child) => ({
|
||||
id: child.id,
|
||||
position: {
|
||||
x: (child.x ?? 0) + area.padding,
|
||||
y: (child.y ?? 0) + area.padding,
|
||||
},
|
||||
node: nodesById.get(child.id)!,
|
||||
kind: 'behaviour',
|
||||
entity: entitiesById.get(child.id)!,
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user