Stuff
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Slimepire.Gameplay.Nodules;
|
||||
|
||||
namespace Slimepire.Gameplay;
|
||||
|
||||
@@ -7,18 +8,64 @@ public partial class BuildGhost : Node2D
|
||||
{
|
||||
public event Action<Vector2>? OnBuild;
|
||||
private Tube? _tube;
|
||||
private readonly Dictionary<Nodule, Tube> _connectedTubes = [];
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_tube = Instantiator.Instantiate<Tube>();
|
||||
_tube.Position = Position / 2; // TODO WHYYY????
|
||||
GetParent()?.AddChild(_tube);
|
||||
TubeConnectionArea.AreaEntered += TubeConnectionAreaOnAreaEntered;
|
||||
TubeConnectionArea.AreaExited += TubeConnectionAreaOnAreaExited;
|
||||
}
|
||||
|
||||
private void TubeConnectionAreaOnAreaEntered(Area2D area)
|
||||
{
|
||||
if (area.GetParent() is Nodule nodule)
|
||||
{
|
||||
CreateGhostTube(nodule, area.GlobalPosition);
|
||||
ConnectTubePlayer.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void TubeConnectionAreaOnAreaExited(Area2D area)
|
||||
{
|
||||
if (area.GetParent() is Nodule nodule)
|
||||
{
|
||||
var tube = _connectedTubes.GetValueOrDefault(nodule);
|
||||
if (tube == null)
|
||||
return;
|
||||
|
||||
_connectedTubes.Remove(nodule);
|
||||
tube.QueueFree();
|
||||
DisconnectTubePlayer.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateGhostTube(Nodule connectedNodule, Vector2 pos)
|
||||
{
|
||||
var tube = Instantiator.Instantiate<Tube>();
|
||||
tube.Position = pos / 2; // TODO WHYYY????
|
||||
_connectedTubes.Add(connectedNodule, tube);
|
||||
GetParent()?.AddChild(tube);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
GlobalPosition = GetGlobalMousePosition();
|
||||
_tube?.SetTarget(GlobalPosition);
|
||||
foreach (var (_, tube) in _connectedTubes)
|
||||
{
|
||||
tube.SetTarget(GlobalPosition);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _UnhandledKeyInput(InputEvent @event)
|
||||
{
|
||||
using (@event)
|
||||
{
|
||||
if (@event is InputEventKey { Keycode: Key.Escape, Pressed: true })
|
||||
{
|
||||
NoduleManager.Instance.Building = false;
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
@@ -27,9 +74,20 @@ public partial class BuildGhost : Node2D
|
||||
{
|
||||
if (@event is InputEventMouseButton { Pressed: false, ButtonIndex: MouseButton.Left })
|
||||
{
|
||||
NoduleManager.Instance.Building = false;
|
||||
OnBuild?.Invoke(GlobalPosition);
|
||||
|
||||
_tube?.QueueFree();
|
||||
var nodule = Instantiator.Instantiate<Nodule>();
|
||||
|
||||
nodule.Position = Position;
|
||||
GetParent()?.AddChild(nodule);
|
||||
|
||||
foreach (var (_, tube) in _connectedTubes)
|
||||
{
|
||||
tube.QueueFree();
|
||||
}
|
||||
|
||||
_connectedTubes.Clear();
|
||||
QueueFree();
|
||||
|
||||
GetViewport().SetInputAsHandled();
|
||||
|
||||
Reference in New Issue
Block a user