switching items with controller

controller-support
HumanoidSandvichDispenser 2023-08-15 00:44:44 -07:00
parent ccac1c03da
commit 7a0e8e29a3
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
3 changed files with 42 additions and 0 deletions

View File

@ -27,6 +27,20 @@ public partial class Inventory : Node2D
set => EquipItem(value, ref _selectedItem); set => EquipItem(value, ref _selectedItem);
} }
private int _quickSwitchIndex = -1;
public const int QUICKSWITCH_SIZE = 3;
public int CurrentQuickSwitchIndex
{
get => _quickSwitchIndex;
set
{
const int size = QUICKSWITCH_SIZE;
_quickSwitchIndex = (value % size + size) % size;
}
}
public bool IsUsingItem => SelectedItem?.IsUsing ?? false; public bool IsUsingItem => SelectedItem?.IsUsing ?? false;
public Inventory() public Inventory()
@ -57,6 +71,16 @@ public partial class Inventory : Node2D
base._Ready(); base._Ready();
} }
public bool EquipIndex(int index)
{
if (index < Items.Count)
{
return EquipItem(Items[index], ref _selectedItem);
}
return EquipItem(null, ref _selectedItem);
}
private bool EquipItem(Item item, ref Item slot) private bool EquipItem(Item item, ref Item slot)
{ {
if (item is not null) if (item is not null)

View File

@ -30,6 +30,14 @@ public abstract partial class PlayerState : CharacterState
{ {
inventory.SelectedItem = inventory.GetItemByMap("equip_3"); inventory.SelectedItem = inventory.GetItemByMap("equip_3");
} }
else if (@event.IsActionPressed("next_item"))
{
inventory.EquipIndex(++inventory.CurrentQuickSwitchIndex);
}
else if (@event.IsActionPressed("prev_item"))
{
inventory.EquipIndex(--inventory.CurrentQuickSwitchIndex);
}
if (@event.IsActionPressed("interact")) if (@event.IsActionPressed("interact"))
{ {

View File

@ -172,6 +172,16 @@ look_right={
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null)
] ]
} }
next_item={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":5,"pressure":0.0,"pressed":false,"script":null)
]
}
prev_item={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":4,"pressure":0.0,"pressed":false,"script":null)
]
}
[internationalization] [internationalization]