Skip to content

Commit 30bdd84

Browse files
committed
Remove dependency from VPE
1 parent 68276b0 commit 30bdd84

8 files changed

+137
-4
lines changed

Runtime/Events/Nodes/LampEventUnit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public sealed class LampEventUnit : EventUnit<LampEventArgs>
3838

3939
public override EventHook GetHook(GraphReference reference)
4040
{
41-
return new EventHook(EventNames.LampEvent);
41+
return new EventHook(VisualScriptingEventNames.LampEvent);
4242
}
4343

4444
protected override void Definition()

Runtime/Events/Nodes/PlayerStartedEventUnit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public sealed class PlayerStartedEventUnit : EventUnit<EventArgs>
2727

2828
public override EventHook GetHook(GraphReference reference)
2929
{
30-
return new EventHook(EventNames.PlayerStartedEvent);
30+
return new EventHook(VisualScriptingEventNames.PlayerStartedEvent);
3131
}
3232

3333
protected override void Definition()

Runtime/Events/Nodes/SwitchEventUnit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SwitchEventUnit : GleEventUnit<SwitchEventArgs2>
3535
// Adding an EventHook with the name of the event to the list of visual scripting events.
3636
public override EventHook GetHook(GraphReference reference)
3737
{
38-
return new EventHook(EventNames.SwitchEvent);
38+
return new EventHook(VisualScriptingEventNames.SwitchEvent);
3939
}
4040

4141
protected override void Definition()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
namespace VisualPinball.Unity
18+
{
19+
public static class VisualScriptingEventNames
20+
{
21+
public const string PlayerStartedEvent = "PlayerStartedEvent";
22+
public const string LampEvent = "LampEvent";
23+
public const string SwitchEvent = "SwitchEvent";
24+
public const string CoilEvent = "CoilEvent";
25+
}
26+
}

Runtime/Gamelogic/VisualScriptingEventNames.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

Runtime/Gamelogic/VisualScriptingGamelogicBridge.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
5454

5555
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
5656
{
57+
EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty);
5758
}
5859

5960
public void Switch(string id, bool isClosed)
6061
{
6162
OnSwitchChanged?.Invoke(this, new SwitchEventArgs2(id, isClosed));
62-
EventBus.Trigger(EventNames.SwitchEvent, new SwitchEventArgs2(id, isClosed));
63+
EventBus.Trigger(VisualScriptingEventNames.SwitchEvent, new SwitchEventArgs2(id, isClosed));
6364
}
6465

6566
public void SetCoil(string id, bool isEnabled)

0 commit comments

Comments
 (0)