14
14
// You should have received a copy of the GNU General Public License
15
15
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
17
+ using System ;
17
18
using System . Collections . Generic ;
18
19
using System . Collections . ObjectModel ;
19
20
using Unity . VisualScripting ;
20
21
using UnityEngine ;
22
+ using VisualPinball . Engine . Game . Engines ;
21
23
22
24
namespace VisualPinball . Unity . VisualScripting
23
25
{
@@ -27,8 +29,11 @@ namespace VisualPinball.Unity.VisualScripting
27
29
[ UnitCategory ( "Visual Pinball" ) ]
28
30
public class SwitchLampUnit : GleUnit , IMultiInputUnit
29
31
{
30
- [ SerializeAs ( nameof ( inputCount ) ) ]
31
- private int _inputCount = 1 ;
32
+ [ Serialize , Inspectable , UnitHeaderInspectable ( "Match" ) ]
33
+ public LampDataType MatchDataType { get ; set ; }
34
+
35
+ [ Serialize , Inspectable , UnitHeaderInspectable ( "Non Match" ) ]
36
+ public LampDataType NonMatchDataType { get ; set ; }
32
37
33
38
[ DoNotSerialize ]
34
39
[ Inspectable , UnitHeaderInspectable ( "Lamp IDs" ) ]
@@ -46,10 +51,19 @@ public int inputCount
46
51
[ PortLabelHidden ]
47
52
public ControlOutput OutputTrigger ;
48
53
54
+ [ SerializeAs ( nameof ( inputCount ) ) ]
55
+ private int _inputCount = 1 ;
56
+
49
57
[ DoNotSerialize ]
50
58
[ PortLabel ( "Source Value" ) ]
51
59
public ValueInput SourceValue { get ; private set ; }
52
60
61
+ [ DoNotSerialize ]
62
+ public ValueInput Match { get ; private set ; }
63
+
64
+ [ DoNotSerialize ]
65
+ public ValueInput NonMatch { get ; private set ; }
66
+
53
67
[ DoNotSerialize ]
54
68
public ReadOnlyCollection < ValueInput > multiInputs { get ; private set ; }
55
69
@@ -62,19 +76,33 @@ protected override void Definition()
62
76
63
77
SourceValue = ValueInput < int > ( nameof ( SourceValue ) ) ;
64
78
65
- var _multiInputs = new List < ValueInput > ( ) ;
66
-
67
- multiInputs = _multiInputs . AsReadOnly ( ) ;
79
+ var mi = new List < ValueInput > ( ) ;
80
+ multiInputs = mi . AsReadOnly ( ) ;
68
81
69
82
for ( var i = 0 ; i < inputCount ; i ++ ) {
70
83
var input = ValueInput ( i . ToString ( ) , LampIdValue . Empty . ToJson ( ) ) ;
71
- _multiInputs . Add ( input ) ;
72
-
84
+ mi . Add ( input ) ;
73
85
Requirement ( input , InputTrigger ) ;
74
86
}
75
87
76
88
_lampIdValueCache . Clear ( ) ;
77
89
90
+ Match = MatchDataType switch {
91
+ LampDataType . OnOff => ValueInput ( nameof ( Match ) , false ) ,
92
+ LampDataType . Status => ValueInput ( nameof ( Match ) , LampStatus . Off ) ,
93
+ LampDataType . Intensity => ValueInput ( nameof ( Match ) , 0f ) ,
94
+ LampDataType . Color => ValueInput ( nameof ( Match ) , UnityEngine . Color . white ) ,
95
+ _ => throw new ArgumentOutOfRangeException ( )
96
+ } ;
97
+
98
+ NonMatch = NonMatchDataType switch {
99
+ LampDataType . OnOff => ValueInput ( nameof ( NonMatch ) , false ) ,
100
+ LampDataType . Status => ValueInput ( nameof ( NonMatch ) , LampStatus . Off ) ,
101
+ LampDataType . Intensity => ValueInput ( nameof ( NonMatch ) , 0f ) ,
102
+ LampDataType . Color => ValueInput ( nameof ( NonMatch ) , UnityEngine . Color . white ) ,
103
+ _ => throw new ArgumentOutOfRangeException ( )
104
+ } ;
105
+
78
106
Succession ( InputTrigger , OutputTrigger ) ;
79
107
}
80
108
@@ -84,8 +112,13 @@ private ControlOutput Process(Flow flow)
84
112
Debug . LogError ( "Cannot find GLE." ) ;
85
113
return OutputTrigger ;
86
114
}
87
-
88
- var value = flow . GetValue < int > ( SourceValue ) ;
115
+
116
+ if ( ! AssertPlayer ( flow ) ) {
117
+ Debug . LogError ( "Cannot find player." ) ;
118
+ return OutputTrigger ;
119
+ }
120
+
121
+ var sourceValue = flow . GetValue < int > ( SourceValue ) ;
89
122
90
123
foreach ( var input in multiInputs ) {
91
124
var json = flow . GetValue < string > ( input ) ;
@@ -95,7 +128,26 @@ private ControlOutput Process(Flow flow)
95
128
}
96
129
97
130
var lampIdValue = _lampIdValueCache [ json . GetHashCode ( ) ] ;
98
- Gle . SetLamp ( lampIdValue . id , lampIdValue . value == value ? 255f : 0f ) ;
131
+
132
+ var dataType = lampIdValue . value == sourceValue ? MatchDataType : NonMatchDataType ;
133
+ var value = lampIdValue . value == sourceValue ? Match : NonMatch ;
134
+
135
+ switch ( dataType ) {
136
+ case LampDataType . OnOff :
137
+ Player . SetLamp ( lampIdValue . id , flow . GetValue < bool > ( value ) ? LampStatus . On : LampStatus . Off ) ;
138
+ break ;
139
+ case LampDataType . Status :
140
+ Player . SetLamp ( lampIdValue . id , flow . GetValue < LampStatus > ( value ) ) ;
141
+ break ;
142
+ case LampDataType . Intensity :
143
+ Player . SetLamp ( lampIdValue . id , flow . GetValue < float > ( value ) ) ;
144
+ break ;
145
+ case LampDataType . Color :
146
+ Player . SetLamp ( lampIdValue . id , flow . GetValue < UnityEngine . Color > ( value ) . ToEngineColor ( ) ) ;
147
+ break ;
148
+ default :
149
+ throw new ArgumentOutOfRangeException ( ) ;
150
+ }
99
151
}
100
152
101
153
return OutputTrigger ;
0 commit comments