Skip to content

Commit a04fef9

Browse files
committed
mpf: Hook up events.
1 parent 3d7615c commit a04fef9

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

VisualPinball.Engine.Mpf.Unity/Runtime/MpfGamelogicEngine.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
// SOFTWARE.
1111

1212
using System;
13+
using System.Collections.Generic;
1314
using System.Linq;
15+
using Mpf.Vpe;
1416
using UnityEngine;
1517
using VisualPinball.Engine.Game.Engines;
1618
using VisualPinball.Unity;
@@ -36,20 +38,50 @@ public class MpfGamelogicEngine : MonoBehaviour, IGamelogicEngine
3638
public event EventHandler<CoilEventArgs> OnCoilChanged;
3739

3840
[NonSerialized]
39-
public MpfClient Client = new MpfClient();
41+
private MpfApi _api;
4042

4143
public string machineFolder;
4244

4345
[SerializeField] private GamelogicEngineSwitch[] availableSwitches = new GamelogicEngineSwitch[0];
4446
[SerializeField] private GamelogicEngineCoil[] availableCoils = new GamelogicEngineCoil[0];
4547
[SerializeField] private GamelogicEngineLamp[] availableLamps = new GamelogicEngineLamp[0];
4648

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+
4773
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
4874
{
75+
_api.StartGame(player.SwitchStatusesClosed);
4976
}
5077

5178
public void Switch(string id, bool isClosed)
5279
{
80+
if (_switchIds.ContainsKey(id)) {
81+
_api.Switch(_switchIds[id].ToString(), isClosed);
82+
} else {
83+
Debug.LogError("Unmapped MPF switch " + id);
84+
}
5385
}
5486

5587
public void GetMachineDescription()
@@ -59,5 +91,46 @@ public void GetMachineDescription()
5991
availableCoils = md.GetCoils().ToArray();
6092
availableLamps = md.GetLights().ToArray();
6193
}
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+
}
62135
}
63136
}

0 commit comments

Comments
 (0)