Stack rework
This commit is contained in:
parent
5da9e11f36
commit
ea9c41054b
7 changed files with 60 additions and 39 deletions
|
@ -43,19 +43,22 @@ func add(stack: Stack) -> Stack:
|
|||
if internal_array[0].amount != 0:
|
||||
return null
|
||||
stack_added.emit(stack,0)
|
||||
return internal_array[0].merge_stack(stack)
|
||||
internal_array[0].merge_stack(stack)
|
||||
return stack
|
||||
|
||||
func add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
||||
if is_equal_approx(abs(ang_diff),PI/2):
|
||||
if internal_array[capacity/2].amount != 0:
|
||||
return null
|
||||
stack_added.emit(stack,capacity/2)
|
||||
return internal_array[capacity/2].merge_stack(stack)
|
||||
internal_array[capacity/2].merge_stack(stack)
|
||||
return stack
|
||||
elif is_equal_approx(abs(ang_diff), PI):
|
||||
if internal_array[capacity-1].amount != 0:
|
||||
return null
|
||||
stack_added.emit(stack,capacity-1)
|
||||
return internal_array[capacity-1].merge_stack(stack)
|
||||
internal_array[capacity-1].merge_stack(stack)
|
||||
return stack
|
||||
return add(stack)
|
||||
|
||||
func can_add(_item : Item = null) -> bool:
|
||||
|
@ -111,4 +114,7 @@ func sort() -> void:
|
|||
if progress_array[i-1] >= 1.0 and internal_array[i].amount == 0:
|
||||
progress_array[i-1] = 0
|
||||
progress_array[i] = 0
|
||||
internal_array[i].merge_stack(internal_array[i-1].extract())
|
||||
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)
|
||||
|
|
|
@ -20,17 +20,11 @@ func _init() -> void:
|
|||
resize.call_deferred()
|
||||
|
||||
func add(stack : Stack) -> Stack:
|
||||
var found_index : int = find_input(stack.held_item)
|
||||
if found_index != -1:
|
||||
stack_added.emit(stack,found_index)
|
||||
stack = input_array[found_index].merge_stack(stack)
|
||||
if stack == null:
|
||||
return null
|
||||
for i in range(input_capacity):
|
||||
if input_array[i].can_be_merged(stack.held_item):
|
||||
stack_added.emit(stack,i)
|
||||
stack = input_array[i].merge_stack(stack)
|
||||
if stack == null:
|
||||
input_array[i].merge_stack(stack)
|
||||
if stack.is_valid() == false:
|
||||
return null
|
||||
return stack
|
||||
|
||||
|
@ -39,15 +33,12 @@ func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
|||
|
||||
func can_add(item : Item = null) -> bool:
|
||||
if item == null:
|
||||
for i in range(input_capacity):
|
||||
return input_array[i].amount == 0
|
||||
return false
|
||||
else:
|
||||
for i in range(input_capacity):
|
||||
if input_array[i].can_be_merged(item):
|
||||
return true
|
||||
#return true
|
||||
return false
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
||||
return can_add(item)
|
||||
|
@ -68,6 +59,8 @@ func peek() -> Stack:
|
|||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
func take(item: Item,amount: int) -> Stack:
|
||||
if item != output_slot.filter:
|
||||
return null
|
||||
var extracted = output_slot.extract_stack(amount)
|
||||
return extracted
|
||||
|
||||
|
|
|
@ -71,19 +71,25 @@ func can_be_merged(item : Item) -> bool:
|
|||
return item == held_item
|
||||
|
||||
## 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 stack == null or stack.held_item == null or stack.amount == 0:
|
||||
return stack
|
||||
func merge_stack(stack: Stack) -> bool:
|
||||
if stack == null or stack.is_valid() == false:
|
||||
return false
|
||||
|
||||
if filter and filter.is_equal(stack.held_item) == false:
|
||||
return false
|
||||
|
||||
if held_item == null:
|
||||
if filter != null and filter.is_equal(stack.held_item) == false:
|
||||
return stack
|
||||
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)
|
||||
stack.invalidate()
|
||||
return true
|
||||
|
||||
var delta_amount = stack.amount - (held_item.stack_size - amount)
|
||||
amount += stack.amount
|
||||
|
||||
if delta_amount <= 0:
|
||||
stack.invalidate()
|
||||
return true
|
||||
|
||||
stack.amount = delta_amount
|
||||
return true
|
||||
|
|
|
@ -13,3 +13,10 @@ func _init(item: Item = null, amount: int = 0) -> void:
|
|||
|
||||
## Amount of items in stack
|
||||
@export_storage var amount : int
|
||||
|
||||
func invalidate() -> void:
|
||||
held_item = null
|
||||
amount = 0
|
||||
|
||||
func is_valid() -> bool:
|
||||
return held_item != null or amount > 0
|
||||
|
|
|
@ -40,14 +40,14 @@ func add(stack: Stack) -> Stack:
|
|||
var found_index : int = find(stack.held_item)
|
||||
if found_index != -1:
|
||||
stack_added.emit(stack,found_index)
|
||||
stack = internal_array[found_index].merge_stack(stack)
|
||||
if stack == null:
|
||||
internal_array[found_index].merge_stack(stack)
|
||||
if stack.is_valid() == false:
|
||||
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_added.emit(stack,i)
|
||||
stack = internal_array[i].merge_stack(stack)
|
||||
if stack == null:
|
||||
internal_array[i].merge_stack(stack)
|
||||
if stack.is_valid() == false:
|
||||
return null
|
||||
return stack
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue