Skip to content

Commit 036604f

Browse files
committed
Add ball creation unit.
1 parent 3dea641 commit 036604f

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// ReSharper disable UnusedType.Global
18+
19+
using Unity.VisualScripting;
20+
using VisualPinball.Unity.Editor;
21+
using IconSize = VisualPinball.Unity.Editor.IconSize;
22+
23+
namespace VisualPinball.Unity.VisualScripting.Editor
24+
{
25+
[Descriptor(typeof(CreateBallUnit))]
26+
public class CreateBallUnitDescriptor : UnitDescriptor<CreateBallUnit>
27+
{
28+
public CreateBallUnitDescriptor(CreateBallUnit target) : base(target)
29+
{
30+
}
31+
32+
protected override string DefinedSummary()
33+
{
34+
return "This node spawns a new ball at a given position.";
35+
}
36+
37+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.BallRoller(IconSize.Large, IconColor.Orange));
38+
39+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
40+
{
41+
base.DefinedPort(port, desc);
42+
43+
switch (port.key) {
44+
case nameof(CreateBallUnit.Position):
45+
desc.summary = "The position in playfield space where the ball should be created.";
46+
break;
47+
case nameof(CreateBallUnit.KickAngle):
48+
desc.summary = "The angle in degrees at which the ball is accelerated at the given position. 0 is straight up, then it goes clock-wise, 180 being straight down.";
49+
break;
50+
case nameof(CreateBallUnit.KickForce):
51+
desc.summary = "The force with which the ball is accelerated at the given position.";
52+
break;
53+
}
54+
}
55+
}
56+
}

Editor/Descriptors/CreateBallUnitDescriptor.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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine
5252
public event EventHandler<CoilEventArgs> OnCoilChanged;
5353
public event EventHandler<SwitchEventArgs2> OnSwitchChanged;
5454

55+
[NonSerialized] public BallManager BallManager;
5556
[NonSerialized] private Player _player;
5657

5758
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
5859
{
5960
_player = player;
61+
BallManager = ballManager;
6062
EventBus.Trigger(VisualScriptingEventNames.PlayerStartedEvent, EventArgs.Empty);
6163
}
6264

Runtime/Nodes/CreateBallUnit.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 Unity.VisualScripting;
18+
using UnityEngine;
19+
20+
namespace VisualPinball.Unity.VisualScripting
21+
{
22+
23+
[UnitTitle("Create Ball")]
24+
[UnitCategory("Events\\Visual Pinball")]
25+
public class CreateBallUnit : GleUnit
26+
{
27+
[DoNotSerialize]
28+
[PortLabelHidden]
29+
public ControlInput InputTrigger;
30+
31+
[DoNotSerialize]
32+
[PortLabelHidden]
33+
public ControlOutput OutputTrigger;
34+
35+
[DoNotSerialize]
36+
[PortLabel("Position")]
37+
public ValueInput Position { get; private set; }
38+
39+
[DoNotSerialize]
40+
[PortLabel("Kick Angle")]
41+
public ValueInput KickAngle { get; private set; }
42+
43+
[DoNotSerialize]
44+
[PortLabel("Kick Force")]
45+
public ValueInput KickForce { get; private set; }
46+
47+
protected override void Definition()
48+
{
49+
InputTrigger = ControlInput(nameof(InputTrigger), Process);
50+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
51+
52+
Position = ValueInput<Vector3>(nameof(Position), Vector3.zero);
53+
KickAngle = ValueInput<float>(nameof(KickAngle), 0);
54+
KickForce = ValueInput<float>(nameof(KickForce), 0);
55+
56+
Requirement(Position, InputTrigger);
57+
Succession(InputTrigger, OutputTrigger);
58+
}
59+
60+
private ControlOutput Process(Flow flow)
61+
{
62+
if (!AssertGle(flow)) {
63+
Debug.LogError("Cannot find GLE.");
64+
return OutputTrigger;
65+
}
66+
67+
if (Gle is VisualScriptingGamelogicEngine vsGle) {
68+
var pos = flow.GetValue<Vector3>(Position);
69+
var kickAngle = flow.GetValue<float>(KickAngle);
70+
var kickForce = flow.GetValue<float>(KickForce);
71+
vsGle.BallManager.CreateBall(new DebugBallCreator(pos.x, pos.y, pos.z, kickAngle, kickForce));
72+
}
73+
74+
return OutputTrigger;
75+
}
76+
}
77+
}

Runtime/Nodes/CreateBallUnit.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.

0 commit comments

Comments
 (0)