Skip to content

Commit 78fb9c5

Browse files
committed
resolve #3
1 parent 0995bdc commit 78fb9c5

File tree

4 files changed

+93
-63
lines changed

4 files changed

+93
-63
lines changed

static/component.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class Socket {
7575
this.onCount = 0;
7676
}
7777

78-
changeState(state) {
79-
if (state === this.on) return;
78+
changeState(state, forceCalculate) {
79+
if (state === this.on && !forceCalculate) return;
8080

8181
this.on = state;
8282
this.surface.color = state ? Socket.ON_COLOR : Socket.OFF_COLOR;
@@ -85,7 +85,7 @@ class Socket {
8585
getConnectedWires(this).forEach(wire => wire.calculate());
8686
} else {
8787
let connectedComponent = getConnectedComponent(this);
88-
componentCalculationQueue.push(connectedComponent)
88+
componentCalculationQueue.push(connectedComponent);
8989
}
9090

9191
this.tickCount++;
@@ -260,7 +260,7 @@ class Component {
260260
this.outSockets.forEach(socket => socket.render());
261261
}
262262

263-
calculate() {}
263+
calculate(forceCalculate) {}
264264

265265
delete() {
266266
this.inSockets.forEach(socket => getConnectedWires(socket).forEach(wire => wire.delete()));
@@ -418,8 +418,8 @@ class TrueComponent extends Component {
418418
this.calculate();
419419
}
420420

421-
calculate() {
422-
this.outSockets[0].changeState(true);
421+
calculate(forceCalculate) {
422+
this.outSockets[0].changeState(true, forceCalculate);
423423
}
424424

425425
flatten() {
@@ -435,8 +435,8 @@ class NotComponent extends Component {
435435
this.calculate();
436436
}
437437

438-
calculate() {
439-
this.outSockets[0].changeState(!this.inSockets[0].on);
438+
calculate(forceCalculate) {
439+
this.outSockets[0].changeState(!this.inSockets[0].on, forceCalculate);
440440
}
441441

442442
getSignal() {
@@ -461,8 +461,8 @@ class OrComponent extends Component {
461461
this.calculate();
462462
}
463463

464-
calculate() {
465-
this.outSockets[0].changeState(this.inSockets[0].on || this.inSockets[1].on);
464+
calculate(forceCalculate) {
465+
this.outSockets[0].changeState(this.inSockets[0].on || this.inSockets[1].on, forceCalculate);
466466
}
467467

468468
getSignal() {
@@ -507,8 +507,8 @@ class IntegratedComponent extends Component {
507507
this.components.forEach(component => component.tick());
508508
}
509509

510-
calculate() {
511-
this.inComponents.forEach(component => component.calculate());
510+
calculate(forceCalculate) {
511+
this.inComponents.forEach(component => component.calculate(forceCalculate));
512512
}
513513

514514
getSignal() {
@@ -733,7 +733,7 @@ function getClosestSocket(boardX, boardY) {
733733
return closestSocket;
734734
}
735735

736-
function connectWire(fromSocket, toSocket, camera) {
736+
function connectWire(fromSocket, toSocket, camera, calculateFlag) {
737737
let wire = new Wire(fromSocket, toSocket, camera);
738738

739739
// check if wire is already existing,
@@ -763,7 +763,8 @@ function connectWire(fromSocket, toSocket, camera) {
763763
});
764764
}
765765
wires.push(wire);
766-
wire.calculate();
766+
if (calculateFlag === undefined || calculateFlag === true)
767+
wire.calculate();
767768
}
768769

769770
function getClosestComponent(x, y) {

static/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ function tickAbstraction() {
197197
let cpf = 0; // calculations per frame
198198
let ccList = []; // calculation components count
199199
let cccDelta = 0;
200+
let forceCalculationQueue = [];
200201
function tickCalculateComponents() {
201202
cpf = 0;
202203
if (cccDelta > FPS * 5) {
@@ -206,7 +207,12 @@ function tickCalculateComponents() {
206207
for (let i = 0; i < calculationLimit && componentCalculationQueue.length > 0; i++) {
207208
let component = componentCalculationQueue.shift();
208209
if (component) {
209-
component.calculate();
210+
let forceIndex = forceCalculationQueue.indexOf(component);
211+
let forceCalculate = forceIndex !== -1;
212+
component.calculate(forceCalculate);
213+
if (forceCalculate) {
214+
forceCalculationQueue.splice(forceIndex, 1);
215+
}
210216
if (ccList.indexOf(component.id) === -1) {
211217
ccList.push(component.id);
212218
}

static/pack.js

+71-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
1+
function getIndexRecursively(component, components) {
2+
let index = components.indexOf(component);
3+
if (index !== -1) return [index];
4+
5+
let result;
6+
components.filter(c => c instanceof IntegratedComponent).forEach((ic, i) => {
7+
let index = getIndexRecursively(component, ic.components);
8+
if (index === undefined) result = [i];
9+
else if (index.length > 0) result = [i, ...index];
10+
});
11+
12+
return result;
13+
}
14+
15+
function stringifyTab(tab, abstractComponentIds) {
16+
let result = {
17+
name: tab.name,
18+
components: [],
19+
wireIndexes: [],
20+
queuedComponentIndexes: []
21+
};
22+
// result.wireIndexes will contain the socket addresses of components.
23+
// for example, if enabled (state = true) wire A is originally connected from result.components[1]'s 1st outSockets
24+
// to result.components[0]'s 2nd inSockets, wire A will be represented as [1, 0, 0, 1, true].
25+
//
26+
// result.queuedComponentIndexes will contain the index of components in result.components
27+
// which must be also contained on componentCalculationQueue when this stringified tab is structured.
28+
29+
tab.components.forEach(component => {
30+
if (component instanceof IntegratedComponent && abstractComponentIds.indexOf(component.integrationId) !== -1) {
31+
result.components.push(makeBlueprintString(component, component.getSignal()));
32+
} else {
33+
result.components.push(component.flatten())
34+
}
35+
});
36+
37+
tab.wires.forEach(wire => {
38+
let fromSocket = wire.fromSocket;
39+
let toSocket = wire.toSocket;
40+
41+
let fromSocketComponent = getConnectedComponent(fromSocket, tab.components, true);
42+
let toSocketComponent = getConnectedComponent(toSocket, tab.components, true);
43+
44+
result.wireIndexes.push([
45+
tab.components.indexOf(fromSocketComponent),
46+
fromSocketComponent.outSockets.indexOf(fromSocket),
47+
tab.components.indexOf(toSocketComponent),
48+
toSocketComponent.inSockets.indexOf(toSocket),
49+
wire.on
50+
]);
51+
});
52+
53+
tab.componentCalculationQueue.forEach(component => {
54+
result.queuedComponentIndexes.push(getIndexRecursively(component, tab.components));
55+
});
56+
57+
return result;
58+
}
59+
160
/*
261
* creating the packed project
362
*/
@@ -87,10 +146,20 @@ function load(projectJSON) {
87146
// SI = socket index
88147
let fromComponent = components[fromCI], toComponent = components[toCI];
89148
let fromSocket = fromComponent.outSockets[fromSI], toSocket = toComponent.inSockets[toSI];
90-
connectWire(fromSocket, toSocket, camera);
149+
connectWire(fromSocket, toSocket, camera, false);
91150
});
92151

93-
tab.queuedComponentIndexes.forEach(index => componentCalculationQueue.push(components[index]));
152+
// tab.queuedComponentIndexes.forEach(index => componentCalculationQueue.push(components[index]));
153+
tab.queuedComponentIndexes.forEach(indexList => {
154+
let item = components;
155+
while (indexList.length > 0) {
156+
item = item[indexList.shift(0)];
157+
if (item instanceof IntegratedComponent)
158+
item = item.components;
159+
}
160+
componentCalculationQueue.push(item);
161+
forceCalculationQueue.push(item);
162+
});
94163
});
95164

96165
changeTab(projectJSON.nowTab);

static/tab.js

-46
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,5 @@ function changeTab(name) {
4545
pathDiv.innerText = name;
4646
}
4747

48-
function stringifyTab(tab, abstractComponentIds) {
49-
let result = {
50-
name: tab.name,
51-
components: [],
52-
wireIndexes: [],
53-
queuedComponentIndexes: []
54-
};
55-
// result.wireIndexes will contain the socket addresses of components.
56-
// for example, if enabled (state = true) wire A is originally connected from result.components[1]'s 1st outSockets
57-
// to result.components[0]'s 2nd inSockets, wire A will be represented as [1, 0, 0, 1, true].
58-
//
59-
// result.queuedComponentIndexes will contain the index of components in result.components
60-
// which must be also contained on componentCalculationQueue when this stringified tab is structured.
61-
62-
tab.components.forEach(component => {
63-
if (component instanceof IntegratedComponent && abstractComponentIds.indexOf(component.integrationId) !== -1) {
64-
result.components.push(makeBlueprintString(component, component.getSignal()));
65-
} else {
66-
result.components.push(component.flatten())
67-
}
68-
});
69-
70-
tab.wires.forEach(wire => {
71-
let fromSocket = wire.fromSocket;
72-
let toSocket = wire.toSocket;
73-
74-
let fromSocketComponent = getConnectedComponent(fromSocket, tab.components, true);
75-
let toSocketComponent = getConnectedComponent(toSocket, tab.components, true);
76-
77-
result.wireIndexes.push([
78-
tab.components.indexOf(fromSocketComponent),
79-
fromSocketComponent.outSockets.indexOf(fromSocket),
80-
tab.components.indexOf(toSocketComponent),
81-
toSocketComponent.inSockets.indexOf(toSocket),
82-
wire.on
83-
]);
84-
});
85-
86-
tab.componentCalculationQueue.forEach(component => {
87-
if (tab.componentCalculationQueue.indexOf(component) !== -1)
88-
result.queuedComponentIndexes.push(tab.components.indexOf(component));
89-
});
90-
91-
return result;
92-
}
93-
9448
createTab(nowTab);
9549
changeTab(nowTab);

0 commit comments

Comments
 (0)