From fed57e38df5922eb6417ebcf91c131c796055379 Mon Sep 17 00:00:00 2001 From: Rendo Date: Thu, 16 Oct 2025 22:05:24 +0500 Subject: [PATCH] Inventory context --- scenes/2d_world.tscn | 60 +++--------------- scenes/gui/construction_button.tscn | 17 +++++ scripts/gui/construction_button.gd | 12 ++-- scripts/gui/construction_buttons_generator.gd | 11 ++++ .../gui/construction_buttons_generator.gd.uid | 1 + scripts/gui/gui_event_bus.gd | 4 +- scripts/inventory/belt_inventory.gd | 47 +++++++------- scripts/inventory/in_out_inventory.gd | 9 +-- scripts/inventory/inventory.gd | 8 +-- scripts/inventory/inventory_context.gd | 13 ++++ scripts/inventory/inventory_context.gd.uid | 1 + scripts/inventory/splitter_inventory.gd | 27 ++++++++ scripts/inventory/splitter_inventory.gd.uid | 1 + scripts/inventory/storage.gd | 10 +-- scripts/structures/assembler.gd | 9 ++- scripts/structures/belt.gd | 6 +- sprites/atlasses/Popekko.png | Bin 2321 -> 2777 bytes 17 files changed, 128 insertions(+), 108 deletions(-) create mode 100644 scenes/gui/construction_button.tscn create mode 100644 scripts/gui/construction_buttons_generator.gd create mode 100644 scripts/gui/construction_buttons_generator.gd.uid create mode 100644 scripts/inventory/inventory_context.gd create mode 100644 scripts/inventory/inventory_context.gd.uid create mode 100644 scripts/inventory/splitter_inventory.gd create mode 100644 scripts/inventory/splitter_inventory.gd.uid diff --git a/scenes/2d_world.tscn b/scenes/2d_world.tscn index 3290eae..f5ce456 100644 --- a/scenes/2d_world.tscn +++ b/scenes/2d_world.tscn @@ -1,12 +1,9 @@ -[gd_scene load_steps=11 format=3 uid="uid://u7thalwj5742"] +[gd_scene load_steps=8 format=3 uid="uid://u7thalwj5742"] [ext_resource type="PackedScene" uid="uid://b7du4vul4bmpq" path="res://scenes/player_platform.tscn" id="1_4ia00"] -[ext_resource type="Script" uid="uid://db37xmo8ygwhg" path="res://scripts/gui/construction_button.gd" id="2_27lod"] -[ext_resource type="Resource" uid="uid://dj524jjal1f0n" path="res://generic/prototypes/belt.tres" id="3_2crwe"] +[ext_resource type="Script" uid="uid://bnjwg4rlcfd8k" path="res://scripts/gui/construction_buttons_generator.gd" id="3_i7yte"] [ext_resource type="Script" uid="uid://b4nkk0ndqcto" path="res://scripts/placement_manager.gd" id="4_i7yte"] [ext_resource type="PackedScene" uid="uid://bac552xgua68e" path="res://scenes/other_platform.tscn" id="5_mnxrb"] -[ext_resource type="Resource" uid="uid://yn1iesx30nfu" path="res://generic/prototypes/dbg_assembler.tres" id="5_ymyc2"] -[ext_resource type="Resource" uid="uid://jhe8wtjoo6c6" path="res://generic/prototypes/dbg_item_deposit.tres" id="6_bq5r8"] [sub_resource type="Animation" id="Animation_bq5r8"] length = 0.001 @@ -65,53 +62,14 @@ offset_top = -0.01600647 grow_horizontal = 2 grow_vertical = 0 -[node name="ConveyorButton" type="Button" parent="GUI/Panel"] +[node name="HBoxContainer" type="HBoxContainer" parent="GUI/Panel"] layout_mode = 1 -anchors_preset = -1 -anchor_left = 0.008 -anchor_top = 0.086 -anchor_right = 0.1 -anchor_bottom = 0.88600004 -offset_left = -0.096000195 -offset_top = -0.01999998 -offset_right = -0.20000076 -offset_bottom = -0.020004272 -size_flags_horizontal = 3 -theme_type_variation = &"CRT" -icon_alignment = 1 -expand_icon = true -script = ExtResource("2_27lod") -constructible = ExtResource("3_2crwe") - -[node name="AssemblerButton" type="Button" parent="GUI/Panel"] -layout_mode = 1 -anchors_preset = -1 -anchor_left = 0.107421875 -anchor_top = 0.08571429 -anchor_right = 0.19921875 -anchor_bottom = 0.8857143 -size_flags_horizontal = 3 -theme_type_variation = &"CRT" -icon_alignment = 1 -expand_icon = true -script = ExtResource("2_27lod") -constructible = ExtResource("5_ymyc2") -metadata/_edit_use_anchors_ = true - -[node name="SpawnerButton" type="Button" parent="GUI/Panel"] -layout_mode = 1 -anchors_preset = -1 -anchor_left = 0.20898438 -anchor_top = 0.08571429 -anchor_right = 0.30078125 -anchor_bottom = 0.8857143 -size_flags_horizontal = 3 -theme_type_variation = &"CRT" -icon_alignment = 1 -expand_icon = true -script = ExtResource("2_27lod") -constructible = ExtResource("6_bq5r8") -metadata/_edit_use_anchors_ = true +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("3_i7yte") [node name="Placer" type="Node2D" parent="."] script = ExtResource("4_i7yte") diff --git a/scenes/gui/construction_button.tscn b/scenes/gui/construction_button.tscn new file mode 100644 index 0000000..3734551 --- /dev/null +++ b/scenes/gui/construction_button.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=2 format=3 uid="uid://dwq8u8ptmcegk"] + +[ext_resource type="Script" uid="uid://db37xmo8ygwhg" path="res://scripts/gui/construction_button.gd" id="1_unpdg"] + +[node name="ConstructionButton" type="AspectRatioContainer"] +offset_right = 12.0 +offset_bottom = 9.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +theme_type_variation = &"CRT" + +[node name="Button" type="Button" parent="."] +layout_mode = 2 +theme_type_variation = &"CRT" +icon_alignment = 1 +expand_icon = true +script = ExtResource("1_unpdg") diff --git a/scripts/gui/construction_button.gd b/scripts/gui/construction_button.gd index a38d23e..6e1ec9d 100644 --- a/scripts/gui/construction_button.gd +++ b/scripts/gui/construction_button.gd @@ -1,9 +1,13 @@ extends Button -@export var constructible : Prototype +class_name ConstructionButton -func _ready() -> void: - icon = constructible.preview +var prototype : Prototype: + set(value): + prototype = value + icon = prototype.preview + get: + return prototype func _pressed() -> void: - GuiEventBus.construction_selected.emit(constructible) + GuiEventBus.construction_selected.emit(prototype) diff --git a/scripts/gui/construction_buttons_generator.gd b/scripts/gui/construction_buttons_generator.gd new file mode 100644 index 0000000..c90afd3 --- /dev/null +++ b/scripts/gui/construction_buttons_generator.gd @@ -0,0 +1,11 @@ +extends Node + +const button = preload("res://scenes/gui/construction_button.tscn") + +func _ready() -> void: + for file in ResourceLoader.list_directory("res://generic/prototypes/"): + var btn = button.instantiate() + var loaded_prototype : Prototype = ResourceLoader.load("res://generic/prototypes/"+file) + + btn.get_node("Button").prototype = loaded_prototype + add_child(btn) diff --git a/scripts/gui/construction_buttons_generator.gd.uid b/scripts/gui/construction_buttons_generator.gd.uid new file mode 100644 index 0000000..c591d53 --- /dev/null +++ b/scripts/gui/construction_buttons_generator.gd.uid @@ -0,0 +1 @@ +uid://bnjwg4rlcfd8k diff --git a/scripts/gui/gui_event_bus.gd b/scripts/gui/gui_event_bus.gd index 57b7d3b..eee3359 100644 --- a/scripts/gui/gui_event_bus.gd +++ b/scripts/gui/gui_event_bus.gd @@ -2,5 +2,5 @@ extends Node @warning_ignore_start("unused_signal") -signal construction_selected(constructible : Prototype) -signal construction_placed(constructible : Prototype) +signal construction_selected(prototype : Prototype) +signal construction_placed(prototype : Prototype) diff --git a/scripts/inventory/belt_inventory.gd b/scripts/inventory/belt_inventory.gd index 53b4d0c..b4b1f95 100644 --- a/scripts/inventory/belt_inventory.gd +++ b/scripts/inventory/belt_inventory.gd @@ -39,38 +39,37 @@ func find(item : Item) -> int: return -1 ## Tries to add an item into inventory. Returns not stored stack of item. -func add(stack: Stack) -> Stack: +func add(stack: Stack, context: InventoryContext = null) -> Stack: + if context != null: + var ang_diff = (context.position - context.source.global_position).angle()-context.target.direction + 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 + 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 if internal_array[0].amount != 0: return null stack_added.emit(stack,0) 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) - 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) - internal_array[capacity-1].merge_stack(stack) - return stack - return add(stack) - -func can_add(_item : Item = null) -> bool: +func can_add(_item : Item = null, context : InventoryContext = null) -> bool: + if context != null: + var ang_diff = (context.position - context.source.global_position).angle()-context.target.direction + 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 internal_array[0].amount == 0 -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(item) - ## Tries to take first item. Returns null if no items in inventory func pop() -> Stack: if internal_array[capacity-1].amount == 0: diff --git a/scripts/inventory/in_out_inventory.gd b/scripts/inventory/in_out_inventory.gd index da2209f..7bfd785 100644 --- a/scripts/inventory/in_out_inventory.gd +++ b/scripts/inventory/in_out_inventory.gd @@ -19,7 +19,7 @@ func _init() -> void: super() resize.call_deferred() -func add(stack : Stack) -> Stack: +func add(stack : Stack, _context: InventoryContext = null) -> Stack: for i in range(input_capacity): if input_array[i].can_be_merged(stack.held_item): stack_added.emit(stack,i) @@ -28,10 +28,7 @@ func add(stack : Stack) -> Stack: return null return stack -func add_from_side(stack : Stack, _ang_diff : float) -> Stack: - return add(stack) - -func can_add(item : Item = null) -> bool: +func can_add(item : Item = null, _context: InventoryContext = null) -> bool: if item == null: return false else: @@ -40,8 +37,6 @@ func can_add(item : Item = null) -> bool: 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: diff --git a/scripts/inventory/inventory.gd b/scripts/inventory/inventory.gd index 31f2331..17ee8fc 100644 --- a/scripts/inventory/inventory.gd +++ b/scripts/inventory/inventory.gd @@ -12,15 +12,11 @@ func _init() -> void: ## Tries to add an item into inventory. Returns not stored stack of item. @abstract -func add(stack : Stack) -> Stack -@abstract -func add_from_side(stack : Stack, ang_diff : float) -> Stack +func add(stack : Stack, context: InventoryContext = null) -> Stack ## Returns if conditions of adding are met @abstract -func can_add(item : Item = null) -> bool -@abstract -func can_add_from_side(ang_diff : float, item : Item = null) -> bool +func can_add(item : Item = null, context: InventoryContext = null) -> bool ## Tries to take first item. Returns null if no items in inventory @abstract diff --git a/scripts/inventory/inventory_context.gd b/scripts/inventory/inventory_context.gd new file mode 100644 index 0000000..9a77177 --- /dev/null +++ b/scripts/inventory/inventory_context.gd @@ -0,0 +1,13 @@ +extends RefCounted + +class_name InventoryContext + +func _init(_source: Structure, _target: Structure,_position: Vector2) -> void: + self.source = _source + self.target = _target + self.position = _position + + +var source : Structure +var target : Structure +var position : Vector2 diff --git a/scripts/inventory/inventory_context.gd.uid b/scripts/inventory/inventory_context.gd.uid new file mode 100644 index 0000000..2ce56c7 --- /dev/null +++ b/scripts/inventory/inventory_context.gd.uid @@ -0,0 +1 @@ +uid://d3ytipk4nt11d diff --git a/scripts/inventory/splitter_inventory.gd b/scripts/inventory/splitter_inventory.gd new file mode 100644 index 0000000..025ca0e --- /dev/null +++ b/scripts/inventory/splitter_inventory.gd @@ -0,0 +1,27 @@ +@tool + +extends Inventory + +@export var capacity : int: + set(value): + if value < 0: + return + if value == capacity: + return + capacity = value + get: + return capacity + +@export_storage var upper_array : Array[InventorySlot] +@export_storage var down_array : Array[InventorySlot] + +func _init() -> void: + super() + deferred_init.call_deferred() + +func deferred_init(): + upper_array.resize(capacity) + down_array.resize(capacity) + for i in range(capacity): + upper_array[i] = InventorySlot.new() + down_array[i] = InventorySlot.new() diff --git a/scripts/inventory/splitter_inventory.gd.uid b/scripts/inventory/splitter_inventory.gd.uid new file mode 100644 index 0000000..d335a56 --- /dev/null +++ b/scripts/inventory/splitter_inventory.gd.uid @@ -0,0 +1 @@ +uid://dlt3mbu6hk572 diff --git a/scripts/inventory/storage.gd b/scripts/inventory/storage.gd index 47257f4..4d23b0b 100644 --- a/scripts/inventory/storage.gd +++ b/scripts/inventory/storage.gd @@ -36,7 +36,7 @@ func find(item : Item) -> int: return -1 ## Tries to add an item into inventory. Returns not stored stack of item. -func add(stack: Stack) -> Stack: +func add(stack: Stack, _context: InventoryContext = null) -> Stack: var found_index : int = find(stack.held_item) if found_index != -1: stack_added.emit(stack,found_index) @@ -51,10 +51,7 @@ func add(stack: Stack) -> Stack: return null return stack -func add_from_side(stack : Stack, _ang_diff : float) -> Stack: - return add(stack) - -func can_add(item : Item = null) -> bool: +func can_add(item : Item = null, _context: InventoryContext = null) -> bool: if item == null: for i in range(internal_array): return internal_array[i].amount == 0 @@ -66,9 +63,6 @@ func can_add(item : Item = null) -> bool: return internal_array[i].held_item.is_equal(item) 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(len(internal_array)): diff --git a/scripts/structures/assembler.gd b/scripts/structures/assembler.gd index d3a5614..905f80d 100644 --- a/scripts/structures/assembler.gd +++ b/scripts/structures/assembler.gd @@ -24,14 +24,17 @@ func _process(_delta: float) -> void: var output : Structure = get_output_structure() if output == null: return - var ang_diff = output.direction_difference(structure_parent) - 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)) + var transfer_context : InventoryContext = InventoryContext.new(structure_parent,output,get_output_position()) + if output.inventory.can_add(inventory.output_slot.held_item,transfer_context): + inventory.output_slot.merge_stack(output.inventory.add(inventory.output_slot.extract(),transfer_context)) func get_output_structure() -> Structure: var rotated = Vector2(1.5,-0.5).rotated(structure_parent.direction) return structure_parent.get_relative(rotated+Vector2(0.5,0.5)) +func get_output_position() -> Vector2: + return to_global(Vector2(1.5,-0.5).rotated(structure_parent.direction)+Vector2(0.5,0.5)) + func check_for_recipe(_stack : Stack, _position : int) -> void: if inventory.output_slot.held_item != null and inventory.output_slot.amount == inventory.output_slot.held_item.stack_size: return diff --git a/scripts/structures/belt.gd b/scripts/structures/belt.gd index 4150055..ac98622 100644 --- a/scripts/structures/belt.gd +++ b/scripts/structures/belt.gd @@ -26,10 +26,10 @@ func try_transfer(structure : Structure) -> void: if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0 or inventory.progress_array[inventory.capacity-1] < inventory.pop_treshold: return var last_slot = inventory.internal_array[inventory.capacity-1] - var ang_diff = structure_parent.direction_difference(structure) - if structure.inventory.can_add_from_side(ang_diff,last_slot.held_item) == false: + var transfer_context : InventoryContext = InventoryContext.new(structure_parent,structure,to_global(structure_parent.direction_vector())) + if structure.inventory.can_add(last_slot.held_item,transfer_context) == false: return - last_slot.merge_stack(structure.inventory.add_from_side(last_slot.extract(),ang_diff)) + last_slot.merge_stack(structure.inventory.add(last_slot.extract(),transfer_context)) if last_slot.amount == 0: inventory.sort() diff --git a/sprites/atlasses/Popekko.png b/sprites/atlasses/Popekko.png index fdd2e23fcd7a43de4a338a2fbae084249e519371..7b2df84670723fdf83b2129e17de229545be76a0 100644 GIT binary patch literal 2777 zcmeHJYdDna8h+-`FlwAi2ZSb(vd9V{%qS@`$RSxZSnVw3u##di7@zD_BZ;V~ zDd$79WK?P+hqaxWOjr{-IM-wx#?1b#eeG*sd;i?O_xtC)zxR2s=Xt*Se&751lH6SO zlN8hy0D$Cpz}_7Ii$aW!JOQ1aS+^bnP~bS)+j&N1O}uMN*7e>1(Xb{+?I3)psv1ImdeYfVKR2%dLMsw$DULw8V$A6d$IME)R{Ldasd z6dEaHAwN)YZK|zRN?@+#5eRRE|6fx+8Nc^+mIokBKuywM)*2m6X>d z@yZAPIKyQuq~oCOxaM|jye|`vvFEi34cv`R2St+5l2CZBcfQURt;0@cZ z4=V3CO{XU71{}S(9)rkVL;i?`g`YH4W?IIQ_u0SDD7oUGqH_A_^fNM97$crcA(MR> zp8`=)YX+qeA1-jYvpf$AOPgfzKrQ7GYwk;5uCbX~I{n6j7#p}|!WjA1bX@wZflNLg zJN0#DVld7uM<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