delurbelako/scripts/inventory/inventory_slot.gd

59 lines
1.2 KiB
GDScript

@tool
extends RefCounted
class_name InventorySlot
@export_storage var held_item : Item:
set(value):
if held_item == null and value != null:
held_item = value
amount += 1
return
held_item = value
get:
return held_item
@export_storage var amount : int:
set(value):
if value <= 0:
held_item = null
amount = 0
return
if held_item == null:
return
if value > held_item.stack_size:
amount = held_item.stack_size
return
amount = value
get:
return amount
func extract_stack(extract_amount: int) -> Stack:
var return_stack : Stack
if extract_amount > amount:
return_stack = Stack.new(held_item,amount)
else:
return_stack = Stack.new(held_item,extract_amount)
self.amount = 0
return return_stack
func extract() -> Stack:
var return_stack : Stack = Stack.new(held_item,amount)
self.amount = 0
return return_stack
func merge_stack(stack: Stack) -> Stack:
if held_item == null:
held_item = stack.held_item
amount = stack.amount
return null
if stack.held_item != held_item:
return null
var return_stack_amount = max(0,stack.amount-amount)
amount += stack.amount - return_stack_amount
if return_stack_amount == 0:
return null
return Stack.new(held_item,return_stack_amount)