@woosh/meep-engine 2.46.7 → 2.46.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "productName": "Meep",
5
5
  "description": "production-ready JavaScript game engine based on Entity Component System Architecture",
6
6
  "author": "Alexander Goldring",
7
- "version": "2.46.7",
7
+ "version": "2.46.10",
8
8
  "main": "build/meep.module.js",
9
9
  "module": "build/meep.module.js",
10
10
  "scripts": {
@@ -106,6 +106,26 @@ export class NodeGraph {
106
106
  return this.connections.data.slice();
107
107
  }
108
108
 
109
+ /**
110
+ *
111
+ * @param {NodeInstance} node
112
+ * @returns {boolean}
113
+ */
114
+ hasNode(node) {
115
+ const existing_node = this.getNode(node.id);
116
+
117
+ if (existing_node === undefined) {
118
+ return false;
119
+ }
120
+
121
+ if (existing_node !== node) {
122
+ // another node with this ID was found
123
+ return false;
124
+ }
125
+
126
+ return true;
127
+ }
128
+
109
129
  /**
110
130
  *
111
131
  * @param {number} id
@@ -29,6 +29,12 @@ function pickNewSetId(things) {
29
29
  return r;
30
30
  }
31
31
 
32
+ /**
33
+ *
34
+ * @type {number}
35
+ */
36
+ let node_id_counter = 0;
37
+
32
38
  export class NodeDescription {
33
39
  constructor() {
34
40
  /**
@@ -38,10 +44,13 @@ export class NodeDescription {
38
44
  this.name = "";
39
45
 
40
46
  /**
41
- *
47
+ * A unique identifier in the scope of a single registry
48
+ * If two nodes belong to the same registry - they must have different IDs
49
+ * By default an auto-incrementing ID is assigned, as this ID is assigned at the time of object construction - it may be subject to race conditions
50
+ * In case nodes are loaded or constructed in async manner - consider using your own ID assignment scheme
42
51
  * @type {number}
43
52
  */
44
- this.id = 0;
53
+ this.id = node_id_counter++;
45
54
 
46
55
  /**
47
56
  * @protected
@@ -119,6 +119,10 @@ export class NodeInstancePortReference {
119
119
  && this.port.equals(other.port)
120
120
  ;
121
121
  }
122
+
123
+ toString() {
124
+ return `NodeInstancePortReference{ id=${this.id}, instance=${this.instance}, port=${this.port}, connections.length=${this.connections.length} }`;
125
+ }
122
126
  }
123
127
 
124
128
  /**
@@ -38,12 +38,21 @@ export class NodeRegistry {
38
38
  /**
39
39
  *
40
40
  * @param {NodeDescription} node
41
+ * @returns {boolean} true if node was added, false if node already exists
41
42
  */
42
43
  addNode(node) {
43
44
  assert.defined(node, "node");
44
45
  assert.notNull(node, "node");
45
46
 
46
- if (this.nodes.has(node.id)) {
47
+ const existing_node = this.nodes.get(node.id);
48
+
49
+ if (existing_node !== undefined) {
50
+
51
+ if (existing_node !== node) {
52
+ // node with the same ID exists and is not the same node
53
+ throw new Error(`A different node with the same ID ${node.id} is already registered. IDs of all nodes in the registry must be unique`);
54
+ }
55
+
47
56
  // console.warn(`Node with id ${node.id} already exists`);
48
57
 
49
58
  return false;
@@ -1,41 +1,6 @@
1
1
  import { NodeGraph } from "../NodeGraph.js";
2
2
  import { assert } from "../../../assert.js";
3
-
4
- /**
5
- *
6
- * @param {NodeGraph} graph
7
- * @param {NodeInstance[]} nodes
8
- * @returns {Connection[]}
9
- */
10
- function collectConnectionsAmongst({
11
- graph,
12
- nodes
13
- }) {
14
-
15
- const r = new Set();
16
-
17
- const node_count = nodes.length;
18
- for (let i = 0; i < node_count; i++) {
19
- const node_0 = nodes[i];
20
-
21
- const connections = node_0.connections.asArray();
22
- const connection_count = connections.length;
23
-
24
- for (let j = 0; j < connection_count; j++) {
25
- const connection = connections[j];
26
-
27
- for (let k = i + 1; k < node_count; k++) {
28
- const node_1 = nodes[k];
29
-
30
- if (connection.isAttachedToNode(node_1.id)) {
31
- r.add(connection);
32
- }
33
- }
34
- }
35
- }
36
-
37
- return Array.from(r);
38
- }
3
+ import { graph_collect_connections_amongst_nodes } from "./graph_collect_connections_amongst_nodes.js";
39
4
 
40
5
  /**
41
6
  * Clone portion of a graph as a new graph, retaining connections amongst those nodes
@@ -53,15 +18,24 @@ export function graph_clone_by_node_subset({ graph, nodes }) {
53
18
 
54
19
  const result = new NodeGraph();
55
20
 
21
+ /**
22
+ * Maps from source node IDs to result node IDs
23
+ * @type {Object<number,number>}
24
+ */
56
25
  const cloned_nodes = {};
57
26
 
58
27
  nodes.forEach(n => {
28
+
29
+ if (!graph.hasNode(n)) {
30
+ throw new Error(`Referenced node ${n} is not a found in the graph`);
31
+ }
32
+
59
33
  const id = result.createNode(n.description);
60
34
 
61
35
  cloned_nodes[n.id] = id;
62
36
  });
63
37
 
64
- collectConnectionsAmongst({ graph, nodes })
38
+ graph_collect_connections_amongst_nodes({ graph, nodes })
65
39
  .forEach(connection => {
66
40
 
67
41
  const source_node_id = cloned_nodes[connection.source.instance.id];
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Given a graph and a set of nodes, get all connections that connect these nodes amongst themselves
3
+ * @param {NodeGraph} graph
4
+ * @param {NodeInstance[]} nodes
5
+ * @returns {Connection[]}
6
+ */
7
+ export function graph_collect_connections_amongst_nodes({
8
+ graph,
9
+ nodes
10
+ }) {
11
+
12
+ const r = new Set();
13
+
14
+ const node_count = nodes.length;
15
+
16
+ for (let i = 0; i < node_count; i++) {
17
+ const node_0 = nodes[i];
18
+
19
+ const connections = node_0.connections.asArray();
20
+ const connection_count = connections.length;
21
+
22
+ for (let j = 0; j < connection_count; j++) {
23
+ const connection = connections[j];
24
+
25
+ if (connection.source.instance === connection.target.instance) {
26
+ // Special case. Auto-connection, from node to itself
27
+ r.add(connection);
28
+ continue;
29
+ }
30
+
31
+ for (let k = i + 1; k < node_count; k++) {
32
+ const node_1 = nodes[k];
33
+
34
+ if (node_0 === node_1) {
35
+ // duplicate node found
36
+ continue;
37
+ }
38
+
39
+ if (connection.isAttachedToNode(node_1.id)) {
40
+ r.add(connection);
41
+
42
+ // connection can only exist between two ports, so we're done
43
+ break;
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ return Array.from(r);
50
+ }