10
10
// SOFTWARE.
11
11
12
12
using System ;
13
+ using System . Collections . Generic ;
13
14
using System . Linq ;
15
+ using Mpf . Vpe ;
14
16
using UnityEngine ;
15
17
using VisualPinball . Engine . Game . Engines ;
16
18
using VisualPinball . Unity ;
@@ -36,20 +38,50 @@ public class MpfGamelogicEngine : MonoBehaviour, IGamelogicEngine
36
38
public event EventHandler < CoilEventArgs > OnCoilChanged ;
37
39
38
40
[ NonSerialized ]
39
- public MpfClient Client = new MpfClient ( ) ;
41
+ private MpfApi _api ;
40
42
41
43
public string machineFolder ;
42
44
43
45
[ SerializeField ] private GamelogicEngineSwitch [ ] availableSwitches = new GamelogicEngineSwitch [ 0 ] ;
44
46
[ SerializeField ] private GamelogicEngineCoil [ ] availableCoils = new GamelogicEngineCoil [ 0 ] ;
45
47
[ SerializeField ] private GamelogicEngineLamp [ ] availableLamps = new GamelogicEngineLamp [ 0 ] ;
46
48
49
+ private Dictionary < string , int > _switchIds = new Dictionary < string , int > ( ) ;
50
+ private Dictionary < string , string > _coilNames = new Dictionary < string , string > ( ) ;
51
+
52
+ private void Awake ( )
53
+ {
54
+ _switchIds . Clear ( ) ;
55
+ foreach ( var sw in availableSwitches ) {
56
+ _switchIds [ sw . Id ] = sw . InternalId ;
57
+ }
58
+ _coilNames . Clear ( ) ;
59
+ foreach ( var coil in availableCoils ) {
60
+ _coilNames [ coil . InternalId . ToString ( ) ] = coil . Id ;
61
+ }
62
+ _api = new MpfApi ( machineFolder ) ;
63
+ _api . Launch ( ) ;
64
+
65
+ _api . Client . OnEnableCoil += OnEnableCoil ;
66
+ _api . Client . OnDisableCoil += OnDisableCoil ;
67
+ _api . Client . OnPulseCoil += OnPulseCoil ;
68
+ _api . Client . OnConfigureHardwareRule += OnConfigureHardwareRule ;
69
+ _api . Client . OnRemoveHardwareRule += OnRemoveHardwareRule ;
70
+ _api . Client . OnFadeLight += OnFadeLight ;
71
+ }
72
+
47
73
public void OnInit ( Player player , TableApi tableApi , BallManager ballManager )
48
74
{
75
+ _api . StartGame ( player . SwitchStatusesClosed ) ;
49
76
}
50
77
51
78
public void Switch ( string id , bool isClosed )
52
79
{
80
+ if ( _switchIds . ContainsKey ( id ) ) {
81
+ _api . Switch ( _switchIds [ id ] . ToString ( ) , isClosed ) ;
82
+ } else {
83
+ Debug . LogError ( "Unmapped MPF switch " + id ) ;
84
+ }
53
85
}
54
86
55
87
public void GetMachineDescription ( )
@@ -59,5 +91,46 @@ public void GetMachineDescription()
59
91
availableCoils = md . GetCoils ( ) . ToArray ( ) ;
60
92
availableLamps = md . GetLights ( ) . ToArray ( ) ;
61
93
}
94
+
95
+ private void OnEnableCoil ( object sender , EnableCoilRequest e )
96
+ {
97
+ if ( _coilNames . ContainsKey ( e . CoilNumber ) ) {
98
+ OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( _coilNames [ e . CoilNumber ] , true ) ) ;
99
+ } else {
100
+ Debug . LogError ( "Unmapped MPF coil " + e . CoilNumber ) ;
101
+ }
102
+ }
103
+
104
+ private void OnDisableCoil ( object sender , DisableCoilRequest e )
105
+ {
106
+ if ( _coilNames . ContainsKey ( e . CoilNumber ) ) {
107
+ OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( _coilNames [ e . CoilNumber ] , false ) ) ;
108
+ } else {
109
+ Debug . LogError ( "Unmapped MPF coil " + e . CoilNumber ) ;
110
+ }
111
+ }
112
+
113
+ private void OnPulseCoil ( object sender , PulseCoilRequest e )
114
+ {
115
+ }
116
+
117
+ private void OnFadeLight ( object sender , FadeLightRequest e )
118
+ {
119
+ }
120
+
121
+ private void OnRemoveHardwareRule ( object sender , RemoveHardwareRuleRequest e )
122
+ {
123
+ }
124
+
125
+ private void OnConfigureHardwareRule ( object sender , ConfigureHardwareRuleRequest e )
126
+ {
127
+ }
128
+
129
+
130
+ private void OnDestroy ( )
131
+ {
132
+ _api . Client . OnEnableCoil -= OnEnableCoil ;
133
+ _api . Dispose ( ) ;
134
+ }
62
135
}
63
136
}
0 commit comments