Documentation
This commit is contained in:
parent
66a623e568
commit
23b6e5d180
5 changed files with 34 additions and 0 deletions
|
@ -1,22 +1,28 @@
|
||||||
@abstract
|
@abstract
|
||||||
extends Resource
|
extends Resource
|
||||||
|
|
||||||
|
## Class that can hold items in it
|
||||||
class_name Inventory
|
class_name Inventory
|
||||||
|
|
||||||
func _init() -> void:
|
func _init() -> void:
|
||||||
resource_local_to_scene = true
|
resource_local_to_scene = true
|
||||||
|
|
||||||
|
## Tries to add an item into inventory. Returns not stored stack of item.
|
||||||
@abstract
|
@abstract
|
||||||
func add(item : Stack) -> Stack
|
func add(item : Stack) -> Stack
|
||||||
|
|
||||||
|
## Tries to take first item. Returns null if no items in inventory
|
||||||
@abstract
|
@abstract
|
||||||
func pop() -> Stack
|
func pop() -> Stack
|
||||||
|
|
||||||
|
## Tries to take certain item from inventory. Returns null if no item found
|
||||||
@abstract
|
@abstract
|
||||||
func take(item : Item,amount: int) -> Stack
|
func take(item : Item,amount: int) -> Stack
|
||||||
|
|
||||||
|
## Finds first entry of item. Returns -1 if no item found
|
||||||
@abstract
|
@abstract
|
||||||
func find(item : Item) -> int
|
func find(item : Item) -> int
|
||||||
|
|
||||||
|
## Does inventory have certain item?
|
||||||
func has(item : Item) -> bool:
|
func has(item : Item) -> bool:
|
||||||
return find(item) != -1
|
return find(item) != -1
|
||||||
|
|
|
@ -28,7 +28,11 @@ class_name InventorySlot
|
||||||
get:
|
get:
|
||||||
return amount
|
return amount
|
||||||
|
|
||||||
|
## Get some amount of items from slot. Returns null if slot is empty
|
||||||
func extract_stack(extract_amount: int) -> Stack:
|
func extract_stack(extract_amount: int) -> Stack:
|
||||||
|
if amount == 0:
|
||||||
|
return null
|
||||||
|
|
||||||
var return_stack : Stack
|
var return_stack : Stack
|
||||||
if extract_amount > amount:
|
if extract_amount > amount:
|
||||||
return_stack = Stack.new(held_item,amount)
|
return_stack = Stack.new(held_item,amount)
|
||||||
|
@ -39,12 +43,17 @@ func extract_stack(extract_amount: int) -> Stack:
|
||||||
|
|
||||||
return return_stack
|
return return_stack
|
||||||
|
|
||||||
|
## Extract entire stack from slot. Returns null if slot is empty
|
||||||
func extract() -> Stack:
|
func extract() -> Stack:
|
||||||
|
if amount == 0:
|
||||||
|
return null
|
||||||
|
|
||||||
var return_stack : Stack = Stack.new(held_item,amount)
|
var return_stack : Stack = Stack.new(held_item,amount)
|
||||||
self.amount = 0
|
self.amount = 0
|
||||||
|
|
||||||
return return_stack
|
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:
|
func merge_stack(stack: Stack) -> Stack:
|
||||||
if held_item == null:
|
if held_item == null:
|
||||||
held_item = stack.held_item
|
held_item = stack.held_item
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
extends RefCounted
|
extends RefCounted
|
||||||
|
|
||||||
|
## Structure of item and amount of it. Basic object that more complex exchange
|
||||||
|
|
||||||
class_name Stack
|
class_name Stack
|
||||||
|
|
||||||
func _init(item: Item = null, amount: int = 0) -> void:
|
func _init(item: Item = null, amount: int = 0) -> void:
|
||||||
held_item = item
|
held_item = item
|
||||||
self.amount = amount
|
self.amount = amount
|
||||||
|
|
||||||
|
## Item in stack
|
||||||
@export_storage var held_item : Item
|
@export_storage var held_item : Item
|
||||||
|
|
||||||
|
## Amount of items in stack
|
||||||
@export_storage var amount : int
|
@export_storage var amount : int
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
@tool
|
@tool
|
||||||
extends Inventory
|
extends Inventory
|
||||||
|
|
||||||
|
## Base class for simple storages with no differentiation.
|
||||||
|
|
||||||
class_name Storage
|
class_name Storage
|
||||||
|
|
||||||
|
## Amount of stacks that can be held in storage
|
||||||
@export var capacity : int:
|
@export var capacity : int:
|
||||||
set(value):
|
set(value):
|
||||||
if value < 0:
|
if value < 0:
|
||||||
|
@ -16,14 +19,17 @@ class_name Storage
|
||||||
get:
|
get:
|
||||||
return capacity
|
return capacity
|
||||||
|
|
||||||
|
## :3
|
||||||
@export_storage var internal_array : Array[InventorySlot] = []
|
@export_storage var internal_array : Array[InventorySlot] = []
|
||||||
|
|
||||||
|
## Finds first entry of item. Returns -1 if no item found
|
||||||
func find(item : Item) -> int:
|
func find(item : Item) -> int:
|
||||||
for i in range(len(internal_array)):
|
for i in range(len(internal_array)):
|
||||||
if internal_array[i].held_item == item:
|
if internal_array[i].held_item == item:
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
## Tries to add an item into inventory. Returns not stored stack of item.
|
||||||
func add(stack: Stack) -> Stack:
|
func add(stack: Stack) -> Stack:
|
||||||
var found_index : int = find(stack.held_item)
|
var found_index : int = find(stack.held_item)
|
||||||
if found_index != -1:
|
if found_index != -1:
|
||||||
|
@ -37,12 +43,14 @@ func add(stack: Stack) -> Stack:
|
||||||
return null
|
return null
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
|
## Tries to take first item. Returns null if no items in inventory
|
||||||
func pop() -> Stack:
|
func pop() -> Stack:
|
||||||
for i in range(len(internal_array)):
|
for i in range(len(internal_array)):
|
||||||
if internal_array[i].held_item != null:
|
if internal_array[i].held_item != null:
|
||||||
return internal_array[i].extract()
|
return internal_array[i].extract()
|
||||||
return null
|
return null
|
||||||
|
|
||||||
|
## Tries to take certain item from inventory. Returns null if no item found
|
||||||
func take(item: Item,amount: int) -> Stack:
|
func take(item: Item,amount: int) -> Stack:
|
||||||
var found = find(item)
|
var found = find(item)
|
||||||
if found == -1:
|
if found == -1:
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
extends Resource
|
extends Resource
|
||||||
|
|
||||||
|
## Base unit of manipulated objects
|
||||||
|
|
||||||
class_name Item
|
class_name Item
|
||||||
|
|
||||||
|
## Name of item that is displayed
|
||||||
@export var display_name : StringName
|
@export var display_name : StringName
|
||||||
|
|
||||||
|
## Preview in UI
|
||||||
@export var preview : Texture2D
|
@export var preview : Texture2D
|
||||||
|
|
||||||
|
## Maximum amount of items that can be held in inventory
|
||||||
@export var stack_size : int
|
@export var stack_size : int
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue