From 7819007b01b51ad78809f3ce4b88ece2d9ca569d Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Fri, 27 Sep 2024 16:49:56 -0700 Subject: [PATCH] Add initial tile scenes Update script to dynamically find paths to follow --- Tiles/Tile1.tscn | 41 +++++++++++++++++++ Tiles/TileRamp1.tscn | 42 ++++++++++++++++++++ Tiles/TileTurn1.tscn | 41 +++++++++++++++++++ character/character.gd | 31 +++++++-------- project.godot | 4 ++ utils.gd | 17 ++++++++ world.gd | 43 ++++++++++---------- world.tscn | 89 ++++++++++-------------------------------- 8 files changed, 200 insertions(+), 108 deletions(-) create mode 100644 Tiles/Tile1.tscn create mode 100644 Tiles/TileRamp1.tscn create mode 100644 Tiles/TileTurn1.tscn create mode 100644 utils.gd diff --git a/Tiles/Tile1.tscn b/Tiles/Tile1.tscn new file mode 100644 index 0000000..d126df2 --- /dev/null +++ b/Tiles/Tile1.tscn @@ -0,0 +1,41 @@ +[gd_scene load_steps=5 format=3 uid="uid://cvtgigpmwwv4g"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] +cull_mode = 2 + +[sub_resource type="PlaneMesh" id="PlaneMesh_jju0s"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(10, 10) + +[sub_resource type="PlaneMesh" id="PlaneMesh_rsriu"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(2, 10) + +[sub_resource type="Curve3D" id="Curve3D_q1k88"] +_data = { +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, -5), +"tilts": PackedFloat32Array(0, 0) +} +point_count = 2 + +[node name="Tile1" type="Node3D"] + +[node name="Floor" type="MeshInstance3D" parent="."] +mesh = SubResource("PlaneMesh_jju0s") + +[node name="Wall1" type="MeshInstance3D" parent="."] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, -5, 1, 0) +mesh = SubResource("PlaneMesh_rsriu") + +[node name="Wall2" type="MeshInstance3D" parent="."] +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="."] +curve = SubResource("Curve3D_q1k88") + +[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) +rotation_mode = 1 +loop = false +tilt_enabled = false diff --git a/Tiles/TileRamp1.tscn b/Tiles/TileRamp1.tscn new file mode 100644 index 0000000..dbfef3e --- /dev/null +++ b/Tiles/TileRamp1.tscn @@ -0,0 +1,42 @@ +[gd_scene load_steps=5 format=3 uid="uid://fafw4evt372"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] +cull_mode = 2 + +[sub_resource type="PlaneMesh" id="PlaneMesh_jju0s"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(10, 10) + +[sub_resource type="PlaneMesh" id="PlaneMesh_rsriu"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(2, 10) + +[sub_resource type="Curve3D" id="Curve3D_q1k88"] +_data = { +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, -5), +"tilts": PackedFloat32Array(0, 0) +} +point_count = 2 + +[node name="Tile1" type="Node3D"] +transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 0, 0) + +[node name="Floor" type="MeshInstance3D" parent="."] +mesh = SubResource("PlaneMesh_jju0s") + +[node name="Wall1" type="MeshInstance3D" parent="."] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, -5, 1, 0) +mesh = SubResource("PlaneMesh_rsriu") + +[node name="Wall2" type="MeshInstance3D" parent="."] +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="."] +curve = SubResource("Curve3D_q1k88") + +[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) +rotation_mode = 1 +loop = false +tilt_enabled = false diff --git a/Tiles/TileTurn1.tscn b/Tiles/TileTurn1.tscn new file mode 100644 index 0000000..db86cb5 --- /dev/null +++ b/Tiles/TileTurn1.tscn @@ -0,0 +1,41 @@ +[gd_scene load_steps=5 format=3 uid="uid://b88nuwe42bhc1"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_cwetn"] +cull_mode = 2 + +[sub_resource type="PlaneMesh" id="PlaneMesh_jju0s"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(10, 10) + +[sub_resource type="PlaneMesh" id="PlaneMesh_rsriu"] +material = SubResource("StandardMaterial3D_cwetn") +size = Vector2(2, 10) + +[sub_resource type="Curve3D" id="Curve3D_q1k88"] +_data = { +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0), +"tilts": PackedFloat32Array(0, 0, 0) +} +point_count = 3 + +[node name="Tile1" type="Node3D"] + +[node name="Floor" type="MeshInstance3D" parent="."] +mesh = SubResource("PlaneMesh_jju0s") + +[node name="Wall1" type="MeshInstance3D" parent="."] +transform = Transform3D(-4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, -5, 1, 0) +mesh = SubResource("PlaneMesh_rsriu") + +[node name="Wall2" type="MeshInstance3D" parent="."] +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="."] +curve = SubResource("Curve3D_q1k88") + +[node name="PathFollow3D" type="PathFollow3D" parent="Path3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) +rotation_mode = 1 +loop = false +tilt_enabled = false diff --git a/character/character.gd b/character/character.gd index e3290ff..185b8b6 100644 --- a/character/character.gd +++ b/character/character.gd @@ -1,10 +1,10 @@ -@tool extends Area3D +class_name Character @export var path: Path3D: set = set_path -@export var speed: float = 5.0 +@export var speed: float = 10.0 var follow: PathFollow3D var marker: Marker3D @@ -22,16 +22,12 @@ func _process(delta): if path == null: return global_position = marker.global_position + global_rotation = marker.global_rotation if follow.progress_ratio >= 1: return var travel = speed * delta follow.progress += travel total_distance += travel - #if is_equal_approx(follow.progress, change_distance): - #path = $Path3D2 - #follow = path.get_node(("Follow")) - #change_distance = path.curve.get_baked_length() - #marker.reparent(follow) func set_path(new_path: Path3D): @@ -39,13 +35,14 @@ func set_path(new_path: Path3D): printerr("path must be a Path3D") return path = new_path - follow = path.get_node("Follow") - marker = follow.get_node("Marker") - update_configuration_warnings() - - -func _get_configuration_warnings() -> PackedStringArray: - var warnings = [] - if path == null or path is not Path3D: - warnings.append("path must be set to a Path3D.") - return warnings + follow = Utils.find_first_node_of_type(path, 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) + if marker == null: + marker = Marker3D.new() + follow.add_child(marker) diff --git a/project.godot b/project.godot index 41da171..70d2b9a 100644 --- a/project.godot +++ b/project.godot @@ -14,6 +14,10 @@ config/name="Fantasy Run" config/features=PackedStringArray("4.3", "Mobile") config/icon="res://icon.svg" +[autoload] + +Utils="*res://utils.gd" + [rendering] renderer/rendering_method="mobile" diff --git a/utils.gd b/utils.gd new file mode 100644 index 0000000..3584b17 --- /dev/null +++ b/utils.gd @@ -0,0 +1,17 @@ +extends Node + +func find_first_node_of_type(node: Node, type: Variant): + 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): + 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)) + return nodes diff --git a/world.gd b/world.gd index cb47d17..9e51e38 100644 --- a/world.gd +++ b/world.gd @@ -1,32 +1,29 @@ extends Node3D -@export var speed: float = 10 -@onready var path_3d_2: Path3D = $Path3D2 @onready var path: Path3D = $Path -@onready var follow: PathFollow3D = $Path/Follow @onready var marker: Marker3D = $Path/Follow/Marker +@onready var character: Character = $Character -var points_added = false - -var point_removed = false var change_distance: float -var total_distance: float = 0 +var paths: Array +var current_path: int = 0 + -# Called when the node enters the scene tree for the first time. func _ready() -> void: - change_distance = path.curve.get_baked_length() - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - %Character.global_position = marker.global_position - if follow.progress_ratio >= 1: + paths = Utils.get_nodes_of_class(self, Path3D) + if paths.size() == 0: + printerr("There are no Path3Ds in the scene.") return - var travel = speed * delta - follow.progress += travel - total_distance += travel - if is_equal_approx(follow.progress, change_distance): - path = $Path3D2 - follow = path.get_node(("Follow")) - change_distance = path.curve.get_baked_length() - marker.reparent(follow) + change_distance = paths[current_path].curve.get_baked_length() + if character.path == null: + character.path = paths[current_path] + + +func _process(delta: float) -> void: + if is_equal_approx(character.follow.progress, change_distance): + current_path += 1 + if current_path >= paths.size(): + return + marker.reparent(paths[current_path]) + character.path = paths[current_path] + change_distance = paths[current_path].curve.get_baked_length() diff --git a/world.tscn b/world.tscn index e26630e..b255dd3 100644 --- a/world.tscn +++ b/world.tscn @@ -1,79 +1,32 @@ -[gd_scene load_steps=9 format=3 uid="uid://blupjt2ulo2g1"] +[gd_scene load_steps=6 format=3 uid="uid://blupjt2ulo2g1"] [ext_resource type="PackedScene" uid="uid://vamte52rvfhy" path="res://character/character.tscn" id="1_nfwx6"] [ext_resource type="Script" path="res://world.gd" id="1_oloil"] - -[sub_resource type="PlaneMesh" id="PlaneMesh_okds2"] -size = Vector2(10, 10) - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gqvdt"] -cull_mode = 2 -albedo_color = Color(1, 0, 0, 1) - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2ipub"] -cull_mode = 2 -albedo_color = Color(0, 1, 0, 1) - -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_k12sw"] -cull_mode = 2 -albedo_color = Color(0, 0, 1, 1) - -[sub_resource type="Curve3D" id="Curve3D_5ttj7"] -_data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0.425, 4.6, 0, 0, 0, 0, 0, 0, 0, 0.47, -4.745, 0, 0, 0, 0, 0, 0, 0, 2.84942, -14.0262), -"tilts": PackedFloat32Array(0, 0, 0) -} -point_count = 3 - -[sub_resource type="Curve3D" id="Curve3D_3mog2"] -_data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 2.8487, -14.2054, 0, 0, 0, 0, 0, 0, 0, 2.81399, -24.5989, 0, 0, 0, 0, 0, 0, -0.0390603, 2.79813, -35.8309), -"tilts": PackedFloat32Array(0, 0, 0) -} -point_count = 3 +[ext_resource type="PackedScene" uid="uid://cvtgigpmwwv4g" path="res://Tiles/Tile1.tscn" id="3_vddfn"] +[ext_resource type="PackedScene" uid="uid://fafw4evt372" path="res://Tiles/TileRamp1.tscn" id="4_uxk3v"] +[ext_resource type="PackedScene" uid="uid://b88nuwe42bhc1" path="res://Tiles/TileTurn1.tscn" id="5_ynp1e"] [node name="World" type="Node3D"] script = ExtResource("1_oloil") -[node name="MeshInstance3D" type="MeshInstance3D" parent="."] -mesh = SubResource("PlaneMesh_okds2") - -[node name="MeshInstance3D2" type="MeshInstance3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.978896, -0.204359, 0, 0.204359, 0.978896, 0, 0.696645, -10.0891) -material_override = SubResource("StandardMaterial3D_gqvdt") -mesh = SubResource("PlaneMesh_okds2") - -[node name="MeshInstance3D3" type="MeshInstance3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5446, -20.8633) -material_override = SubResource("StandardMaterial3D_2ipub") -mesh = SubResource("PlaneMesh_okds2") - -[node name="MeshInstance3D4" type="MeshInstance3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5446, -31.7451) -material_override = SubResource("StandardMaterial3D_k12sw") -mesh = SubResource("PlaneMesh_okds2") - [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] transform = Transform3D(-0.827728, -0.0513662, 0.558773, 0.136568, 0.947417, 0.289396, -0.544256, 0.315852, -0.777189, 3.45982, 2.03445, -6.78341) -[node name="Path" type="Path3D" parent="."] -curve = SubResource("Curve3D_5ttj7") - -[node name="Follow" type="PathFollow3D" parent="Path"] -transform = Transform3D(1, 0, 0, 0, 0.999858, 0, 0, 0, 0.999858, 0, 0.425, 4.6) -rotation_mode = 0 -loop = false -tilt_enabled = false - -[node name="Marker" type="Marker3D" parent="Path/Follow"] - -[node name="Path3D2" type="Path3D" parent="."] -curve = SubResource("Curve3D_3mog2") - -[node name="Follow" type="PathFollow3D" parent="Path3D2"] -transform = Transform3D(1, 0, 0, 0, 0.999858, 0, 0, 0, 0.999858, 0, 2.8487, -14.2054) -rotation_mode = 0 -loop = false -tilt_enabled = false - [node name="Character" parent="." instance=ExtResource("1_nfwx6")] + +[node name="Tile1" parent="." instance=ExtResource("3_vddfn")] + +[node name="Tile2" parent="." instance=ExtResource("3_vddfn")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -10) + +[node name="Tile3" parent="." instance=ExtResource("4_uxk3v")] +transform = Transform3D(1, 0, 0, 0, 0.866025, -0.5, 0, 0.5, 0.866025, 0, 2.49484, -19.3201) + +[node name="Tile4" parent="." instance=ExtResource("3_vddfn")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5, -28.6473) + +[node name="Tile5" parent="." instance=ExtResource("5_ynp1e")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5, -38.641) + +[node name="Tile6" parent="." instance=ExtResource("3_vddfn")] +transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 10, 5, -38.6473)