summaryrefslogtreecommitdiff
path: root/scripts/WeaponHolder.cs
blob: 210be601ce7e66748857de1dfcfe0a7258a9011e (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
36
37
38
39
40
41
42
43
using Godot;

public partial class WeaponHolder : Node2D
{
    [Export] public PackedScene[] WeaponScenes { get; set; } 
    
    private Weapon _currentWeapon;

    public override void _Ready()
    {
        // Equip the first weapon automatically
        EquipWeapon(0); 
    }

    public void EquipWeapon(int index)
    {
        if (WeaponScenes == null || WeaponScenes.Length <= index || WeaponScenes[index] == null) 
            return;

        if (_currentWeapon != null)
        {
            _currentWeapon.QueueFree();
        }

        _currentWeapon = WeaponScenes[index].Instantiate<Weapon>();
        AddChild(_currentWeapon);
        
        GD.Print($"WeaponHolder equipped weapon index: {index}");
    }

    public override void _UnhandledInput(InputEvent @event)
    {
        // Handle shooting
        if (@event.IsActionPressed("shoot") && _currentWeapon != null)
        {
            _currentWeapon.Fire(GetGlobalMousePosition());
        }

        // Handle swapping
        if (@event.IsActionPressed("weapon_1")) EquipWeapon(0);
        else if (@event.IsActionPressed("weapon_2")) EquipWeapon(1);
    }
}