Skip to content

Commit acd7bdc

Browse files
committed
misc: misc clean up. Use dynamic table variable for Score Motor status.
1 parent 480d491 commit acd7bdc

File tree

5 files changed

+122
-83
lines changed

5 files changed

+122
-83
lines changed

Editor/Descriptors/EMAddPointsUnitDescriptor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public EMAddPointsUnitDescriptor(EMAddPointsUnit target) : base(target)
3030
protected override string DefinedSummary()
3131
{
3232
return "This node takes an incoming point value and pulses values to that can be used to simulate adding points to a score reel. "
33-
+ "\n\nFor example, an incoming point value of 500 will provide 5 pulses of 100.";
33+
+ "\n\nFor example, an incoming point value of 500 will provide 5 pulses of 100. "
34+
+ "\n\nSingle pulse points (1, 10, 100, 1000, 10000) will be blocked if the score motor is running and Block Points is enabled.";
3435
}
3536

3637
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Mech(Unity.Editor.IconSize.Large, Unity.Editor.IconColor.Orange));

Editor/Descriptors/EMResetPointsUnitDescriptor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4848
desc.summary = "The amount of time (in ms) the score motor runs.";
4949
break;
5050

51+
case nameof(EMAddPointsUnit.positions):
52+
desc.summary = "Score motor positions.";
53+
break;
54+
5155
case nameof(EMResetPointsUnit.started):
5256
desc.summary = "Triggered when score motor starts.";
5357
break;

Runtime/Gamelogic/State.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public T Get<T>(string variableId) where T : class
6161
return _variables[variableId].Get<T>();
6262
}
6363

64+
public bool IsDefined(string variableId) => _variables.ContainsKey(variableId);
65+
6466
public StateVariable GetVariable(string variableId) => _variables[variableId];
6567

6668
public object Get(string variableId)

Runtime/Nodes/EM/EMAddPointsUnit.cs

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class EMAddPointsUnit : GleUnit
6363
[PortLabel("Point Value")]
6464
public ValueOutput OutputPointValue { get; private set; }
6565

66+
private State State => VsGle.TableState;
6667
private static string VARIABLE_EM_SCORE_MOTOR = "EM_SCORE_MOTOR";
6768

6869
protected override void Definition()
@@ -88,75 +89,81 @@ protected override void Definition()
8889

8990
private IEnumerator Process(Flow flow)
9091
{
91-
var running = false;
92-
93-
if (Variables.Application.IsDefined(VARIABLE_EM_SCORE_MOTOR)) {
94-
running = Variables.Application.Get<bool>(VARIABLE_EM_SCORE_MOTOR);
92+
if (!AssertVsGle(flow)) {
93+
yield return OutputTrigger;
9594
}
95+
else {
96+
var running = false;
9697

97-
yield return OutputTrigger;
98-
99-
var points = flow.GetValue<int>(pointValue);
98+
if (State.IsDefined(VARIABLE_EM_SCORE_MOTOR)) {
99+
running = State.Get<Bool>(VARIABLE_EM_SCORE_MOTOR);
100+
}
101+
else {
102+
State.AddProperty(new StateVariable(VARIABLE_EM_SCORE_MOTOR, "", false));
103+
}
104+
105+
var points = flow.GetValue<int>(pointValue);
100106

101-
if (points > 0) {
102-
var pulses =
103-
(points % 100000 == 0) ? points / 100000 :
104-
(points % 10000 == 0) ? points / 10000 :
105-
(points % 1000 == 0) ? points / 1000 :
106-
(points % 100 == 0) ? points / 100 :
107-
(points % 10 == 0) ? points / 10 :
108-
points;
107+
if (points > 0) {
108+
var pulses =
109+
(points % 100000 == 0) ? points / 100000 :
110+
(points % 10000 == 0) ? points / 10000 :
111+
(points % 1000 == 0) ? points / 1000 :
112+
(points % 100 == 0) ? points / 100 :
113+
(points % 10 == 0) ? points / 10 :
114+
points;
109115

110-
if (pulses == 1) {
111-
if (!running || (running && !flow.GetValue<bool>(blockPoints))) {
112-
Debug.Log($"Single pulse triggering with {points} points");
116+
if (pulses == 1) {
117+
if (!running || (running && !flow.GetValue<bool>(blockPoints))) {
118+
Debug.Log($"Single pulse triggering with {points} points");
113119

114-
flow.SetValue(OutputPointValue, points);
120+
flow.SetValue(OutputPointValue, points);
115121

116-
yield return pulse;
122+
yield return pulse;
123+
}
117124
}
118-
}
119-
else if (running)
120-
{
121-
Debug.Log($"Score motor is already running.");
122-
}
123-
else {
124-
Debug.Log("Starting score motor");
125+
else if (running) {
126+
Debug.Log($"Score motor is already running.");
127+
}
128+
else {
129+
Debug.Log("Starting score motor");
125130

126-
Variables.Application.Set(VARIABLE_EM_SCORE_MOTOR, true);
127-
128-
yield return started;
131+
State.Set<Bool>(VARIABLE_EM_SCORE_MOTOR, true);
129132

130-
var motorPositions = flow.GetValue<int>(positions);
133+
yield return started;
131134

132-
var delay = (flow.GetValue<float>(duration) / 1000f) / motorPositions;
133-
var realtime = flow.GetValue<bool>(unscaledTime);
135+
var motorPositions = flow.GetValue<int>(positions);
134136

135-
var pointsPerPulse = points / pulses;
137+
var delay = (flow.GetValue<float>(duration) / 1000f) / motorPositions;
138+
var realtime = flow.GetValue<bool>(unscaledTime);
136139

137-
for (int loop = 0; loop < motorPositions; loop++) {
138-
var outputPoints = loop < pulses ? pointsPerPulse : 0;
140+
var pointsPerPulse = points / pulses;
139141

140-
Debug.Log($"Pulse {loop + 1} of {motorPositions} - waiting {delay}ms and triggering with {outputPoints} points");
142+
for (int loop = 0; loop < motorPositions; loop++) {
143+
var outputPoints = loop < pulses ? pointsPerPulse : 0;
141144

142-
if (realtime) {
143-
yield return new WaitForSecondsRealtime(delay);
144-
}
145-
else {
146-
yield return new WaitForSeconds(delay);
147-
}
145+
Debug.Log($"Pulse {loop + 1} of {motorPositions} - waiting {delay}ms and triggering with {outputPoints} points");
148146

149-
flow.SetValue(OutputPointValue, outputPoints);
147+
if (realtime) {
148+
yield return new WaitForSecondsRealtime(delay);
149+
}
150+
else {
151+
yield return new WaitForSeconds(delay);
152+
}
150153

151-
yield return pulse;
152-
}
154+
flow.SetValue(OutputPointValue, outputPoints);
155+
156+
yield return pulse;
157+
}
153158

154-
Debug.Log("Stopping score motor");
159+
Debug.Log("Stopping score motor");
155160

156-
Variables.Application.Set(VARIABLE_EM_SCORE_MOTOR, false);
161+
State.Set<Bool>(VARIABLE_EM_SCORE_MOTOR, false);
157162

158-
yield return stopped;
163+
yield return stopped;
164+
}
159165
}
166+
160167
}
161168
}
162169
}

Runtime/Nodes/EM/EMResetPointsUnit.cs

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ public class EMResetPointsUnit : GleUnit
3333
[DoNotSerialize]
3434
public ValueInput duration { get; private set; }
3535

36+
[DoNotSerialize]
37+
public ValueInput positions { get; private set; }
38+
3639
[DoNotSerialize]
3740
[PortLabel("Unscaled")]
3841
public ValueInput unscaledTime { get; private set; }
3942

43+
[DoNotSerialize]
44+
[PortLabelHidden]
45+
public ControlOutput OutputTrigger;
46+
4047
[DoNotSerialize]
4148
public ControlOutput started;
4249

@@ -53,17 +60,21 @@ public class EMResetPointsUnit : GleUnit
5360
[PortLabel("Point Value")]
5461
public ValueOutput OutputPointValue { get; private set; }
5562

56-
private Bool running = false;
63+
private State State => VsGle.TableState;
64+
private static string VARIABLE_EM_SCORE_MOTOR = "EM_SCORE_MOTOR";
5765

5866
protected override void Definition()
5967
{
6068
InputTrigger = ControlInputCoroutine(nameof(InputTrigger), Process);
6169

6270
pointValue = ValueInput(nameof(pointValue), 0);
6371

64-
duration = ValueInput(nameof(duration), .750f);
72+
positions = ValueInput(nameof(positions), 6);
73+
duration = ValueInput(nameof(duration), 750);
6574
unscaledTime = ValueInput(nameof(unscaledTime), false);
6675

76+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
77+
6778
started = ControlOutput(nameof(started));
6879
stopped = ControlOutput(nameof(stopped));
6980

@@ -74,49 +85,63 @@ protected override void Definition()
7485

7586
private IEnumerator Process(Flow flow)
7687
{
77-
if (running) {
78-
var points = flow.GetValue<int>(pointValue);
79-
80-
Debug.Log($"Score motor is already running. Ignoring {points} point(s).");
81-
82-
yield return null;
88+
if (!AssertVsGle(flow)) {
89+
yield return OutputTrigger;
8390
}
8491
else {
85-
Debug.Log("Starting score motor");
92+
yield return OutputTrigger;
93+
94+
var running = false;
8695

87-
yield return started;
96+
if (State.IsDefined(VARIABLE_EM_SCORE_MOTOR)) {
97+
running = State.Get<Bool>(VARIABLE_EM_SCORE_MOTOR);
98+
}
99+
else {
100+
State.AddProperty(new StateVariable(VARIABLE_EM_SCORE_MOTOR, "", false));
101+
}
88102

89-
running = true;
103+
if (running) {
104+
Debug.Log($"Score motor is already running.");
105+
}
106+
else {
107+
Debug.Log("Starting score motor");
90108

91-
var points = flow.GetValue<int>(pointValue);
109+
State.Set<Bool>(VARIABLE_EM_SCORE_MOTOR, true);
92110

93-
var seconds = flow.GetValue<float>(duration) / 6;
94-
var realtime = flow.GetValue<bool>(unscaledTime);
111+
yield return started;
95112

96-
while (points > 0) {
97-
for (int loop = 0; loop < 6; loop++) {
98-
points = AdvancePoints(points);
113+
var motorPositions = flow.GetValue<int>(positions);
99114

100-
Debug.Log($"Pulse {loop + 1} of 6 - waiting {seconds} and triggering with {points} points");
115+
var delay = (flow.GetValue<float>(duration) / 1000f) / motorPositions;
116+
var realtime = flow.GetValue<bool>(unscaledTime);
101117

102-
if (realtime) {
103-
yield return new WaitForSecondsRealtime(seconds);
104-
}
105-
else {
106-
yield return new WaitForSeconds(seconds);
107-
}
118+
var points = flow.GetValue<int>(pointValue);
108119

109-
flow.SetValue(OutputPointValue, points);
120+
while (points > 0) {
121+
for (int loop = 0; loop < motorPositions; loop++) {
122+
points = AdvancePoints(points);
110123

111-
yield return pulse;
112-
}
124+
Debug.Log($"Pulse {loop + 1} of {motorPositions} - waiting {delay}ms and triggering with {points} points");
125+
126+
if (realtime) {
127+
yield return new WaitForSecondsRealtime(delay);
128+
}
129+
else {
130+
yield return new WaitForSeconds(delay);
131+
}
132+
133+
flow.SetValue(OutputPointValue, points);
134+
135+
yield return pulse;
136+
}
137+
}
138+
139+
Debug.Log("Stopping score motor");
140+
141+
State.Set<Bool>(VARIABLE_EM_SCORE_MOTOR, false);
142+
143+
yield return stopped;
113144
}
114-
115-
Debug.Log("Stopping score motor");
116-
117-
running = false;
118-
119-
yield return stopped;
120145
}
121146
}
122147

0 commit comments

Comments
 (0)