diff --git a/generic/items/dbg_item.tres b/generic/items/dbg_item.tres index 7bd1da2..ff58d13 100644 --- a/generic/items/dbg_item.tres +++ b/generic/items/dbg_item.tres @@ -1,6 +1,6 @@ [gd_resource type="Resource" script_class="Item" load_steps=5 format=3 uid="uid://byn78mwy08ff"] -[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_1hrh7"] +[ext_resource type="Texture2D" uid="uid://gfkhedfdi7ug" path="res://sprites/atlasses/Popekko.png" id="1_1hrh7"] [ext_resource type="Script" uid="uid://p5327ibxtyfs" path="res://scripts/item.gd" id="1_2pbm1"] [ext_resource type="PackedScene" uid="uid://bbh7akfbskq4u" path="res://scenes/debug_item.tscn" id="2_1hrh7"] diff --git a/generic/prototypes/conveyor.tres b/generic/prototypes/conveyor.tres index 04d8ee9..c50cf39 100644 --- a/generic/prototypes/conveyor.tres +++ b/generic/prototypes/conveyor.tres @@ -1,7 +1,7 @@ [gd_resource type="Resource" script_class="Prototype" load_steps=5 format=3 uid="uid://dj524jjal1f0n"] [ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_53h7j"] -[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_x27pp"] +[ext_resource type="Texture2D" uid="uid://gfkhedfdi7ug" path="res://sprites/atlasses/Popekko.png" id="1_x27pp"] [ext_resource type="PackedScene" uid="uid://b0h8dd82b3ox5" path="res://scenes/conveyor.tscn" id="2_hinjf"] [sub_resource type="AtlasTexture" id="AtlasTexture_uvy8r"] diff --git a/generic/prototypes/dbg_assembler.tres b/generic/prototypes/dbg_assembler.tres index 316eb81..072757f 100644 --- a/generic/prototypes/dbg_assembler.tres +++ b/generic/prototypes/dbg_assembler.tres @@ -1,7 +1,7 @@ [gd_resource type="Resource" script_class="Prototype" load_steps=5 format=3 uid="uid://yn1iesx30nfu"] [ext_resource type="Script" uid="uid://c80sp6f77l5ha" path="res://scripts/prototype.gd" id="1_mqcr0"] -[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_sh8t1"] +[ext_resource type="Texture2D" uid="uid://gfkhedfdi7ug" path="res://sprites/atlasses/Popekko.png" id="1_sh8t1"] [ext_resource type="PackedScene" uid="uid://dfatkxv6n55dw" path="res://scenes/debug_assembler.tscn" id="2_kf3x0"] [sub_resource type="AtlasTexture" id="AtlasTexture_kf3x0"] diff --git a/project.godot b/project.godot index 352b36b..39dd6b7 100644 --- a/project.godot +++ b/project.godot @@ -20,6 +20,7 @@ config/icon="res://icon.svg" GuiEventBus="*res://scripts/gui/gui_event_bus.gd" RuntimePlayerData="*res://scripts/autoloads/runtime_player_data.gd" Registry="*res://scripts/autoloads/registry.gd" +ConveyorManager="*res://scripts/autoloads/conveyor_manager.gd" [display] @@ -43,6 +44,12 @@ plc_cancel={ "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(441.39453, 21.890625),"global_position":Vector2(445.39453, 58.890625),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null) ] } +plc_rotate_up={ +"deadzone": 1.0, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":8,"position":Vector2(85.30078, 15.171875),"global_position":Vector2(89.30078, 52.171875),"factor":1.0,"button_index":4,"canceled":false,"pressed":true,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"location":0,"echo":false,"script":null) +] +} [layer_names] diff --git a/scenes/conveyor.tscn b/scenes/conveyor.tscn index 32c204f..41d461a 100644 --- a/scenes/conveyor.tscn +++ b/scenes/conveyor.tscn @@ -1,6 +1,6 @@ -[gd_scene load_steps=8 format=3 uid="uid://b0h8dd82b3ox5"] +[gd_scene load_steps=14 format=3 uid="uid://b0h8dd82b3ox5"] -[ext_resource type="Texture2D" uid="uid://dinyjq8853usn" path="res://sprites/atlasses/Popekko.png" id="1_kqxj7"] +[ext_resource type="Texture2D" uid="uid://gfkhedfdi7ug" path="res://sprites/atlasses/Popekko.png" id="1_kqxj7"] [ext_resource type="Script" uid="uid://bbd7o2st8kmgl" path="res://scripts/structure.gd" id="1_y326v"] [ext_resource type="Script" uid="uid://bd4ojfqrl8idm" path="res://scripts/inventory/inventory_slot.gd" id="2_54w8r"] [ext_resource type="Script" uid="uid://v0hkuo3gda1b" path="res://scripts/inventory/conveyor_inventory.gd" id="3_ruvuk"] @@ -10,20 +10,129 @@ resource_local_to_scene = true script = ExtResource("3_ruvuk") capacity = 4 -pop_treshold = null +internal_array = Array[ExtResource("2_54w8r")]([Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0) +, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0) +, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0) +, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0) +]) +progress_array = Array[float]([0.0, 0.0, 0.0, 0.0]) metadata/_custom_type_script = "uid://v0hkuo3gda1b" -[sub_resource type="AtlasTexture" id="AtlasTexture_uvy8r"] +[sub_resource type="AtlasTexture" id="AtlasTexture_54w8r"] atlas = ExtResource("1_kqxj7") -region = Rect2(16, 0, 16, 16) +region = Rect2(16, 0, 16, 128) + +[sub_resource type="Animation" id="Animation_ruvuk"] +length = 0.001 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 1, +"values": [0] +} + +[sub_resource type="Animation" id="Animation_1rfp0"] +resource_name = "down" +loop_mode = 1 +step = 0.25 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 0.25, 0.5, 0.75), +"transitions": PackedFloat32Array(1, 1, 1, 1), +"update": 1, +"values": [7, 6, 5, 4] +} + +[sub_resource type="Animation" id="Animation_t4je2"] +resource_name = "left" +length = 1.000025 +loop_mode = 1 +step = 0.25 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 0.25, 0.5, 0.75), +"transitions": PackedFloat32Array(1, 1, 1, 1), +"update": 1, +"values": [3, 2, 1, 0] +} + +[sub_resource type="Animation" id="Animation_54w8r"] +resource_name = "right" +length = 1.000025 +loop_mode = 1 +step = 0.25 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 0.25, 0.5, 0.75), +"transitions": PackedFloat32Array(1, 1, 1, 1), +"update": 1, +"values": [0, 1, 2, 3] +} + +[sub_resource type="Animation" id="Animation_crbfm"] +resource_name = "up" +loop_mode = 1 +step = 0.25 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 0.25, 0.5, 0.75), +"transitions": PackedFloat32Array(1, 1, 1, 1), +"update": 1, +"values": [4, 5, 6, 7] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_crbfm"] +_data = { +&"RESET": SubResource("Animation_ruvuk"), +&"down": SubResource("Animation_1rfp0"), +&"left": SubResource("Animation_t4je2"), +&"right": SubResource("Animation_54w8r"), +&"up": SubResource("Animation_crbfm") +} [node name="Conveyor" type="Node2D" groups=["buildings"]] script = ExtResource("1_y326v") inventory = SubResource("Resource_t4je2") +facing = 1 [node name="Sprite2D" type="Sprite2D" parent="."] -texture = SubResource("AtlasTexture_uvy8r") +texture = SubResource("AtlasTexture_54w8r") +vframes = 8 [node name="Conveyor" type="Node2D" parent="."] z_index = 1 script = ExtResource("5_54w8r") + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +libraries = { +&"": SubResource("AnimationLibrary_crbfm") +} +autoplay = "right" + +[connection signal="switched_facing" from="." to="Conveyor" method="_on_conveyor_switched_facing"] diff --git a/scripts/autoloads/conveyor_manager.gd b/scripts/autoloads/conveyor_manager.gd new file mode 100644 index 0000000..d1ebfe1 --- /dev/null +++ b/scripts/autoloads/conveyor_manager.gd @@ -0,0 +1,8 @@ +extends Node + +# TODO: Use this autoload for conveyor optimizations + +var sync_time : float + +func _process(delta: float) -> void: + sync_time = fposmod(sync_time+delta,1) diff --git a/scripts/autoloads/conveyor_manager.gd.uid b/scripts/autoloads/conveyor_manager.gd.uid new file mode 100644 index 0000000..0b0f993 --- /dev/null +++ b/scripts/autoloads/conveyor_manager.gd.uid @@ -0,0 +1 @@ +uid://damurnhtipmga diff --git a/scripts/globals.gd b/scripts/globals.gd index d7ff107..2ca0efa 100644 --- a/scripts/globals.gd +++ b/scripts/globals.gd @@ -4,3 +4,13 @@ extends Object class_name Globals const GRID_SIZE : Vector2 = Vector2(16,16) + +enum Sides { + RIGHT, + DOWN, + LEFT, + UP +} + +static func facing_difference(from : Structure.Facing, to : Structure.Facing) -> float: + return Structure.facing_to_vector(from).angle_to(Structure.facing_to_vector(to)) diff --git a/scripts/inventory/conveyor_inventory.gd b/scripts/inventory/conveyor_inventory.gd index 2048895..dcb21d2 100644 --- a/scripts/inventory/conveyor_inventory.gd +++ b/scripts/inventory/conveyor_inventory.gd @@ -45,9 +45,22 @@ func add(stack: Stack) -> Stack: stack_added.emit(stack,0) return internal_array[0].merge_stack(stack) +func add_from_side(stack : Stack, ang_diff : float) -> Stack: + if abs(ang_diff) >= PI/4: + if internal_array[capacity/2].amount != 0: + return null + stack_added.emit(stack,capacity/2) + return internal_array[capacity/2].merge_stack(stack) + return add(stack) + func can_add() -> bool: return internal_array[0].amount == 0 +func can_add_from_side(ang_diff : float) -> bool: + if abs(ang_diff) >= PI/4: + return internal_array[capacity/2].amount == 0 + return can_add() + ## 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/placement_manager.gd b/scripts/placement_manager.gd index d58e676..6166700 100644 --- a/scripts/placement_manager.gd +++ b/scripts/placement_manager.gd @@ -16,7 +16,10 @@ func _input(event: InputEvent) -> void: else: if held_construction.try_place(zone): held_construction = null - + if event.is_action_pressed("plc_rotate_up"): + if held_construction != null: + held_construction.cycle_up_facing() + if event.is_action_pressed("plc_cancel"): held_construction.queue_free() diff --git a/scripts/structure.gd b/scripts/structure.gd index cdbd90f..d13fa7d 100644 --- a/scripts/structure.gd +++ b/scripts/structure.gd @@ -4,6 +4,16 @@ extends Node2D ## Game object that interact with other structures in its grid space class_name Structure +enum Facing { + UNDIRECTIONAL = 0, + RIGHT = 1, + DOWN = 2, + LEFT = 3, + UP = 4 +} + +signal switched_facing(to: Facing) + ## Dimensions of structure in grid tiles @export var dimensions : Rect2i = Rect2i(0,0,1,1): set(value): @@ -15,6 +25,20 @@ class_name Structure ## Inventory of this structure @export var inventory : Inventory +@export var facing : Facing + +static func facing_to_vector(face : Facing) -> Vector2: + match face: + Facing.RIGHT: + return Vector2.RIGHT + Facing.LEFT: + return Vector2.LEFT + Facing.UP: + return Vector2.UP + Facing.DOWN: + return Vector2.DOWN + _: + return Vector2.ZERO ## Debug draw of points func _draw() -> void: @@ -58,3 +82,9 @@ func get_dimension_points() -> Array[Vector2]: for y in range(dimensions.size.y): result[x + y * dimensions.size.x] = (Vector2(x,y)*Globals.GRID_SIZE + Vector2(dimensions.position)) return result + +func cycle_up_facing() -> void: + if facing == Facing.UNDIRECTIONAL: + return + facing = wrapi(facing+1,1,5) as Facing + switched_facing.emit(facing) diff --git a/scripts/structures/conveyor.gd b/scripts/structures/conveyor.gd index c87375c..02cf5ef 100644 --- a/scripts/structures/conveyor.gd +++ b/scripts/structures/conveyor.gd @@ -1,20 +1,65 @@ extends Node2D -@onready var inventory : ConveyorInventory = get_parent().inventory +@onready var parent_structure : Structure = get_parent() +@onready var inventory : ConveyorInventory = parent_structure.inventory +@onready var animator : AnimationPlayer = $"../AnimationPlayer" + +func _ready() -> void: + sync_animations.call_deferred() + +func sync_animations() -> void: + animator.seek(ConveyorManager.sync_time,true) func _draw() -> void: for i in range(inventory.capacity): if inventory.internal_array[i].amount > 0: - var calculated_position = Vector2(-8,0)+Vector2(16/inventory.capacity*i+16/inventory.capacity*inventory.progress_array[i],0) - draw_texture(inventory.internal_array[i].held_item.preview,calculated_position - inventory.internal_array[i].held_item.preview.get_size()/2.0) + var calculated_position = calculate_position(i) - inventory.internal_array[i].held_item.preview.get_size()/2.0 + draw_texture(inventory.internal_array[i].held_item.preview,calculated_position) func _process(delta: float) -> void: inventory.advance(delta) queue_redraw() - var right : Structure = get_parent().get_relative(Vector2.RIGHT) - if right == null or right.inventory.can_add() == false: + var next : Structure = get_next() + if next == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,parent_structure.facing)) == false: return var popped = inventory.pop() if popped == null: return - right.inventory.add(popped) + next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,parent_structure.facing)) + +func calculate_position(index: int) -> Vector2: + var indexed_part = 16.0 / inventory.capacity + match parent_structure.facing: + Structure.Facing.RIGHT: + return Vector2.LEFT*8 + Vector2.RIGHT * indexed_part * (index + inventory.progress_array[index]) + Structure.Facing.DOWN: + return Vector2.UP*8 + Vector2.DOWN * indexed_part * (index + inventory.progress_array[index]) + Structure.Facing.LEFT: + return Vector2.RIGHT*8 + Vector2.LEFT * indexed_part * (index + inventory.progress_array[index]) + Structure.Facing.UP: + return Vector2.DOWN*8 + Vector2.UP * indexed_part * (index + inventory.progress_array[index]) + return Vector2.ZERO + +func get_next() -> Structure: + match parent_structure.facing: + Structure.Facing.RIGHT: + return parent_structure.get_relative(Vector2(1,0)) + Structure.Facing.DOWN: + return parent_structure.get_relative(Vector2(0,1)) + Structure.Facing.LEFT: + return parent_structure.get_relative(Vector2(-1,0)) + Structure.Facing.UP: + return parent_structure.get_relative(Vector2(0,-1)) + return null + +func _on_conveyor_switched_facing(to: Structure.Facing) -> void: + match to: + Structure.Facing.RIGHT: + animator.play("right") + Structure.Facing.DOWN: + animator.play("down") + Structure.Facing.LEFT: + animator.play("left") + Structure.Facing.UP: + animator.play("up") + sync_animations() diff --git a/sprites/atlasses/Popekko.png b/sprites/atlasses/Popekko.png index 8ff06cb..dfb90d7 100644 Binary files a/sprites/atlasses/Popekko.png and b/sprites/atlasses/Popekko.png differ diff --git a/sprites/atlasses/Popekko.png.import b/sprites/atlasses/Popekko.png.import index f2b9199..4dc9be4 100644 --- a/sprites/atlasses/Popekko.png.import +++ b/sprites/atlasses/Popekko.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://dinyjq8853usn" +uid="uid://gfkhedfdi7ug" path="res://.godot/imported/Popekko.png-5c34a5f2a8c85fcf59453310fa6553d9.ctex" metadata={ "vram_texture": false