From 552d30d33a15138ec3b6bfba3ae6e89d0648925b Mon Sep 17 00:00:00 2001 From: Ross M Karchner Date: Wed, 14 Aug 2024 06:53:16 -0400 Subject: [PATCH] pieces now spawn one at a time --- scenes/GameScene/game.gd | 26 +++++++++++++++++ scenes/GameScene/game.tscn | 58 +++++++------------------------------ scenes/GameScene/segment.gd | 12 ++++++-- scenes/Levels/level_1.tscn | 55 +++++++++++++++++++++++++++++++++++ scenes/gamepiece_spawner.gd | 23 +++++++++++++++ 5 files changed, 124 insertions(+), 50 deletions(-) create mode 100644 scenes/GameScene/game.gd create mode 100644 scenes/Levels/level_1.tscn create mode 100644 scenes/gamepiece_spawner.gd diff --git a/scenes/GameScene/game.gd b/scenes/GameScene/game.gd new file mode 100644 index 0000000..5b91d0f --- /dev/null +++ b/scenes/GameScene/game.gd @@ -0,0 +1,26 @@ +extends Node2D + +@export var levels:Array[PackedScene] + +var current_level = 0 +var current_level_instance = null + +@onready var piece_spawner = $GamepieceSpawner + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + load_current_level() + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass + + +func load_current_level(): + if not current_level_instance: + current_level_instance = levels[current_level].instantiate() + add_child(current_level_instance) + piece_spawner.add_segments_to_queue(current_level_instance.get_node("PossibleSolution").get_children()) + piece_spawner.reveal_next_piece() + diff --git a/scenes/GameScene/game.tscn b/scenes/GameScene/game.tscn index ad11689..78b9ed0 100644 --- a/scenes/GameScene/game.tscn +++ b/scenes/GameScene/game.tscn @@ -1,53 +1,17 @@ -[gd_scene load_steps=7 format=4 uid="uid://drwnwutwrrpb0"] +[gd_scene load_steps=4 format=3 uid="uid://drwnwutwrrpb0"] -[ext_resource type="PackedScene" uid="uid://cy1uuyuh3e44f" path="res://scenes/GameScene/load.tscn" id="2_6vgst"] -[ext_resource type="PackedScene" uid="uid://73fcyu51g72" path="res://scenes/power_source.tscn" id="2_efwgb"] -[ext_resource type="PackedScene" uid="uid://cfrdeamt1c5dy" path="res://scenes/GameScene/segment.tscn" id="4_cmlyv"] -[ext_resource type="PackedScene" uid="uid://cp11itxbkl8u2" path="res://scenes/drop_area.tscn" id="6_dvwn4"] - -[sub_resource type="TileSetScenesCollectionSource" id="TileSetScenesCollectionSource_0044b"] -resource_name = "Map" -scenes/1/scene = ExtResource("6_dvwn4") -scenes/2/scene = ExtResource("2_efwgb") -scenes/3/scene = ExtResource("2_6vgst") - -[sub_resource type="TileSet" id="TileSet_28ise"] -tile_shape = 3 -tile_size = Vector2i(94, 94) -sources/0 = SubResource("TileSetScenesCollectionSource_0044b") +[ext_resource type="PackedScene" uid="uid://j34kskfwf7ku" path="res://scenes/Levels/level_1.tscn" id="1_cw4a4"] +[ext_resource type="Script" path="res://scenes/GameScene/game.gd" id="1_y3mbf"] +[ext_resource type="Script" path="res://scenes/gamepiece_spawner.gd" id="3_8wmyh"] [node name="Game" type="Node2D"] position = Vector2(254, 124) +script = ExtResource("1_y3mbf") +levels = Array[PackedScene]([ExtResource("1_cw4a4")]) -[node name="TileMapLayerHex" type="TileMapLayer" parent="."] -position = Vector2(-26, -18) -tile_map_data = PackedByteArray("AAD//wMAAAAAAAAAAQAAAAMAAAAAAAAAAQAAAAIAAAAAAAAAAQD//wIAAAAAAAAAAQD//wEAAAAAAAAAAQD+/wEAAAAAAAAAAQD+/wIAAAAAAAAAAgABAAIAAAAAAAAAAwD+/wMAAAAAAAAAAQD//wQAAAAAAAAAAQAAAAQAAAAAAAAAAQABAAQAAAAAAAAAAQABAAMAAAAAAAAAAQAAAAEAAAAAAAAAAQA=") -tile_set = SubResource("TileSet_28ise") - -[node name="Segment" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(-133, -53) -connection_pairs = Array[Vector2i]([Vector2i(2, -2)]) - -[node name="Segment2" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(-82, 20) -connection_pairs = Array[Vector2i]([Vector2i(5, 2)]) - -[node name="Segment3" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(63, -54) -connection_pairs = Array[Vector2i]([Vector2i(5, 3)]) - -[node name="Segment4" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(-179, 27) -connection_pairs = Array[Vector2i]([Vector2i(0, 1)]) - -[node name="Segment5" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(114, 20) -connection_pairs = Array[Vector2i]([Vector2i(4, 2)]) - -[node name="Segment6" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(-34, -55) -connection_pairs = Array[Vector2i]([Vector2i(5, 3)]) +[node name="GamepieceSpawner" type="Node2D" parent="."] +top_level = true +position = Vector2(54, 30) +script = ExtResource("3_8wmyh") -[node name="Segment7" parent="." instance=ExtResource("4_cmlyv")] -position = Vector2(17, 19) -connection_pairs = Array[Vector2i]([Vector2i(1, 0)]) +[node name="LevelContainer" type="Node2D" parent="."] diff --git a/scenes/GameScene/segment.gd b/scenes/GameScene/segment.gd index bdb0480..e5c7022 100644 --- a/scenes/GameScene/segment.gd +++ b/scenes/GameScene/segment.gd @@ -1,7 +1,10 @@ @tool extends Node2D +class_name Segment -var draggable = false +signal dropped + +var draggable = true var is_inside_dropable = false var offset: Vector2 var dragStartPosition: Vector2 @@ -87,16 +90,19 @@ func _on_click_area_mouse_exited() -> void: func _on_click_area_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void: if Engine.is_editor_hint(): return - if event.is_action_pressed("click"): + if event.is_action_pressed("click") and draggable: dragging = true offset = get_global_mouse_position() - global_position dragStartPosition =global_position Dragging.is_dragging = true - elif event.is_action_released("click"): + elif event.is_action_released("click") and draggable: dragging = false Dragging.is_dragging = false if drop_target: + drop_target.monitorable = false global_position = drop_target.global_position + dropped.emit() + $ClickArea.queue_free() drop_target = null else: global_position = dragStartPosition diff --git a/scenes/Levels/level_1.tscn b/scenes/Levels/level_1.tscn new file mode 100644 index 0000000..9f9b5de --- /dev/null +++ b/scenes/Levels/level_1.tscn @@ -0,0 +1,55 @@ +[gd_scene load_steps=7 format=4 uid="uid://j34kskfwf7ku"] + +[ext_resource type="PackedScene" uid="uid://cp11itxbkl8u2" path="res://scenes/drop_area.tscn" id="1_g2nlb"] +[ext_resource type="PackedScene" uid="uid://73fcyu51g72" path="res://scenes/power_source.tscn" id="2_spqev"] +[ext_resource type="PackedScene" uid="uid://cy1uuyuh3e44f" path="res://scenes/GameScene/load.tscn" id="3_gak4n"] +[ext_resource type="PackedScene" uid="uid://cfrdeamt1c5dy" path="res://scenes/GameScene/segment.tscn" id="4_c6ir0"] + +[sub_resource type="TileSetScenesCollectionSource" id="TileSetScenesCollectionSource_ie45y"] +resource_name = "Map" +scenes/1/scene = ExtResource("1_g2nlb") +scenes/2/scene = ExtResource("2_spqev") +scenes/3/scene = ExtResource("3_gak4n") + +[sub_resource type="TileSet" id="TileSet_f5i1a"] +tile_shape = 3 +tile_size = Vector2i(94, 94) +sources/0 = SubResource("TileSetScenesCollectionSource_ie45y") + +[node name="Level1" type="Node2D"] + +[node name="Tiles" type="TileMapLayer" parent="."] +position = Vector2(-26, -18) +tile_map_data = PackedByteArray("AAD//wMAAAAAAAAAAQAAAAMAAAAAAAAAAQAAAAIAAAAAAAAAAQD//wIAAAAAAAAAAQD//wEAAAAAAAAAAQD+/wEAAAAAAAAAAQD+/wIAAAAAAAAAAgABAAIAAAAAAAAAAwD+/wMAAAAAAAAAAQD//wQAAAAAAAAAAQAAAAQAAAAAAAAAAQABAAQAAAAAAAAAAQABAAMAAAAAAAAAAQAAAAEAAAAAAAAAAQA=") +tile_set = SubResource("TileSet_f5i1a") + +[node name="PossibleSolution" type="Node2D" parent="."] +visible = false + +[node name="Segment" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(-133, -53) +connection_pairs = Array[Vector2i]([Vector2i(2, -2)]) + +[node name="Segment2" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(-82, 20) +connection_pairs = Array[Vector2i]([Vector2i(5, 2)]) + +[node name="Segment3" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(63, -54) +connection_pairs = Array[Vector2i]([Vector2i(5, 3)]) + +[node name="Segment4" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(-179, 27) +connection_pairs = Array[Vector2i]([Vector2i(0, 1)]) + +[node name="Segment5" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(114, 20) +connection_pairs = Array[Vector2i]([Vector2i(4, 2)]) + +[node name="Segment6" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(-34, -55) +connection_pairs = Array[Vector2i]([Vector2i(5, 3)]) + +[node name="Segment7" parent="PossibleSolution" instance=ExtResource("4_c6ir0")] +position = Vector2(17, 19) +connection_pairs = Array[Vector2i]([Vector2i(1, 0)]) diff --git a/scenes/gamepiece_spawner.gd b/scenes/gamepiece_spawner.gd new file mode 100644 index 0000000..776b2fb --- /dev/null +++ b/scenes/gamepiece_spawner.gd @@ -0,0 +1,23 @@ +extends Node2D +class_name GamepieceSpawner + + +@onready var queue = Node2D.new() + + +func _ready() -> void: + queue.hide() + add_child(queue) + + +func add_segments_to_queue(pieces:Array[Node]) -> void: + pieces.shuffle() + for piece in pieces: + piece.position=Vector2.ZERO + piece.dropped.connect(reveal_next_piece) + piece.reparent(queue) + +func reveal_next_piece() -> void: + var next_piece = queue.get_child(0) + if next_piece: + next_piece.reparent(self)