Compare commits
2 commits
3af13d0a8b
...
ea9c41054b
Author | SHA1 | Date | |
---|---|---|---|
ea9c41054b | |||
5da9e11f36 |
9 changed files with 105 additions and 62 deletions
|
@ -10,6 +10,7 @@
|
|||
resource_local_to_scene = true
|
||||
script = ExtResource("3_ruvuk")
|
||||
capacity = 4
|
||||
progress_speed = 10.0
|
||||
pop_treshold = 0.95
|
||||
internal_array = Array[ExtResource("2_54w8r")]([Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
||||
, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
||||
|
|
|
@ -8,9 +8,15 @@
|
|||
[ext_resource type="Resource" uid="uid://d2lbc1qqkayaa" path="res://generic/recipes/test_recipe.tres" id="6_wqoim"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_kno0u"]
|
||||
resource_local_to_scene = true
|
||||
script = ExtResource("3_wqoim")
|
||||
input_capacity = 2
|
||||
output_capacity = 1
|
||||
input_array = Array[ExtResource("2_p1cyh")]([Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
|
||||
, Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
|
||||
])
|
||||
output_slot = Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
|
||||
|
||||
metadata/_custom_type_script = "uid://m6b6vawdqgqb"
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]
|
||||
|
@ -21,6 +27,7 @@ region = Rect2(32, 0, 32, 32)
|
|||
script = ExtResource("1_k5y3y")
|
||||
dimensions = Rect2i(0, 0, 2, 2)
|
||||
inventory = SubResource("Resource_kno0u")
|
||||
facing = 2
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
texture = SubResource("AtlasTexture_xh4eg")
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -7,32 +7,24 @@ class_name InOutInventory
|
|||
@export var output_capacity : int
|
||||
|
||||
@export_storage var input_array : Array[InventorySlot]
|
||||
@export_storage var output_array : Array[InventorySlot]
|
||||
@export_storage var output_slot : 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()
|
||||
output_slot = 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:
|
||||
input_array[i].merge_stack(stack)
|
||||
if stack.is_valid() == false:
|
||||
return null
|
||||
return stack
|
||||
|
||||
|
@ -41,16 +33,11 @@ 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):
|
||||
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:
|
||||
|
@ -58,27 +45,23 @@ func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
|||
|
||||
## 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)
|
||||
if output_slot.held_item != null:
|
||||
var extracted = output_slot.extract()
|
||||
stack_taken.emit(extracted,0)
|
||||
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()
|
||||
if output_slot.held_item != null:
|
||||
var peeked = output_slot.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:
|
||||
if item != output_slot.filter:
|
||||
return null
|
||||
var extracted = output_array[found].extract_stack(amount)
|
||||
stack_taken.emit(extracted,found)
|
||||
var extracted = output_slot.extract_stack(amount)
|
||||
return extracted
|
||||
|
||||
func find_input(item : Item) -> int:
|
||||
|
@ -88,7 +71,6 @@ func find_input(item : Item) -> int:
|
|||
return -1
|
||||
|
||||
func find(item : Item) -> int:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item == item:
|
||||
return i
|
||||
if output_slot.held_item == item:
|
||||
return 0
|
||||
return -1
|
||||
|
|
|
@ -71,17 +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:
|
||||
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
|
||||
|
||||
|
|
|
@ -4,16 +4,41 @@ extends StructureBehaviour
|
|||
@onready var inventory : InOutInventory = get_parent().inventory
|
||||
|
||||
func _ready() -> void:
|
||||
inventory.stack_added.connect(check_for_recipe)
|
||||
await get_tree().process_frame
|
||||
switch_recipe(selected_recipe)
|
||||
await get_tree().process_frame
|
||||
for i in len(selected_recipe.ingridients):
|
||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||
print(inventory.input_array[i].filter)
|
||||
|
||||
func switch_recipe(recipe: Recipe) -> void:
|
||||
selected_recipe = recipe
|
||||
inventory.input_capacity = len(selected_recipe.ingridients)
|
||||
inventory.resize()
|
||||
for i in len(selected_recipe.ingridients):
|
||||
for i in range(len(selected_recipe.ingridients)):
|
||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if inventory.output_slot.amount <= 0:
|
||||
return
|
||||
var output : Structure = get_output_structure()
|
||||
if output == null:
|
||||
return
|
||||
var ang_diff = Globals.facing_difference(output.facing,structure_parent.facing)
|
||||
if output.inventory.can_add_from_side(ang_diff):
|
||||
inventory.output_slot.merge_stack(output.inventory.add_from_side(inventory.output_slot.extract(),ang_diff))
|
||||
|
||||
func get_output_structure() -> Structure:
|
||||
var facing_direction = Structure.facing_to_vector(structure_parent.facing)
|
||||
var rotated = Vector2(0.5,1.5).rotated(-Vector2.DOWN.angle_to(facing_direction))
|
||||
return structure_parent.get_relative(rotated+Vector2(0.5,0.5))
|
||||
|
||||
func check_for_recipe(_stack : Stack, _position : int) -> void:
|
||||
for i in range(len(selected_recipe.ingridients)):
|
||||
if inventory.input_array[i].amount < selected_recipe.ingridients[i].amount:
|
||||
return
|
||||
craft()
|
||||
|
||||
func craft() -> void:
|
||||
for i in range(len(selected_recipe.ingridients)):
|
||||
inventory.input_array[i].extract_stack(selected_recipe.ingridients[i].amount)
|
||||
inventory.output_slot.merge_stack(Stack.new(selected_recipe.result.item,selected_recipe.result.amount))
|
||||
|
|
|
@ -19,12 +19,7 @@ func _process(delta: float) -> void:
|
|||
inventory.advance(delta)
|
||||
queue_redraw()
|
||||
var next : Structure = get_next()
|
||||
if next == null or next.inventory == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,structure_parent.facing),inventory.internal_array[inventory.capacity-1].held_item) == false:
|
||||
return
|
||||
var popped = inventory.pop()
|
||||
if popped == null:
|
||||
return
|
||||
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,structure_parent.facing))
|
||||
try_transfer(next)
|
||||
|
||||
func calculate_position(index: int) -> Vector2:
|
||||
var indexed_part = 16.0 / inventory.capacity
|
||||
|
@ -43,6 +38,18 @@ func get_next() -> Structure:
|
|||
var faced_vector = Structure.facing_to_vector(structure_parent.facing)
|
||||
return structure_parent.get_relative(faced_vector)
|
||||
|
||||
func try_transfer(structure : Structure) -> void:
|
||||
if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0:
|
||||
return
|
||||
var last_slot = inventory.internal_array[inventory.capacity-1]
|
||||
var ang_diff = Globals.facing_difference(structure_parent.facing,structure.facing)
|
||||
if structure.inventory.can_add_from_side(ang_diff,last_slot.held_item) == false:
|
||||
return
|
||||
last_slot.merge_stack(structure.inventory.add_from_side(last_slot.extract(),ang_diff))
|
||||
|
||||
if last_slot.amount == 0:
|
||||
inventory.sort()
|
||||
|
||||
func _on_conveyor_switched_facing(to: Structure.Facing) -> void:
|
||||
match to:
|
||||
Structure.Facing.RIGHT:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue