diff --git a/scenes/conveyor.tscn b/scenes/conveyor.tscn index 18a8b7c..75144ee 100644 --- a/scenes/conveyor.tscn +++ b/scenes/conveyor.tscn @@ -10,7 +10,6 @@ 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) diff --git a/scenes/debug_assembler.tscn b/scenes/debug_assembler.tscn index 0da2137..0ad16e3 100644 --- a/scenes/debug_assembler.tscn +++ b/scenes/debug_assembler.tscn @@ -8,15 +8,9 @@ [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"] @@ -27,7 +21,6 @@ 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") diff --git a/scripts/inventory/belt_inventory.gd b/scripts/inventory/belt_inventory.gd index 53b4d0c..adff753 100644 --- a/scripts/inventory/belt_inventory.gd +++ b/scripts/inventory/belt_inventory.gd @@ -43,22 +43,19 @@ func add(stack: Stack) -> Stack: if internal_array[0].amount != 0: return null stack_added.emit(stack,0) - internal_array[0].merge_stack(stack) - return stack + return internal_array[0].merge_stack(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) - internal_array[capacity/2].merge_stack(stack) - return stack + return internal_array[capacity/2].merge_stack(stack) elif is_equal_approx(abs(ang_diff), PI): if internal_array[capacity-1].amount != 0: return null stack_added.emit(stack,capacity-1) - internal_array[capacity-1].merge_stack(stack) - return stack + return internal_array[capacity-1].merge_stack(stack) return add(stack) func can_add(_item : Item = null) -> bool: @@ -114,7 +111,4 @@ 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 - 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) + internal_array[i].merge_stack(internal_array[i-1].extract()) diff --git a/scripts/inventory/in_out_inventory.gd b/scripts/inventory/in_out_inventory.gd index da2209f..055c2f8 100644 --- a/scripts/inventory/in_out_inventory.gd +++ b/scripts/inventory/in_out_inventory.gd @@ -7,24 +7,32 @@ class_name InOutInventory @export var output_capacity : int @export_storage var input_array : Array[InventorySlot] -@export_storage var output_slot : 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() - output_slot = 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) - input_array[i].merge_stack(stack) - if stack.is_valid() == false: + stack = input_array[i].merge_stack(stack) + if stack == null: return null return stack @@ -33,35 +41,44 @@ 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 false + #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: - if output_slot.held_item != null: - var extracted = output_slot.extract() - stack_taken.emit(extracted,0) - return extracted + 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: - if output_slot.held_item != null: - var peeked = output_slot.as_stack() - return peeked + 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: - if item != output_slot.filter: + var found = find(item) + if found == -1: return null - var extracted = output_slot.extract_stack(amount) + var extracted = output_array[found].extract_stack(amount) + stack_taken.emit(extracted,found) return extracted func find_input(item : Item) -> int: @@ -71,6 +88,7 @@ func find_input(item : Item) -> int: return -1 func find(item : Item) -> int: - if output_slot.held_item == item: - return 0 + for i in range(output_capacity): + if output_array[i].held_item == item: + return i return -1 diff --git a/scripts/inventory/inventory_slot.gd b/scripts/inventory/inventory_slot.gd index af5ed47..a99ffc9 100644 --- a/scripts/inventory/inventory_slot.gd +++ b/scripts/inventory/inventory_slot.gd @@ -71,25 +71,17 @@ 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) -> bool: - if stack == null or stack.is_valid() == false: - return false - - if filter and filter.is_equal(stack.held_item) == false: - return false - +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 - 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 + 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) diff --git a/scripts/inventory/stack.gd b/scripts/inventory/stack.gd index 93767a2..062425f 100644 --- a/scripts/inventory/stack.gd +++ b/scripts/inventory/stack.gd @@ -13,10 +13,3 @@ 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 diff --git a/scripts/inventory/storage.gd b/scripts/inventory/storage.gd index 47257f4..1ef1be1 100644 --- a/scripts/inventory/storage.gd +++ b/scripts/inventory/storage.gd @@ -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) - internal_array[found_index].merge_stack(stack) - if stack.is_valid() == false: + stack = internal_array[found_index].merge_stack(stack) + if stack == null: 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) - internal_array[i].merge_stack(stack) - if stack.is_valid() == false: + stack = internal_array[i].merge_stack(stack) + if stack == null: return null return stack diff --git a/scripts/structures/assembler.gd b/scripts/structures/assembler.gd index ca99392..f107eb8 100644 --- a/scripts/structures/assembler.gd +++ b/scripts/structures/assembler.gd @@ -4,41 +4,16 @@ 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 range(len(selected_recipe.ingridients)): + for i in 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)) diff --git a/scripts/structures/belt.gd b/scripts/structures/belt.gd index 0d5b32f..702ab7f 100644 --- a/scripts/structures/belt.gd +++ b/scripts/structures/belt.gd @@ -19,7 +19,12 @@ func _process(delta: float) -> void: inventory.advance(delta) queue_redraw() var next : Structure = get_next() - try_transfer(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)) func calculate_position(index: int) -> Vector2: var indexed_part = 16.0 / inventory.capacity @@ -38,18 +43,6 @@ 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: