95 lines
1.9 KiB
GDScript
95 lines
1.9 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
|
|
|
|
@export_storage var filter : Comparable
|
|
|
|
## 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
|
|
|
|
func as_stack() -> Stack:
|
|
if amount == 0:
|
|
return null
|
|
|
|
var return_stack : Stack = Stack.new(held_item,amount)
|
|
|
|
return return_stack
|
|
|
|
func can_be_merged(item : Item) -> bool:
|
|
if filter != null:
|
|
return filter.is_equal(item)
|
|
if amount == 0:
|
|
return true
|
|
return item == held_item
|
|
|
|
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
|
func merge_stack(stack: Stack) -> bool:
|
|
if stack == null or stack.is_valid() == false:
|
|
return false
|
|
|
|
if filter and filter.is_equal(stack.held_item) == false:
|
|
return false
|
|
|
|
if held_item == null:
|
|
held_item = stack.held_item
|
|
amount = stack.amount
|
|
stack.invalidate()
|
|
return true
|
|
|
|
var delta_amount = stack.amount - (held_item.stack_size - amount)
|
|
amount += stack.amount
|
|
|
|
if delta_amount <= 0:
|
|
stack.invalidate()
|
|
return true
|
|
|
|
stack.amount = delta_amount
|
|
return true
|