Skip to content

Commit 8a03a34

Browse files
committed
Fix switch event unit.
1 parent b9da183 commit 8a03a34

File tree

8 files changed

+112
-21
lines changed

8 files changed

+112
-21
lines changed

Editor/GleUnitAnalyzer.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
using System.Collections.Generic;
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.Collections.Generic;
218
using Unity.VisualScripting;
3-
using VisualPinball.Unity;
419
using VisualPinball.Unity.VisualScripting;
520

621
namespace Editor
722
{
823

924
[Analyser(typeof(SwitchEventUnit))]
10-
public class SwitchEventUnitAnalyzer : GleUnitAnalyser<SwitchEventArgs>
25+
public class SwitchEventUnitAnalyzer : GleUnitAnalyser<VisualScriptingScriptEvent>
1126
{
1227
public SwitchEventUnitAnalyzer(GraphReference reference, SwitchEventUnit target) : base(reference, target)
1328
{

Editor/Widgets/SetCoilUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SetCoilUnitWidget(FlowCanvas canvas, SetCoilUnit unit) : base(canvas, uni
3434

3535
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3636
{
37-
if (port == unit.id) {
37+
if (port == unit.Id) {
3838
InspectorProvider.instance.Renew(ref _coilIdInspector, meta, _setCoilInspectorConstructor);
3939
return _coilIdInspector;
4040
}

Editor/Widgets/SwitchEventUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SwitchEventUnitWidget(FlowCanvas canvas, SwitchEventUnit unit) : base(can
3434

3535
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3636
{
37-
if (port == unit.id) {
37+
if (port == unit.Id) {
3838
InspectorProvider.instance.Renew(ref _lampIdInspector, meta, _switchIdInspectorConstructor);
3939

4040
return _lampIdInspector;

Runtime/Events/Nodes/SetCoilUnit.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using Unity.VisualScripting;
18+
using UnityEngine;
1819

1920
namespace VisualPinball.Unity.VisualScripting
2021
{
@@ -24,27 +25,49 @@ public class SetCoilUnit : GleUnit
2425
{
2526
[DoNotSerialize]
2627
[PortLabelHidden]
27-
public ControlInput inputTrigger;
28+
public ControlInput InputTrigger;
2829

2930
[DoNotSerialize]
3031
[PortLabelHidden]
31-
public ControlOutput outputTrigger;
32+
public ControlOutput OutputTrigger;
3233

3334
[DoNotSerialize]
3435
[PortLabel("Coil ID")]
35-
public ValueInput id { get; private set; }
36+
public ValueInput Id { get; private set; }
3637

3738
[DoNotSerialize]
3839
[PortLabel("Value")]
39-
public ValueInput enabled { get; private set; }
40+
public ValueInput IsEnabled { get; private set; }
4041

4142
protected override void Definition()
4243
{
43-
inputTrigger = ControlInput(nameof(inputTrigger), _ => outputTrigger);
44-
outputTrigger = ControlOutput(nameof(outputTrigger));
44+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
45+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
4546

46-
id = ValueInput<string>(nameof(id), string.Empty);
47-
enabled = ValueInput<bool>(nameof(enabled), false);
47+
Id = ValueInput<string>(nameof(Id), string.Empty);
48+
IsEnabled = ValueInput<bool>(nameof(IsEnabled), false);
49+
50+
Requirement(Id, InputTrigger);
51+
Succession(InputTrigger, OutputTrigger);
52+
}
53+
private ControlOutput Process(Flow flow)
54+
{
55+
var gle = flow.stack.gameObject.GetComponentInParent<VisualScriptingGamelogicEngine>();
56+
57+
if (gle != null) {
58+
59+
var id = flow.GetValue<string>(Id);
60+
var isEnabled = flow.GetValue<bool>(IsEnabled);
61+
62+
gle.SetCoil(id, isEnabled);
63+
64+
} else {
65+
Debug.LogError("Cannot find GLE.");
66+
}
67+
68+
return OutputTrigger;
4869
}
70+
71+
4972
}
5073
}

Runtime/Events/Nodes/SwitchEventUnit.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ namespace VisualPinball.Unity.VisualScripting
2020
{
2121
[UnitTitle("On Switch Changed")]
2222
[UnitCategory("Events\\Visual Pinball")]
23-
public class SwitchEventUnit : GleEventUnit<SwitchEventArgs>
23+
public class SwitchEventUnit : GleEventUnit<VisualScriptingScriptEvent>
2424
{
2525
[DoNotSerialize]
2626
[PortLabel("Switch ID")]
27-
public ValueInput id { get; private set; }
27+
public ValueInput Id { get; private set; }
2828

2929
[DoNotSerialize]
3030
[PortLabel("Is Enabled")]
31-
public ValueOutput enabled { get; private set; }
31+
public ValueOutput IsEnabled { get; private set; }
3232

3333
protected override bool register => true;
3434

@@ -42,13 +42,18 @@ protected override void Definition()
4242
{
4343
base.Definition();
4444

45-
id = ValueInput(nameof(id), string.Empty);
46-
enabled = ValueOutput<bool>(nameof(enabled));
45+
Id = ValueInput(nameof(Id), string.Empty);
46+
IsEnabled = ValueOutput<bool>(nameof(IsEnabled));
4747
}
4848

49-
protected override void AssignArguments(Flow flow, SwitchEventArgs args)
49+
protected override bool ShouldTrigger(Flow flow, VisualScriptingScriptEvent args)
5050
{
51-
flow.SetValue(enabled, args.IsEnabled);
51+
return flow.GetValue<string>(Id) == args.Id;
52+
}
53+
54+
protected override void AssignArguments(Flow flow, VisualScriptingScriptEvent args)
55+
{
56+
flow.SetValue(IsEnabled, args.IsEnabled);
5257
}
5358
}
5459
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.VisualScripting
18+
{
19+
public readonly struct VisualScriptingScriptEvent
20+
{
21+
public readonly string Id;
22+
public readonly bool IsEnabled;
23+
24+
public VisualScriptingScriptEvent(string id, bool isEnabled)
25+
{
26+
Id = id;
27+
IsEnabled = isEnabled;
28+
}
29+
}
30+
}

Runtime/Gamelogic/VisualScriptingEvents.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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using System;
18+
using System.Xml;
19+
using Unity.VisualScripting;
1820
using UnityEngine;
1921
using VisualPinball.Engine.Game.Engines;
2022

@@ -49,9 +51,14 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
4951
{
5052
}
5153

52-
public void Switch(string id, bool isClosed)
54+
public void SetCoil(string id, bool isEnabled)
5355
{
56+
OnCoilChanged?.Invoke(this, new CoilEventArgs(id, isEnabled));
57+
}
5458

59+
public void Switch(string id, bool isClosed)
60+
{
61+
EventBus.Trigger(EventNames.SwitchEvent, new VisualScriptingScriptEvent(id, isClosed));
5562
}
5663
}
5764
}

0 commit comments

Comments
 (0)