Optimised and improved shovel
This commit is contained in:
parent
9197d0a939
commit
cd2013e4d8
2 changed files with 9 additions and 31 deletions
|
|
@ -19,12 +19,4 @@ texture_pressed = ExtResource("2_pw2pj")
|
|||
stretch_mode = 0
|
||||
script = ExtResource("3_u6gir")
|
||||
|
||||
[node name="RayCast2D" type="RayCast2D" parent="."]
|
||||
enabled = false
|
||||
target_position = Vector2(0, 0)
|
||||
collision_mask = 2
|
||||
hit_from_inside = true
|
||||
collide_with_areas = true
|
||||
collide_with_bodies = false
|
||||
|
||||
[connection signal="focus_exited" from="." to="." method="OnFocusExited"]
|
||||
|
|
|
|||
|
|
@ -6,43 +6,29 @@ namespace Newlon.Components.GUI;
|
|||
|
||||
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 (@event.IsActionPressed("primary_action") && ButtonPressed)
|
||||
{
|
||||
var checkedPosition = (PoolContainer.Instance.Plants.GetGlobalMousePosition() / Utility.Tile).Ceil() * Utility.Tile - new Vector2(20, 14);
|
||||
|
||||
for (int i = Utility.LayersCount-1; i >= 0; i--)
|
||||
{
|
||||
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
|
||||
{
|
||||
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
|
||||
{
|
||||
plantData.Kill();
|
||||
break;
|
||||
}
|
||||
plantData.Kill();
|
||||
break;
|
||||
}
|
||||
}
|
||||
ButtonPressed = false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue