Skip to content

Add new GLE OnStarted event handler. Remove OnPlayerStartedEvent. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Runtime/Gamelogic/VisualScriptingEventNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
61 changes: 33 additions & 28 deletions Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGamelogicEngine>();
_init = false;

_player = GetComponent<Player>();
if (_gle == null) {
Debug.LogWarning("Cannot find gamelogic engine.");
return;
}
if (_player == null) {
Debug.LogWarning("Cannot find player.");
return;
}

_player.OnPlayerStarted += OnPlayerStarted;
_gle = GetComponent<IGamelogicEngine>();
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);
}
}

Expand All @@ -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);
}

}
}
6 changes: 4 additions & 2 deletions Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -51,6 +51,7 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
public event EventHandler<LampColorEventArgs> OnLampColorChanged;
public event EventHandler<CoilEventArgs> OnCoilChanged;
public event EventHandler<SwitchEventArgs2> OnSwitchChanged;
public event EventHandler<EventArgs> OnStarted;

[NonSerialized] public BallManager BallManager;
[NonSerialized] private Player _player;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventArgs>
public sealed class GleStartedEventUnit : EventUnit<EventArgs>
{
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Runtime/Nodes/GleUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGamelogicEngine>();
Expand All @@ -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<Player>();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"contributors": [
Expand Down