newlon/scripts/components/gui/ShovelButton.cs

48 lines
1.4 KiB
C#

using Godot;
using System;
public partial class ShovelButton : TextureButton
{
private RayCast2D _shovelCast;
public override void _Ready()
{
_shovelCast = GetNode<RayCast2D>("RayCast2D");
_shovelCast.CallDeferred("reparent", GetTree().CurrentScene);
}
private void OnFocusExited()
{
ButtonPressed = false;
}
public override void _Process(double delta)
{
_shovelCast.GlobalPosition = _shovelCast.GetGlobalMousePosition();
}
public override void _Toggled(bool toggledOn)
{
_shovelCast.Enabled = toggledOn;
Cursor.Instance.shovel = toggledOn;
Cursor.Instance.UpdateCursor();
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("primary_action"))
{
if (_shovelCast.IsColliding())
{
var checkedPosition = (_shovelCast.GetCollider() as Node).GetParent<RuntimePlantData>().GlobalPosition;
for (int i = Utility.LayersCount-1; i >= 0; i--)
{
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
{
plantData.Kill();
break;
}
}
}
ButtonPressed = false;
}
}
}