68 lines
1.5 KiB
GDScript
68 lines
1.5 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
|
|
|
|
## Get some amount of items from slot. Returns null if slot is empty
|
|
func extract_stack(extract_amount: int) -> Stack:
|
|
if amount == 0:
|
|
return null
|
|
|
|
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
|
|
|
|
## Extract entire stack from slot. Returns null if slot is empty
|
|
func extract() -> Stack:
|
|
if amount == 0:
|
|
return null
|
|
|
|
var return_stack : Stack = Stack.new(held_item,amount)
|
|
self.amount = 0
|
|
|
|
return return_stack
|
|
|
|
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
|
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)
|