From 05d285a750a29910f75b95fb388fa09e3c0ef0e0 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Mon, 30 Sep 2024 20:50:05 -0700 Subject: [PATCH] Update typing Add Tile class --- Tiles/Tile.gd | 17 +++++++++++++++++ Tiles/Tile1.tscn | 9 ++++++--- Tiles/TileRamp1.tscn | 10 ++++++---- Tiles/TileTurn1.tscn | 9 ++++++--- character/character.gd | 12 ++++++------ project.godot | 1 + utils.gd | 12 ++++++------ world.gd | 25 ++++++++++++------------- 8 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 Tiles/Tile.gd diff --git a/Tiles/Tile.gd b/Tiles/Tile.gd new file mode 100644 index 0000000..4f368cf --- /dev/null +++ b/Tiles/Tile.gd @@ -0,0 +1,17 @@ +extends Node3D +class_name Tile + + +@onready var path = $MainPath as Path3D +@onready var follow = $MainPath/Follow as PathFollow3D + +var length: float: + get: + return path.curve.get_baked_length() as float + +func _ready(): + pass # Replace with function body. + + +func _process(delta): + pass diff --git a/Tiles/Tile1.tscn b/Tiles/Tile1.tscn index d126df2..2b3dd77 100644 --- a/Tiles/Tile1.tscn +++ b/Tiles/Tile1.tscn @@ -1,4 +1,6 @@ -[gd_scene load_steps=5 format=3 uid="uid://cvtgigpmwwv4g"] +[gd_scene load_steps=6 format=3 uid="uid://cvtgigpmwwv4g"] + +[ext_resource type="Script" path="res://Tiles/Tile.gd" id="1_wu32c"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] cull_mode = 2 @@ -19,6 +21,7 @@ _data = { point_count = 2 [node name="Tile1" type="Node3D"] +script = ExtResource("1_wu32c") [node name="Floor" type="MeshInstance3D" parent="."] mesh = SubResource("PlaneMesh_jju0s") @@ -31,10 +34,10 @@ mesh = SubResource("PlaneMesh_rsriu") transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 5, 1, 0) mesh = SubResource("PlaneMesh_rsriu") -[node name="Path3D" type="Path3D" parent="."] +[node name="MainPath" type="Path3D" parent="."] curve = SubResource("Curve3D_q1k88") -[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +[node name="Follow" type="PathFollow3D" parent="MainPath"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) rotation_mode = 1 loop = false diff --git a/Tiles/TileRamp1.tscn b/Tiles/TileRamp1.tscn index 1037020..7b25017 100644 --- a/Tiles/TileRamp1.tscn +++ b/Tiles/TileRamp1.tscn @@ -1,4 +1,6 @@ -[gd_scene load_steps=5 format=3 uid="uid://fafw4evt372"] +[gd_scene load_steps=6 format=3 uid="uid://fafw4evt372"] + +[ext_resource type="Script" path="res://Tiles/Tile.gd" id="1_qgvt0"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] cull_mode = 2 @@ -19,7 +21,7 @@ _data = { point_count = 2 [node name="Tile1" type="Node3D"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) +script = ExtResource("1_qgvt0") [node name="Floor" type="MeshInstance3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 0.0893164, -0.333333) @@ -33,10 +35,10 @@ mesh = SubResource("PlaneMesh_rsriu") transform = Transform3D(-4.37114e-08, 1, 0, -0.866025, -3.78552e-08, -0.5, -0.5, -2.18557e-08, 0.866025, 5, 0.955342, 0.166667) mesh = SubResource("PlaneMesh_rsriu") -[node name="Path3D" type="Path3D" parent="."] +[node name="MainPath" type="Path3D" parent="."] curve = SubResource("Curve3D_q1k88") -[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +[node name="Follow" type="PathFollow3D" parent="MainPath"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2.40419, 3.96713) rotation_mode = 1 loop = false diff --git a/Tiles/TileTurn1.tscn b/Tiles/TileTurn1.tscn index db86cb5..2bbaec1 100644 --- a/Tiles/TileTurn1.tscn +++ b/Tiles/TileTurn1.tscn @@ -1,4 +1,6 @@ -[gd_scene load_steps=5 format=3 uid="uid://b88nuwe42bhc1"] +[gd_scene load_steps=6 format=3 uid="uid://b88nuwe42bhc1"] + +[ext_resource type="Script" path="res://Tiles/Tile.gd" id="1_6m2c8"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] cull_mode = 2 @@ -19,6 +21,7 @@ _data = { point_count = 3 [node name="Tile1" type="Node3D"] +script = ExtResource("1_6m2c8") [node name="Floor" type="MeshInstance3D" parent="."] mesh = SubResource("PlaneMesh_jju0s") @@ -31,10 +34,10 @@ mesh = SubResource("PlaneMesh_rsriu") transform = Transform3D(1.91069e-15, -4.37114e-08, 1, -1, -4.37114e-08, 0, 4.37114e-08, -1, -4.37114e-08, 0, 1, -5) mesh = SubResource("PlaneMesh_rsriu") -[node name="Path3D" type="Path3D" parent="."] +[node name="MainPath" type="Path3D" parent="."] curve = SubResource("Curve3D_q1k88") -[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +[node name="Follow" type="PathFollow3D" parent="MainPath"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) rotation_mode = 1 loop = false diff --git a/character/character.gd b/character/character.gd index 185b8b6..0b39356 100644 --- a/character/character.gd +++ b/character/character.gd @@ -14,18 +14,18 @@ func _ready() -> void: if path == null or path is not Path3D: printerr("path is not set to a Path3D") return - follow = path.get_node("Follow") - marker = follow.get_node("Marker") + follow = path.get_node("Follow") as PathFollow3D + marker = follow.get_node("Marker") as Marker3D -func _process(delta): +func _process(delta: float): if path == null: return global_position = marker.global_position global_rotation = marker.global_rotation if follow.progress_ratio >= 1: return - var travel = speed * delta + var travel: float = speed * delta follow.progress += travel total_distance += travel @@ -35,14 +35,14 @@ func set_path(new_path: Path3D): printerr("path must be a Path3D") return path = new_path - follow = Utils.find_first_node_of_type(path, PathFollow3D) + follow = Utils.find_first_node_of_type(path, PathFollow3D) as PathFollow3D if follow == null: follow = PathFollow3D.new() path.add_child(follow) marker = Marker3D.new() follow.add_child(marker) return - marker = Utils.find_first_node_of_type(follow, Marker3D) + marker = Utils.find_first_node_of_type(follow, Marker3D) as Marker3D if marker == null: marker = Marker3D.new() follow.add_child(marker) diff --git a/project.godot b/project.godot index 70d2b9a..bb3738b 100644 --- a/project.godot +++ b/project.godot @@ -11,6 +11,7 @@ config_version=5 [application] config/name="Fantasy Run" +run/main_scene="res://world.tscn" config/features=PackedStringArray("4.3", "Mobile") config/icon="res://icon.svg" diff --git a/utils.gd b/utils.gd index 3584b17..0853015 100644 --- a/utils.gd +++ b/utils.gd @@ -1,17 +1,17 @@ extends Node -func find_first_node_of_type(node: Node, type: Variant): +func find_first_node_of_type(node: Node, type: Variant) -> Node: for child in node.get_children(): if is_instance_of(child, type): return child return null -func get_nodes_of_class(node: Node, type: Variant) -> Array: - var nodes = [] - for child in node.get_children(true): +func get_nodes_of_class(node: Node, type: Variant, include_internal: bool = false) -> Array[Variant]: + var nodes: Array[Variant] = [] + for child in node.get_children(include_internal): if is_instance_of(child, type): nodes.append(child) - elif child.get_child_count(true) > 0: - nodes.append_array(get_nodes_of_class(child, type)) + elif child.get_child_count(include_internal) > 0: + nodes.append_array(get_nodes_of_class(child, type, include_internal)) return nodes diff --git a/world.gd b/world.gd index 485f48d..c667692 100644 --- a/world.gd +++ b/world.gd @@ -1,27 +1,26 @@ extends Node3D -@onready var path: Path3D = $Path -@onready var character: Character = $Character +@onready var character = $Character as Character var change_distance: float -var paths: Array -var current_path: int = 0 +var tiles: Array[Tile] +var current_tile: int = 0 func _ready() -> void: - paths = Utils.get_nodes_of_class(self, Path3D) - if paths.size() == 0: - printerr("There are no Path3Ds in the scene.") + tiles.assign(Utils.get_nodes_of_class(self, Tile)) + if tiles.size() == 0: + printerr("There are no Tiles in the scene.") return - change_distance = paths[current_path].curve.get_baked_length() + change_distance = tiles[current_tile].length if character.path == null: - character.path = paths[current_path] + character.path = tiles[current_tile].path func _process(delta: float) -> void: if is_equal_approx(character.follow.progress, change_distance): - current_path += 1 - if current_path >= paths.size(): + current_tile += 1 + if current_tile >= tiles.size(): return - character.path = paths[current_path] - change_distance = paths[current_path].curve.get_baked_length() + character.path = tiles[current_tile].path + change_distance = tiles[current_tile].length