Broken pos
This commit is contained in:
parent
11d928c4f3
commit
3af13d0a8b
24 changed files with 304 additions and 146 deletions
|
@ -34,7 +34,7 @@ func deferred_init():
|
|||
## Finds first entry of item. Returns -1 if no item found
|
||||
func find(item : Item) -> int:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].held_item == item:
|
||||
if internal_array[i].can_be_merged(item):
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
@ -58,15 +58,15 @@ func add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
|||
return internal_array[capacity-1].merge_stack(stack)
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
func can_add(_item : Item = null) -> bool:
|
||||
return internal_array[0].amount == 0
|
||||
|
||||
func can_add_from_side(ang_diff : float) -> bool:
|
||||
func can_add_from_side(ang_diff : float, item : Item = null) -> bool:
|
||||
if is_equal_approx(abs(ang_diff),PI/2):
|
||||
return internal_array[capacity/2].amount == 0
|
||||
elif is_equal_approx(abs(ang_diff),PI):
|
||||
return internal_array[capacity-1].amount == 0
|
||||
return can_add()
|
||||
return can_add(item)
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
|
@ -77,6 +77,13 @@ func pop() -> Stack:
|
|||
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):
|
||||
|
|
94
scripts/inventory/in_out_inventory.gd
Normal file
94
scripts/inventory/in_out_inventory.gd
Normal file
|
@ -0,0 +1,94 @@
|
|||
@tool
|
||||
extends Inventory
|
||||
|
||||
class_name InOutInventory
|
||||
|
||||
@export var input_capacity : int
|
||||
@export var output_capacity : int
|
||||
|
||||
@export_storage var input_array : Array[InventorySlot]
|
||||
@export_storage var output_array : Array[InventorySlot]
|
||||
|
||||
func resize() -> void:
|
||||
input_array.resize(input_capacity)
|
||||
output_array.resize(output_capacity)
|
||||
for i in range(input_capacity):
|
||||
input_array[i] = InventorySlot.new()
|
||||
for i in range(output_capacity):
|
||||
output_array[i] = InventorySlot.new()
|
||||
|
||||
func _init() -> void:
|
||||
super()
|
||||
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:
|
||||
return null
|
||||
return stack
|
||||
|
||||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(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):
|
||||
print(input_array[i].filter)
|
||||
if input_array[i].can_be_merged(item):
|
||||
print(input_array[i].filter)
|
||||
return true
|
||||
#return true
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
||||
return can_add(item)
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item != null:
|
||||
var extracted = output_array[i].extract()
|
||||
stack_taken.emit(extracted,i)
|
||||
return extracted
|
||||
return null
|
||||
|
||||
func peek() -> Stack:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item != null:
|
||||
var peeked = output_array[i].as_stack()
|
||||
return peeked
|
||||
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:
|
||||
return null
|
||||
var extracted = output_array[found].extract_stack(amount)
|
||||
stack_taken.emit(extracted,found)
|
||||
return extracted
|
||||
|
||||
func find_input(item : Item) -> int:
|
||||
for i in range(input_capacity):
|
||||
if input_array[i].held_item == item:
|
||||
return i
|
||||
return -1
|
||||
|
||||
func find(item : Item) -> int:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item == item:
|
||||
return i
|
||||
return -1
|
1
scripts/inventory/in_out_inventory.gd.uid
Normal file
1
scripts/inventory/in_out_inventory.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://m6b6vawdqgqb
|
|
@ -18,14 +18,17 @@ func add_from_side(stack : Stack, ang_diff : float) -> Stack
|
|||
|
||||
## Returns if conditions of adding are met
|
||||
@abstract
|
||||
func can_add() -> bool
|
||||
func can_add(item : Item = null) -> bool
|
||||
@abstract
|
||||
func can_add_from_side(ang_diff : float) -> bool
|
||||
func can_add_from_side(ang_diff : float, item : Item = null) -> bool
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
@abstract
|
||||
func pop() -> Stack
|
||||
|
||||
@abstract
|
||||
func peek() -> Stack
|
||||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
@abstract
|
||||
func take(item : Item,amount: int) -> Stack
|
||||
|
|
|
@ -28,6 +28,8 @@ class_name InventorySlot
|
|||
get:
|
||||
return amount
|
||||
|
||||
@export_storage var filter : Comparable
|
||||
|
||||
## Get some amount of items from slot. Returns null if slot is empty
|
||||
func extract_stack(extract_amount: int) -> Stack:
|
||||
if amount == 0:
|
||||
|
@ -53,9 +55,26 @@ func extract() -> Stack:
|
|||
|
||||
return return_stack
|
||||
|
||||
func as_stack() -> Stack:
|
||||
if amount == 0:
|
||||
return null
|
||||
|
||||
var return_stack : Stack = Stack.new(held_item,amount)
|
||||
|
||||
return return_stack
|
||||
|
||||
func can_be_merged(item : Item) -> bool:
|
||||
if filter != null:
|
||||
return filter.is_equal(item)
|
||||
if amount == 0:
|
||||
return true
|
||||
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 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
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
@tool
|
||||
|
||||
extends Inventory
|
||||
|
||||
## Base class for FIFO inventories.
|
||||
|
||||
class_name Queue
|
||||
|
||||
## Amount of stacks that can be held in storage
|
||||
@export var capacity : int:
|
||||
set(value):
|
||||
if value < 0:
|
||||
return
|
||||
if value == capacity:
|
||||
return
|
||||
capacity = value
|
||||
get:
|
||||
return capacity
|
||||
|
||||
## :3
|
||||
@export_storage var internal_array : Array[InventorySlot] = []
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
super()
|
||||
internal_array.resize(capacity)
|
||||
for i in range(capacity):
|
||||
internal_array[i] = InventorySlot.new()
|
||||
|
||||
## 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:
|
||||
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:
|
||||
return null
|
||||
return stack
|
||||
|
||||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].amount == 0:
|
||||
return true
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float) -> bool:
|
||||
return can_add()
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
if internal_array[0].held_item == null:
|
||||
return null
|
||||
var stack_to_return = internal_array[0].extract()
|
||||
stack_taken.emit(stack_to_return,0)
|
||||
sort()
|
||||
return stack_to_return
|
||||
|
||||
## 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:
|
||||
return null
|
||||
var stack_to_return = internal_array[found].extract_stack(amount)
|
||||
stack_taken.emit(stack_to_return,found)
|
||||
sort()
|
||||
return stack_to_return
|
||||
|
||||
func sort() -> void:
|
||||
for i in range(len(internal_array)-1):
|
||||
if internal_array[i] == null:
|
||||
internal_array[i].merge_stack(internal_array[i+1].extract())
|
|
@ -1 +0,0 @@
|
|||
uid://q2gbtqfi32d
|
|
@ -21,6 +21,9 @@ class_name Storage
|
|||
|
||||
func _init() -> void:
|
||||
super()
|
||||
deferred_init.call_deferred()
|
||||
|
||||
func deferred_init():
|
||||
internal_array.resize(capacity)
|
||||
for i in range(capacity):
|
||||
internal_array[i] = InventorySlot.new()
|
||||
|
@ -51,14 +54,20 @@ func add(stack: Stack) -> Stack:
|
|||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].amount == 0:
|
||||
return true
|
||||
func can_add(item : Item = null) -> bool:
|
||||
if item == null:
|
||||
for i in range(internal_array):
|
||||
return internal_array[i].amount == 0
|
||||
return false
|
||||
else:
|
||||
for i in range(internal_array):
|
||||
if internal_array[i].amount == 0:
|
||||
return (internal_array[i].filter != null and internal_array[i].filter.is_equal(item)) or internal_array[i].filter == null
|
||||
return internal_array[i].held_item.is_equal(item)
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float) -> bool:
|
||||
return can_add()
|
||||
func can_add_from_side(_ang_diff : float,item : Item = null) -> bool:
|
||||
return can_add(item)
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
|
@ -69,6 +78,13 @@ func pop() -> Stack:
|
|||
return extracted
|
||||
return null
|
||||
|
||||
func peek() -> Stack:
|
||||
for i in range(len(internal_array)):
|
||||
if internal_array[i].held_item != null:
|
||||
var peeked = internal_array[i].as_stack()
|
||||
return peeked
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue