Documentation

This commit is contained in:
Rendo 2025-10-12 16:41:34 +05:00
commit 23b6e5d180
5 changed files with 34 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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