From 23b6e5d180114898e214acb3eba87e5ba66f2812 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 12 Oct 2025 16:41:34 +0500 Subject: [PATCH] Documentation --- scripts/inventory/inventory.gd | 6 ++++++ scripts/inventory/inventory_slot.gd | 9 +++++++++ scripts/inventory/stack.gd | 4 ++++ scripts/inventory/storage.gd | 8 ++++++++ scripts/item.gd | 7 +++++++ 5 files changed, 34 insertions(+) diff --git a/scripts/inventory/inventory.gd b/scripts/inventory/inventory.gd index c98a3b4..2da7131 100644 --- a/scripts/inventory/inventory.gd +++ b/scripts/inventory/inventory.gd @@ -1,22 +1,28 @@ @abstract extends Resource +## Class that can hold items in it class_name Inventory func _init() -> void: resource_local_to_scene = true +## Tries to add an item into inventory. Returns not stored stack of item. @abstract func add(item : Stack) -> Stack +## Tries to take first item. Returns null if no items in inventory @abstract func pop() -> Stack +## Tries to take certain item from inventory. Returns null if no item found @abstract func take(item : Item,amount: int) -> Stack +## Finds first entry of item. Returns -1 if no item found @abstract func find(item : Item) -> int +## Does inventory have certain item? func has(item : Item) -> bool: return find(item) != -1 diff --git a/scripts/inventory/inventory_slot.gd b/scripts/inventory/inventory_slot.gd index a30cd1f..ad4235e 100644 --- a/scripts/inventory/inventory_slot.gd +++ b/scripts/inventory/inventory_slot.gd @@ -28,7 +28,11 @@ class_name InventorySlot 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) @@ -39,12 +43,17 @@ func extract_stack(extract_amount: int) -> Stack: 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 diff --git a/scripts/inventory/stack.gd b/scripts/inventory/stack.gd index 991e853..062425f 100644 --- a/scripts/inventory/stack.gd +++ b/scripts/inventory/stack.gd @@ -1,11 +1,15 @@ extends RefCounted +## Structure of item and amount of it. Basic object that more complex exchange + class_name Stack func _init(item: Item = null, amount: int = 0) -> void: held_item = item self.amount = amount +## Item in stack @export_storage var held_item : Item +## Amount of items in stack @export_storage var amount : int diff --git a/scripts/inventory/storage.gd b/scripts/inventory/storage.gd index dafe70e..ec2d666 100644 --- a/scripts/inventory/storage.gd +++ b/scripts/inventory/storage.gd @@ -1,8 +1,11 @@ @tool extends Inventory +## Base class for simple storages with no differentiation. + class_name Storage +## Amount of stacks that can be held in storage @export var capacity : int: set(value): if value < 0: @@ -16,14 +19,17 @@ class_name Storage get: return capacity +## :3 @export_storage var internal_array : Array[InventorySlot] = [] +## Finds first entry of item. Returns -1 if no item found func find(item : Item) -> int: for i in range(len(internal_array)): if internal_array[i].held_item == item: return i return -1 +## Tries to add an item into inventory. Returns not stored stack of item. func add(stack: Stack) -> Stack: var found_index : int = find(stack.held_item) if found_index != -1: @@ -37,12 +43,14 @@ func add(stack: Stack) -> Stack: return null return stack +## Tries to take first item. Returns null if no items in inventory func pop() -> Stack: for i in range(len(internal_array)): if internal_array[i].held_item != null: return internal_array[i].extract() return null +## Tries to take certain item from inventory. Returns null if no item found func take(item: Item,amount: int) -> Stack: var found = find(item) if found == -1: diff --git a/scripts/item.gd b/scripts/item.gd index 9d20b7a..405f691 100644 --- a/scripts/item.gd +++ b/scripts/item.gd @@ -1,7 +1,14 @@ extends Resource +## Base unit of manipulated objects + class_name Item +## Name of item that is displayed @export var display_name : StringName + +## Preview in UI @export var preview : Texture2D + +## Maximum amount of items that can be held in inventory @export var stack_size : int