diff --git a/VisualPinball.Engine.PinMAME.Unity/Runtime/PinMameGamelogicEngine.cs b/VisualPinball.Engine.PinMAME.Unity/Runtime/PinMameGamelogicEngine.cs index 9edee6e..68f39fb 100644 --- a/VisualPinball.Engine.PinMAME.Unity/Runtime/PinMameGamelogicEngine.cs +++ b/VisualPinball.Engine.PinMAME.Unity/Runtime/PinMameGamelogicEngine.cs @@ -101,8 +101,12 @@ public GamelogicEngineLamp[] RequestedLamps { [NonSerialized] private Player _player; [NonSerialized] private PinMame.PinMame _pinMame; + [NonSerialized] private BallManager _ballManager; + [NonSerialized] private PlayfieldComponent _playfieldComponent; + [SerializeReference] private PinMameGame _game; + private Dictionary _switches = new(); private Dictionary _pinMameIdToSwitchIdMappings = new(); private Dictionary _switchIdToPinMameIdMappings = new(); @@ -218,6 +222,8 @@ private void OnDestroy() public void OnInit(Player player, TableApi tableApi, BallManager ballManager) { string vpmPath = null; + _ballManager = ballManager; + _playfieldComponent = GetComponentInChildren(); #if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR vpmPath = Path.Combine(Application.persistentDataPath, "pinmame"); @@ -327,6 +333,7 @@ private void UpdateCaches() _pinMameIdToSwitchIdMappings.Clear(); _switchIdToPinMameIdMappings.Clear(); + // check aliases first (the switches/coils that aren't an integer) foreach (var alias in _game.AvailableAliases) { switch (alias.AliasType) { case AliasType.Switch: @@ -346,16 +353,30 @@ private void UpdateCaches() } } - + // retrieve the game's switches foreach (var @switch in _game.AvailableSwitches) { _switches[@switch.Id] = @switch; - if (int.TryParse(@switch.Id, out int pinMameId)) { + if (int.TryParse(@switch.Id, out var pinMameId)) { _pinMameIdToSwitchIdMappings[pinMameId] = @switch.Id; _switchIdToPinMameIdMappings[@switch.Id] = pinMameId; + + // add mappings with prefixed 0. + if (pinMameId < 10) { + _switchIdToPinMameIdMappings["0" + @switch.Id] = pinMameId; + _switchIdToPinMameIdMappings["00" + @switch.Id] = pinMameId; + + _switches["0" + @switch.Id] = @switch; + _switches["00" + @switch.Id] = @switch; + } + if (pinMameId < 100) { + _switchIdToPinMameIdMappings["0" + @switch.Id] = pinMameId; + _switches["0" + @switch.Id] = @switch; + } } } + // retrieve the game's coils foreach (var coil in _game.AvailableCoils) { _coils[coil.Id] = coil; @@ -365,6 +386,7 @@ private void UpdateCaches() } } + // retrieve the game's lamps foreach (var lamp in _game.AvailableLamps) { _lamps[lamp.Id] = lamp; @@ -712,6 +734,10 @@ public void Switch(string id, bool isClosed) } Logger.Info($"[PinMAME] => sw {id}: {isClosed} | {_switches[id].Description}"); _pinMame.SetSwitch(_switchIdToPinMameIdMappings[_switches[id].Id], isClosed); + } else if (id == "s_spawn_ball") { + if (isClosed) { + _ballManager.CreateBall(new DebugBallCreator(630, _playfieldComponent.Height / 2f, _playfieldComponent.TableHeight)); + } } else { Logger.Error($"[PinMAME] Unknown switch \"{id}\"."); }