summaryrefslogtreecommitdiff
path: root/scripts/items/ItemPickup.cs
blob: df5d997d0cef6ed8f937b9d7e5cd8e7f9073a814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Godot;

public partial class ItemPickup : Area2D
{
    [Export] public ItemDefinition? Item { get; set; }

    public override void _Ready()
    {
        BodyEntered += OnBodyEntered;
    }

    private void OnBodyEntered(Node2D body)
    {
        if (!body.IsInGroup("player") || Item == null)
        {
            return;
        }

        PlayerBuildComponent? build = body.GetNodeOrNull<PlayerBuildComponent>("PlayerBuildComponent");
        if (build == null)
        {
            GD.PrintErr("[ItemPickup] PlayerBuildComponent missing on player.");
            return;
        }

        bool applied = Item.Type == ItemDefinition.ItemType.Passive
            ? build.AddPassiveItem(Item)
            : build.EquipWeaponItem(Item);

        if (applied)
        {
            QueueFree();
        }
    }
}