2023-11-08 14:28:55 +11:00
|
|
|
extends Node
|
|
|
|
class_name Inventory
|
|
|
|
|
|
|
|
signal item_added(item)
|
|
|
|
signal item_removed(item)
|
|
|
|
|
|
|
|
@export var max_size := 0
|
2023-12-08 03:05:11 +11:00
|
|
|
@export var contents : Array[Item] = []
|
2023-11-08 14:28:55 +11:00
|
|
|
var selected_index := 0
|
2023-12-08 03:05:11 +11:00
|
|
|
var selected_item : Item :
|
2023-11-08 14:28:55 +11:00
|
|
|
get:
|
2023-11-13 19:36:35 +11:00
|
|
|
return contents[selected_index] if contents.size() > 0 else null
|
2023-11-08 14:28:55 +11:00
|
|
|
set(_value):
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2023-12-08 03:05:11 +11:00
|
|
|
func add(item : Item) -> bool:
|
|
|
|
if item != null and contents.size() < max_size or max_size == 0:
|
|
|
|
contents.append(item)
|
|
|
|
item_added.emit(item)
|
|
|
|
networked_add.rpc(Data.cards.find(item))
|
2023-11-08 14:28:55 +11:00
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
2023-12-08 03:05:11 +11:00
|
|
|
func remove_at(index : int) -> Item:
|
2023-11-08 14:28:55 +11:00
|
|
|
if contents.size() <= 0:
|
|
|
|
return null
|
2023-12-08 03:05:11 +11:00
|
|
|
var item = contents[index]
|
2023-11-08 14:28:55 +11:00
|
|
|
contents.remove_at(index)
|
|
|
|
if selected_index >= contents.size() and selected_index > 0:
|
|
|
|
selected_index -= 1
|
2023-12-08 03:05:11 +11:00
|
|
|
item_removed.emit(item)
|
2023-11-08 14:28:55 +11:00
|
|
|
networked_remove_at.rpc(index)
|
2023-12-08 03:05:11 +11:00
|
|
|
return item
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
2023-12-08 03:05:11 +11:00
|
|
|
func remove() -> Item:
|
2023-11-08 14:28:55 +11:00
|
|
|
return remove_at(selected_index)
|
|
|
|
|
|
|
|
|
|
|
|
func increment_selected():
|
|
|
|
if contents.size() > 0:
|
|
|
|
selected_index += 1
|
|
|
|
if selected_index >= contents.size():
|
|
|
|
selected_index = 0
|
2023-11-13 19:36:35 +11:00
|
|
|
networked_set_selected.rpc(selected_index)
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
|
|
|
func decrement_selected():
|
|
|
|
if contents.size() > 0:
|
|
|
|
selected_index -= 1
|
|
|
|
if selected_index < 0:
|
|
|
|
selected_index = contents.size() - 1
|
2023-11-13 19:36:35 +11:00
|
|
|
networked_set_selected.rpc(selected_index)
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
2023-11-13 19:36:35 +11:00
|
|
|
@rpc("reliable", "any_peer")
|
2023-11-08 14:28:55 +11:00
|
|
|
func networked_add(value):
|
|
|
|
contents.append(Data.cards[value])
|
|
|
|
item_added.emit(Data.cards[value])
|
|
|
|
|
|
|
|
|
2023-11-13 19:36:35 +11:00
|
|
|
@rpc("reliable", "any_peer")
|
2023-11-08 14:28:55 +11:00
|
|
|
func networked_remove_at(value):
|
|
|
|
var item = contents[value]
|
|
|
|
contents.remove_at(value)
|
|
|
|
item_removed.emit(item)
|
2023-11-13 19:36:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
@rpc("reliable", "any_peer")
|
|
|
|
func networked_set_selected(value):
|
|
|
|
selected_index = value
|