29
29
namespace VisualPinball . Engine . PinMAME
30
30
{
31
31
[ AddComponentMenu ( "Visual Pinball/PinMAME/PinMAME Mech Handler" ) ]
32
- public class PinMameMechComponent : MonoBehaviour , IMechHandler , ISwitchDeviceComponent , ISerializationCallbackReceiver
32
+ public class PinMameMechComponent : MonoBehaviour , IMechHandler , ISwitchDeviceComponent , ICoilDeviceComponent , ISerializationCallbackReceiver
33
33
{
34
34
#region Data
35
35
36
36
[ Tooltip ( "This defines how the mech is controlled." ) ]
37
37
public PinMameMechType Type = PinMameMechType . OneDirectionalSolenoid ;
38
38
39
- [ Tooltip ( "The first solenoid handled by the mech." ) ]
40
- public int Solenoid1 ;
41
-
42
- [ Tooltip ( "The second solenoid handled by the mech." ) ]
43
- public int Solenoid2 ;
44
-
45
39
[ Tooltip ( "How the mech behaves when the end of the range of motion is reached." ) ]
46
40
public PinMameRepeat Repeat ;
47
41
@@ -71,6 +65,7 @@ public class PinMameMechComponent : MonoBehaviour, IMechHandler, ISwitchDeviceCo
71
65
[ Tooltip ( "The amount of time in milliseconds required to reach full speed." ) ]
72
66
public int Acceleration ;
73
67
68
+ [ Unit ( "times acceleration" ) ]
74
69
[ Min ( 0 ) ]
75
70
[ Tooltip ( "Retardation factor. One means same as acceleration, 0.5 half as fast, etc." ) ]
76
71
public int Retardation ;
@@ -79,9 +74,12 @@ public class PinMameMechComponent : MonoBehaviour, IMechHandler, ISwitchDeviceCo
79
74
80
75
#region Access
81
76
77
+ [ SerializeField ] private string _solenoid1 ;
78
+ [ SerializeField ] private string _solenoid2 ;
79
+
82
80
public event EventHandler < MechEventArgs > OnMechUpdate ;
83
81
84
- public PinMameMechConfig Config ( List < SwitchMapping > switchMappings )
82
+ public PinMameMechConfig Config ( List < SwitchMapping > switchMappings , List < CoilMapping > coilMappings )
85
83
{
86
84
var type = 0u ;
87
85
type |= Type switch {
@@ -104,10 +102,13 @@ public PinMameMechConfig Config(List<SwitchMapping> switchMappings)
104
102
type |= FastUpdates ? ( uint ) PinMameMechFlag . Fast : ( uint ) PinMameMechFlag . Slow ;
105
103
type |= ResultByLength ? ( uint ) PinMameMechFlag . LengthSw : ( uint ) PinMameMechFlag . StepSw ;
106
104
105
+ var coilMapping1 = coilMappings . FirstOrDefault ( cm => ReferenceEquals ( cm . Device , this ) && cm . DeviceItem == _solenoid1 ) ;
106
+ var coilMapping2 = coilMappings . FirstOrDefault ( cm => ReferenceEquals ( cm . Device , this ) && cm . DeviceItem == _solenoid2 ) ;
107
+
107
108
var mechConfig = new PinMameMechConfig (
108
109
type ,
109
- Solenoid1 ,
110
- Solenoid2 ,
110
+ coilMapping1 ? . InternalId ?? 0 ,
111
+ coilMapping2 ? . InternalId ?? 0 ,
111
112
Length ,
112
113
Steps ,
113
114
0 ,
@@ -118,14 +119,27 @@ public PinMameMechConfig Config(List<SwitchMapping> switchMappings)
118
119
foreach ( var mark in Marks ) {
119
120
var switchMapping = switchMappings . FirstOrDefault ( sm => ReferenceEquals ( sm . Device , this ) && sm . DeviceItem == mark . SwitchId ) ;
120
121
if ( switchMapping == null ) {
121
- Logger . Error ( $ "Switch \" { mark . Description } \" for mech { name } is not mapped in the switch manager, ignoring.") ;
122
+ Logger . Error ( $ "Switch \" { mark . Name } \" for mech { name } is not mapped in the switch manager, ignoring.") ;
122
123
continue ;
123
124
}
124
- mechConfig . AddSwitch ( new PinMameMechSwitchConfig ( switchMapping . InternalId , mark . StepBeginning , mark . StepEnd , mark . Pulse ) ) ;
125
- }
126
125
127
- return mechConfig ;
126
+ switch ( mark . Type ) {
127
+ case MechMarkSwitchType . EnableBetween :
128
+ mechConfig . AddSwitch ( new PinMameMechSwitchConfig ( switchMapping . InternalId , mark . StepBeginning , mark . StepEnd ) ) ;
129
+ break ;
130
+
131
+ case MechMarkSwitchType . AlwaysPulse :
132
+ mechConfig . AddSwitch ( new PinMameMechSwitchConfig ( switchMapping . InternalId , - mark . PulseFreq , mark . PulseDuration ) ) ;
133
+ break ;
128
134
135
+ case MechMarkSwitchType . PulseBetween :
136
+ mechConfig . AddSwitch ( new PinMameMechSwitchConfig ( switchMapping . InternalId , mark . StepBeginning , mark . StepEnd , mark . PulseFreq ) ) ;
137
+ break ;
138
+ default :
139
+ throw new ArgumentOutOfRangeException ( ) ;
140
+ }
141
+ }
142
+ return mechConfig ;
129
143
}
130
144
131
145
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
@@ -140,6 +154,32 @@ public PinMameMechConfig Config(List<SwitchMapping> switchMappings)
140
154
141
155
IEnumerable < GamelogicEngineSwitch > IDeviceComponent < GamelogicEngineSwitch > . AvailableDeviceItems => AvailableSwitches ;
142
156
157
+ IEnumerable < IGamelogicEngineDeviceItem > IDeviceComponent < IGamelogicEngineDeviceItem > . AvailableDeviceItems => AvailableCoils ;
158
+
159
+ public IEnumerable < IGamelogicEngineDeviceItem > AvailableWireDestinations => AvailableCoils ;
160
+
161
+ IEnumerable < GamelogicEngineCoil > IDeviceComponent < GamelogicEngineCoil > . AvailableDeviceItems => AvailableCoils ;
162
+
163
+ public IEnumerable < GamelogicEngineCoil > AvailableCoils {
164
+ get {
165
+ switch ( Type )
166
+ {
167
+ case PinMameMechType . OneSolenoid :
168
+ case PinMameMechType . OneDirectionalSolenoid :
169
+ return new [ ] { new GamelogicEngineCoil ( _solenoid1 ) { Description = "Mech Solenoid" } } ;
170
+ case PinMameMechType . TwoDirectionalSolenoids :
171
+ case PinMameMechType . TwoStepperSolenoids :
172
+ case PinMameMechType . FourStepperSolenoids :
173
+ return new [ ] {
174
+ new GamelogicEngineCoil ( _solenoid1 ) { Description = "Mech Solenoid 1" } ,
175
+ new GamelogicEngineCoil ( _solenoid2 ) { Description = "Mech Solenoid 2" } ,
176
+ } ;
177
+ default :
178
+ throw new ArgumentOutOfRangeException ( ) ;
179
+ }
180
+ }
181
+ }
182
+
143
183
#endregion
144
184
145
185
#region Runtime
@@ -167,6 +207,14 @@ public void OnBeforeSerialize()
167
207
{
168
208
#if UNITY_EDITOR
169
209
210
+ if ( string . IsNullOrEmpty ( _solenoid1 ) ) {
211
+ _solenoid1 = GenerateCoilId ( ) ;
212
+ }
213
+ if ( string . IsNullOrEmpty ( _solenoid2 ) || _solenoid1 == _solenoid2 ) {
214
+ _solenoid2 = GenerateCoilId ( ) ;
215
+ }
216
+
217
+
170
218
var switchIds = new HashSet < string > ( ) ;
171
219
foreach ( var mark in Marks ) {
172
220
if ( ! mark . HasId || switchIds . Contains ( mark . SwitchId ) ) {
@@ -181,6 +229,8 @@ public void OnAfterDeserialize()
181
229
{
182
230
}
183
231
232
+ private string GenerateCoilId ( ) => $ "coil_{ Guid . NewGuid ( ) . ToString ( ) [ ..8 ] } ";
233
+
184
234
#endregion
185
235
}
186
236
}
0 commit comments