Secondary Item switching

This commit is contained in:
Dustin 2025-04-08 23:06:07 -07:00
parent bf481c5eea
commit 78bcdb0d6f
2 changed files with 33 additions and 0 deletions

View File

@ -32,6 +32,10 @@ func _process(delta):
var _selected_item = player_inventory.switch_primary() var _selected_item = player_inventory.switch_primary()
if _selected_item: if _selected_item:
print("Switched Primary item to: ", _selected_item.name) print("Switched Primary item to: ", _selected_item.name)
elif Input.is_action_just_released("switch_secondary_item_" + str(player_number)):
var _selected_item = player_inventory.switch_secondary()
if _selected_item:
print("Switched Secondary item to: ", _selected_item.name)
func hit_Receiver(damage): func hit_Receiver(damage):
$Hurtbox_Component.set_hurtbox(false) $Hurtbox_Component.set_hurtbox(false)

View File

@ -54,6 +54,35 @@ func switch_primary() -> Item:
## Nothing changed ## Nothing changed
return null return null
func switch_secondary() -> Item:
## Don't switch unless there is another item
if secondary_selection != null and _items.size() > 1:
var found_item :bool = false
var itemarr = _items.keys()
for i in itemarr:
## Item found in last iteration, grab the next one.
if found_item:
if i == primary_selection:
break
secondary_selection = i
return i
if i == secondary_selection:
found_item = true
## Item found but was the last in the list, grab the first one.
if found_item and itemarr[0] != secondary_selection:
if itemarr[0] != primary_selection:
secondary_selection = itemarr[0]
return itemarr[0]
## Swap primary and secondary
if primary_selection != null:
var i = primary_selection
if secondary_selection != null:
primary_selection = secondary_selection
secondary_selection = i
return i
## Nothing changed
return null
func select_secondary(_item :Item) -> void: func select_secondary(_item :Item) -> void:
if _items.has(_item): if _items.has(_item):
secondary_selection = _item secondary_selection = _item