|
| 1 | +// Visual Pinball Engine |
| 2 | +// Copyright (C) 2021 freezy and VPE Team |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +using System; |
| 18 | +using Unity.VisualScripting; |
| 19 | +using UnityEngine; |
| 20 | + |
| 21 | +namespace VisualPinball.Unity.VisualScripting |
| 22 | +{ |
| 23 | + [DisallowMultipleComponent] |
| 24 | + [RequireComponent(typeof(IGamelogicEngine))] |
| 25 | + [RequireComponent(typeof(Player))] |
| 26 | + [AddComponentMenu("Visual Pinball/Game Logic Engine/Visual Scripting Bridge")] |
| 27 | + public class VisualScriptingGamelogicBridge : MonoBehaviour |
| 28 | + { |
| 29 | + private IGamelogicEngine _gle; |
| 30 | + private Player _player; |
| 31 | + |
| 32 | + private void Awake() |
| 33 | + { |
| 34 | + _gle = GetComponent<IGamelogicEngine>(); |
| 35 | + _player = GetComponent<Player>(); |
| 36 | + if (_gle == null) { |
| 37 | + Debug.LogWarning("Cannot find gamelogic engine."); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (_player == null) { |
| 41 | + Debug.LogWarning("Cannot find player."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + _gle.OnSwitchChanged += OnSwitchChanged; |
| 46 | + _gle.OnCoilChanged += OnCoilChanged; |
| 47 | + _gle.OnLampChanged += OnLampChanged; |
| 48 | + _player.OnPlayerStarted += OnPlayerStarted; |
| 49 | + } |
| 50 | + |
| 51 | + private void OnDestroy() |
| 52 | + { |
| 53 | + if (_player != null) { |
| 54 | + _player.OnPlayerStarted -= OnPlayerStarted; |
| 55 | + } |
| 56 | + if (_gle != null) { |
| 57 | + _gle.OnSwitchChanged -= OnSwitchChanged; |
| 58 | + _gle.OnCoilChanged -= OnCoilChanged; |
| 59 | + _gle.OnLampChanged -= OnLampChanged; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static void OnSwitchChanged(object sender, SwitchEventArgs2 e) |
| 64 | + { |
| 65 | + EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, new SwitchEventArgs2(e.Id, e.IsEnabled)); |
| 66 | + } |
| 67 | + |
| 68 | + private static void OnCoilChanged(object sender, CoilEventArgs e) |
| 69 | + { |
| 70 | + EventBus.Trigger(VisualScriptingEventNames.CoilEvent, e); |
| 71 | + } |
| 72 | + |
| 73 | + private static void OnPlayerStarted(object sender, EventArgs e) |
| 74 | + { |
| 75 | + EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty); |
| 76 | + } |
| 77 | + |
| 78 | + private static void OnLampChanged(object sender, LampEventArgs e) |
| 79 | + { |
| 80 | + EventBus.Trigger(VisualScriptingEventNames.LampEvent, e); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments