diff --git a/Runtime/Gamelogic/VisualScriptingEventNames.cs b/Runtime/Gamelogic/VisualScriptingEventNames.cs index 9eef2cc..7df4dcc 100644 --- a/Runtime/Gamelogic/VisualScriptingEventNames.cs +++ b/Runtime/Gamelogic/VisualScriptingEventNames.cs @@ -18,7 +18,7 @@ namespace VisualPinball.Unity { public static class VisualScriptingEventNames { - public const string PlayerStartedEvent = "PlayerStartedEvent"; + public const string GleStartedEvent = "GleStartedEvent"; public const string LampEvent = "LampEvent"; public const string SwitchEvent = "SwitchEvent"; public const string CoilEvent = "CoilEvent"; diff --git a/Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs b/Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs index 3959672..fc91985 100644 --- a/Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs +++ b/Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs @@ -23,37 +23,54 @@ namespace VisualPinball.Unity.VisualScripting [DisallowMultipleComponent] [RequireComponent(typeof(IGamelogicEngine))] [RequireComponent(typeof(Player))] - [AddComponentMenu("Visual Pinball/Game Logic Engine/Visual Scripting Bridge")] + [AddComponentMenu("Visual Pinball/Gamelogic Engine/Visual Scripting Bridge")] public class VisualScriptingGamelogicBridge : MonoBehaviour { - private IGamelogicEngine _gle; private Player _player; + private IGamelogicEngine _gle; + + private bool _init; private void Awake() { - _gle = GetComponent(); + _init = false; + _player = GetComponent(); - if (_gle == null) { - Debug.LogWarning("Cannot find gamelogic engine."); - return; - } if (_player == null) { Debug.LogWarning("Cannot find player."); - return; } - _player.OnPlayerStarted += OnPlayerStarted; + _gle = GetComponent(); + if (_gle != null) { + _gle.OnStarted += OnStarted; + } + else { + Debug.LogWarning("Cannot find gamelogic engine."); + } } - private void OnDestroy() - { - if (_player != null) { - _player.OnPlayerStarted -= OnPlayerStarted; + private void OnDestroy() { + if (_gle != null) { + _gle.OnStarted -= OnStarted; + + if (_init) { + _gle.OnSwitchChanged -= OnSwitchChanged; + _gle.OnCoilChanged -= OnCoilChanged; + _gle.OnLampChanged -= OnLampChanged; + } } + } + + private void OnStarted(object sender, EventArgs e) + { if (_gle != null) { - _gle.OnSwitchChanged -= OnSwitchChanged; - _gle.OnCoilChanged -= OnCoilChanged; - _gle.OnLampChanged -= OnLampChanged; + _gle.OnSwitchChanged += OnSwitchChanged; + _gle.OnCoilChanged += OnCoilChanged; + _gle.OnLampChanged += OnLampChanged; + + _init = true; + + EventBus.Trigger(VisualScriptingEventNames.GleStartedEvent, e); } } @@ -67,21 +84,9 @@ private static void OnCoilChanged(object sender, CoilEventArgs e) EventBus.Trigger(VisualScriptingEventNames.CoilEvent, e); } - private void OnPlayerStarted(object sender, EventArgs e) - { - if (_gle != null) { - _gle.OnSwitchChanged += OnSwitchChanged; - _gle.OnCoilChanged += OnCoilChanged; - _gle.OnLampChanged += OnLampChanged; - } - - EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty); - } - private static void OnLampChanged(object sender, LampEventArgs e) { EventBus.Trigger(VisualScriptingEventNames.LampEvent, e); } - } } diff --git a/Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs b/Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs index 39e50fc..19daf9a 100644 --- a/Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs +++ b/Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs @@ -25,7 +25,7 @@ namespace VisualPinball.Unity.VisualScripting { [DisallowMultipleComponent] - [AddComponentMenu("Visual Pinball/Game Logic Engine/Visual Scripting Game Logic")] + [AddComponentMenu("Visual Pinball/Gamelogic Engine/Visual Scripting Game Logic")] public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine { public string Name => "Visual Scripting Gamelogic Engine"; @@ -51,6 +51,7 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine public event EventHandler OnLampColorChanged; public event EventHandler OnCoilChanged; public event EventHandler OnSwitchChanged; + public event EventHandler OnStarted; [NonSerialized] public BallManager BallManager; [NonSerialized] private Player _player; @@ -59,7 +60,8 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager) { _player = player; BallManager = ballManager; - EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty); + + EventBus.Trigger(VisualScriptingEventNames.GleStartedEvent, EventArgs.Empty); } public void Switch(string id, bool isClosed) diff --git a/Runtime/Nodes/PlayerStartedEventUnit.cs b/Runtime/Nodes/GleStartedEventUnit.cs similarity index 79% rename from Runtime/Nodes/PlayerStartedEventUnit.cs rename to Runtime/Nodes/GleStartedEventUnit.cs index a177189..55a963a 100644 --- a/Runtime/Nodes/PlayerStartedEventUnit.cs +++ b/Runtime/Nodes/GleStartedEventUnit.cs @@ -19,20 +19,15 @@ namespace VisualPinball.Unity.VisualScripting { - [UnitTitle("On Player Started Event")] + [UnitTitle("On Gamelogic Engine Started Event")] [UnitCategory("Events\\Visual Pinball")] - public sealed class PlayerStartedEventUnit : EventUnit + public sealed class GleStartedEventUnit : EventUnit { protected override bool register => true; public override EventHook GetHook(GraphReference reference) { - return new EventHook(VisualScriptingEventNames.PlayerStartedEvent); - } - - protected override void Definition() - { - base.Definition(); + return new EventHook(VisualScriptingEventNames.GleStartedEvent); } } } diff --git a/Runtime/Nodes/PlayerStartedEventUnit.cs.meta b/Runtime/Nodes/GleStartedEventUnit.cs.meta similarity index 83% rename from Runtime/Nodes/PlayerStartedEventUnit.cs.meta rename to Runtime/Nodes/GleStartedEventUnit.cs.meta index 829d2a4..c4ea3f6 100644 --- a/Runtime/Nodes/PlayerStartedEventUnit.cs.meta +++ b/Runtime/Nodes/GleStartedEventUnit.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 50220fa9400eef54fba36cf0cdabb035 +guid: 1978d4951d07c4cb2bf69173f0be745e MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Runtime/Nodes/GleUnit.cs b/Runtime/Nodes/GleUnit.cs index d59a870..e30770d 100644 --- a/Runtime/Nodes/GleUnit.cs +++ b/Runtime/Nodes/GleUnit.cs @@ -35,10 +35,10 @@ public abstract class GleUnit : Unit, IGleUnit [DoNotSerialize] protected Player Player; - + protected bool AssertGle(Flow flow) { - if (Gle != null) { + if (!UnityObjectUtility.IsUnityNull(Gle)) { return true; } Gle = flow.stack.gameObject.GetComponentInParent(); @@ -47,7 +47,7 @@ protected bool AssertGle(Flow flow) protected bool AssertPlayer(Flow flow) { - if (Player != null) { + if (!UnityObjectUtility.IsUnityNull(Player)) { return true; } Player = flow.stack.gameObject.GetComponentInParent(); diff --git a/package.json b/package.json index 33c333f..d10150b 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,10 @@ "Visual" ], "unity": "2021.2", - "unityRelease": "7f1", + "unityRelease": "8f1", "dependencies": { "com.unity.visualscripting": "1.7.6", - "org.visualpinball.engine.unity": "0.0.1-preview.84" + "org.visualpinball.engine.unity": "0.0.1-preview.90" }, "author": "freezy ", "contributors": [