Basic inventory, item and storage
This commit is contained in:
parent
b0b659acd4
commit
66a623e568
11 changed files with 155 additions and 1 deletions
59
scripts/inventory/inventory_slot.gd
Normal file
59
scripts/inventory/inventory_slot.gd
Normal file
|
@ -0,0 +1,59 @@
|
|||
@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)
|
Loading…
Add table
Add a link
Reference in a new issue