Basic inventory, item and storage

This commit is contained in:
Rendo 2025-10-12 16:28:44 +05:00
commit 66a623e568
11 changed files with 155 additions and 1 deletions

View file

@ -0,0 +1,22 @@
@abstract
extends Resource
class_name Inventory
func _init() -> void:
resource_local_to_scene = true
@abstract
func add(item : Stack) -> Stack
@abstract
func pop() -> Stack
@abstract
func take(item : Item,amount: int) -> Stack
@abstract
func find(item : Item) -> int
func has(item : Item) -> bool:
return find(item) != -1

View file

@ -0,0 +1 @@
uid://01aqkh38trcr

View 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)

View file

@ -0,0 +1 @@
uid://bd4ojfqrl8idm

View file

@ -0,0 +1,11 @@
extends RefCounted
class_name Stack
func _init(item: Item = null, amount: int = 0) -> void:
held_item = item
self.amount = amount
@export_storage var held_item : Item
@export_storage var amount : int

View file

@ -0,0 +1 @@
uid://h78lmbi3vbfe

View file

@ -0,0 +1,50 @@
@tool
extends Inventory
class_name Storage
@export var capacity : int:
set(value):
if value < 0:
return
if value == capacity:
return
capacity = value
internal_array.resize(capacity)
for i in range(capacity):
internal_array[i] = InventorySlot.new()
get:
return capacity
@export_storage var internal_array : Array[InventorySlot] = []
func find(item : Item) -> int:
for i in range(len(internal_array)):
if internal_array[i].held_item == item:
return i
return -1
func add(stack: Stack) -> Stack:
var found_index : int = find(stack.held_item)
if found_index != -1:
stack = internal_array[found_index].merge_stack(stack)
if stack == null:
return null
for i in range(len(internal_array)):
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
stack = internal_array[i].merge_stack(stack)
if stack == null:
return null
return stack
func pop() -> Stack:
for i in range(len(internal_array)):
if internal_array[i].held_item != null:
return internal_array[i].extract()
return null
func take(item: Item,amount: int) -> Stack:
var found = find(item)
if found == -1:
return null
return internal_array[found].extract_stack(amount)

View file

@ -0,0 +1 @@
uid://1scdy7mttx5h

7
scripts/item.gd Normal file
View file

@ -0,0 +1,7 @@
extends Resource
class_name Item
@export var display_name : StringName
@export var preview : Texture2D
@export var stack_size : int

1
scripts/item.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://p5327ibxtyfs