예제 #1
0
        /// <summary>
        /// Sends the switch element to the gamelogic engine and linked wires.
        /// </summary>
        /// <param name="closed">Switch status</param>
        public void OnSwitch(bool closed)
        {
            if (_engine != null && _switchIds != null)
            {
                foreach (var switchConfig in _switchIds)
                {
                    // close the switch now
                    _engine.Switch(switchConfig.SwitchId, closed);

                    // if it's pulse, schedule to re-open
                    if (closed && switchConfig.IsPulseSwitch)
                    {
                        SimulationSystemGroup.ScheduleSwitch(switchConfig.PulseDelay,
                                                             () => _engine.Switch(switchConfig.SwitchId, false));
                    }
                }
            }

            if (_wires != null)
            {
                foreach (var wireConfig in _wires)
                {
                    IApiWireDest dest = null;
                    switch (wireConfig.Destination)
                    {
                    case WireDestination.Playfield:
                        dest = _player.Wire(wireConfig.PlayfieldItem);
                        break;

                    case WireDestination.Device:
                        var device = _player.WireDevice(wireConfig.Device);
                        if (device != null)
                        {
                            device.Wire(wireConfig.DeviceItem)?.OnChange(closed);
                        }
                        else
                        {
                            Logger.Warn($"Cannot find wire device \"{wireConfig.Device}\".");
                        }
                        break;
                    }

                    // close the switch now
                    dest?.OnChange(closed);

                    // if it's pulse, schedule to re-open
                    if (closed && wireConfig.IsPulseSource)
                    {
                        if (dest != null)
                        {
                            SimulationSystemGroup.ScheduleSwitch(wireConfig.PulseDelay,
                                                                 () => dest.OnChange(false));
                        }
                    }
                }
            }
        }
예제 #2
0
        public void ScheduleSwitch(bool closed, int delay)
        {
            if (_engine != null && _switchIds != null)
            {
                foreach (var switchConfig in _switchIds)
                {
                    SimulationSystemGroup.ScheduleSwitch(delay,
                                                         () => _engine.Switch(switchConfig.SwitchId, closed));
                }
            }
            else
            {
                Logger.Warn("Cannot schedule device switch.");
            }

            if (_wires != null)
            {
                foreach (var wireConfig in _wires)
                {
                    IApiWireDest dest = null;
                    switch (wireConfig.Destination)
                    {
                    case WireDestination.Playfield:
                        dest = _player.Wire(wireConfig.PlayfieldItem);
                        break;

                    case WireDestination.Device:
                        var device = _player.WireDevice(wireConfig.Device);
                        if (device != null)
                        {
                            dest = device.Wire(wireConfig.DeviceItem);
                        }
                        else
                        {
                            Logger.Warn($"Cannot find wire device \"{wireConfig.Device}\".");
                        }
                        break;
                    }

                    if (dest != null)
                    {
                        SimulationSystemGroup.ScheduleSwitch(wireConfig.PulseDelay,
                                                             () => dest.OnChange(closed));
                    }
                }
            }
        }