This commit is contained in:
Rendo 2025-10-17 18:22:49 +05:00
commit 074bb21ffd
18 changed files with 99 additions and 51 deletions

View file

@ -14,12 +14,10 @@ class_name BeltInventory
get:
return capacity
@export var progress_speed : float = 1.0
@export var pop_treshold : float = 0.99
## :3
@export_storage var internal_array : Array[InventorySlot] = []
@export_storage var progress_array : Array[float] = []
func _init() -> void:
super()
@ -27,7 +25,6 @@ func _init() -> void:
func deferred_init():
internal_array.resize(capacity)
progress_array.resize(capacity)
for i in range(capacity):
internal_array[i] = InventorySlot.new()
@ -74,30 +71,21 @@ func can_add(_item : Item = null, context : InventoryContext = null) -> bool:
func pop() -> Stack:
if internal_array[capacity-1].amount == 0:
return null
if progress_array[capacity-1] < pop_treshold:
return null
stack_taken.emit(internal_array[capacity-1], capacity-1)
return internal_array[capacity-1].extract()
func peek() -> Stack:
if internal_array[capacity-1].amount == 0:
return null
if progress_array[capacity-1] < pop_treshold:
return null
return internal_array[capacity-1].as_stack()
func advance(delta : float) -> void:
var progress_flag : bool = false
for i in range(capacity):
if internal_array[i].amount == 0:
continue
if progress_array[i] >= 1:
progress_flag = true
continue
progress_array[i] += delta * progress_speed
if progress_flag:
sort()
func advance() -> void:
for i in range(capacity-1,0,-1):
if internal_array[i].amount == 0 and internal_array[0].updated == false:
var extracted_stack = internal_array[i-1].extract()
internal_array[i].merge_stack(extracted_stack)
if extracted_stack != null and extracted_stack.is_valid():
internal_array[i-1].merge_stack(extracted_stack)
## Tries to take certain item from inventory. Returns null if no item found
func take(item: Item,amount: int) -> Stack:
@ -108,12 +96,6 @@ func take(item: Item,amount: int) -> Stack:
stack_taken.emit(extracted,found)
return extracted
func sort() -> void:
for i in range(capacity-1,0,-1):
if progress_array[i-1] >= 1.0 and internal_array[i].amount == 0:
progress_array[i-1] = 0
progress_array[i] = 0
var extracted_stack = internal_array[i-1].extract()
internal_array[i].merge_stack(extracted_stack)
if extracted_stack.is_valid():
internal_array[i-1].merge_stack(extracted_stack)
func refresh() -> void:
for i in range(capacity):
internal_array[i].updated = false

View file

@ -20,6 +20,8 @@ func _init() -> void:
resize.call_deferred()
func add(stack : Stack, _context: InventoryContext = null) -> Stack:
if stack == null:
return stack
for i in range(input_capacity):
if input_array[i].can_be_merged(stack.held_item):
stack_added.emit(stack,i)
@ -69,3 +71,7 @@ func find(item : Item) -> int:
if output_slot.held_item == item:
return 0
return -1
func refresh() -> void:
for i in range(input_capacity):
input_array[i].updated = false

View file

@ -33,6 +33,9 @@ func take(item : Item,amount: int) -> Stack
@abstract
func find(item : Item) -> int
@abstract
func refresh() -> void
## Does inventory have certain item?
func has(item : Item) -> bool:
return find(item) != -1

View file

@ -30,12 +30,15 @@ class_name InventorySlot
return amount
@export_storage var filter : Comparable
@export_storage var updated : bool
## Get some amount of items from slot. Returns null if slot is empty
func extract_stack(extract_amount: int) -> Stack:
if amount == 0:
if amount == 0 or updated:
return null
updated = true
var return_stack : Stack
if extract_amount > amount:
return_stack = Stack.new(held_item,amount)
@ -48,9 +51,11 @@ func extract_stack(extract_amount: int) -> Stack:
## Extract entire stack from slot. Returns null if slot is empty
func extract() -> Stack:
if amount == 0:
if amount == 0 or updated:
return null
updated = true
var return_stack : Stack = Stack.new(held_item,amount)
self.amount = 0
@ -65,6 +70,8 @@ func as_stack() -> Stack:
return return_stack
func can_be_merged(item : Item) -> bool:
if updated:
return false
if filter != null:
return filter.is_equal(item)
if amount <= 0:
@ -73,12 +80,14 @@ func can_be_merged(item : Item) -> bool:
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
func merge_stack(stack: Stack) -> bool:
if stack == null or stack.is_valid() == false:
if stack == null or stack.is_valid() == false or updated:
return false
if filter and filter.is_equal(stack.held_item) == false:
return false
updated = true
if held_item == null:
held_item = stack.held_item
amount = stack.amount

View file

@ -29,7 +29,7 @@ func deferred_init():
down_array[i] = InventorySlot.new()
func add(stack : Stack, context: InventoryContext = null) -> Stack:
if context == null:
if context == null or stack == null:
return stack
if context.position == context.target.global_position:
return add_to_array(stack,upper_array,context)
@ -72,3 +72,8 @@ func take(item : Item,amount: int) -> Stack:
## Finds first entry of item. Returns -1 if no item found
func find(item : Item) -> int:
return -1
func refresh() -> void:
for i in range(capacity):
upper_array[i].updated = false
down_array[i].updated = false

View file

@ -87,3 +87,7 @@ func take(item: Item,amount: int) -> Stack:
var extracted = internal_array[found].extract_stack(amount)
stack_taken.emit(extracted,found)
return extracted
func refresh() -> void:
for i in range(capacity):
internal_array[i].updated = false

View file

@ -27,3 +27,6 @@ func take(item : Item,amount: int) -> Stack:
## Finds first entry of item. Returns -1 if no item found
func find(item : Item) -> int:
return -1
func refresh():
pass