From d6da1268d8963b140af0b751792652f5d91d94c2 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Fri, 4 Oct 2024 10:01:33 -0700 Subject: [PATCH] Update to make scripts use a Tile instead of a Path3D --- Tiles/Tile.gd | 35 +++++++++++++++++++++++--------- Tiles/Tile1.tscn | 2 ++ Tiles/TileRamp1.tscn | 2 ++ Tiles/TileTurn.gd | 36 ++++++++++++--------------------- Tiles/TileTurn1.tscn | 23 ++++++++++++--------- character/character.gd | 46 ++++++++++++++---------------------------- utils.gd | 2 +- world.gd | 29 +++++++++++++++++--------- world.tscn | 3 ++- 9 files changed, 94 insertions(+), 84 deletions(-) diff --git a/Tiles/Tile.gd b/Tiles/Tile.gd index c301981..63857e6 100644 --- a/Tiles/Tile.gd +++ b/Tiles/Tile.gd @@ -2,28 +2,45 @@ extends Node3D class_name Tile -@onready var path = $MainPath as Path3D @onready var follow = $MainPath/Follow as PathFollow3D @onready var spawn_point = $SpawnPoint as Marker3D +@onready var progress_marker = $MainPath/Follow/ProgressMarker as Marker3D + +var paths: Array[Path3D] +var path_index: int = 0 + +var path: Path3D: + get = get_current_path + +var marker_position: Vector3: + get: + return progress_marker.global_position + +var marker_rotation: Vector3: + get: + return progress_marker.global_rotation -var length: float: - get = get_length func _ready(): - pass # Replace with function body. + paths.append($MainPath) func _process(delta): - pass + if is_equal_approx(follow.progress_ratio, 1) and has_next_path(): + path_index += 1 + follow = Utils.get_first_node_of_type(path, PathFollow3D) as PathFollow3D + progress_marker.reparent(follow) -func get_length(): - return path.curve.get_baked_length() +func get_current_path() -> Path3D: + return paths[path_index] func has_next_path() -> bool: - return false + return path_index + 1 < paths.size() -func get_next_path(): +func get_next_path() -> Path3D: + if has_next_path(): + return paths[path_index+1] return null diff --git a/Tiles/Tile1.tscn b/Tiles/Tile1.tscn index 6298701..3f8aaf9 100644 --- a/Tiles/Tile1.tscn +++ b/Tiles/Tile1.tscn @@ -45,3 +45,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5) rotation_mode = 1 loop = false tilt_enabled = false + +[node name="ProgressMarker" type="Marker3D" parent="MainPath/Follow"] diff --git a/Tiles/TileRamp1.tscn b/Tiles/TileRamp1.tscn index 6aab525..f53a188 100644 --- a/Tiles/TileRamp1.tscn +++ b/Tiles/TileRamp1.tscn @@ -46,3 +46,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2.40419, 3.96713) rotation_mode = 1 loop = false tilt_enabled = false + +[node name="ProgressMarker" type="Marker3D" parent="MainPath/Follow"] diff --git a/Tiles/TileTurn.gd b/Tiles/TileTurn.gd index 9d1e277..7873bb4 100644 --- a/Tiles/TileTurn.gd +++ b/Tiles/TileTurn.gd @@ -2,18 +2,18 @@ extends Tile class_name TileTurn @onready var area_3d = $Area3D as Area3D -@onready var main_path = $MainPath as Path3D -@onready var main_follow = $MainPath/Follow as PathFollow3D @onready var path_forward = $PathForward as Path3D @onready var path_right = $PathRight as Path3D @onready var path_left = $PathLeft as Path3D + var connected = false var current_direction = "forward" var on_main_path = true func _ready(): - pass + paths.append($MainPath) + paths.append($PathForward) func _process(delta): @@ -22,33 +22,23 @@ func _process(delta): var character = area as Character character.turn.connect(turn) connected = true - if main_follow.progress_ratio >= 1 and on_main_path: - on_main_path = false - var marker = Utils.find_first_node_of_type(main_follow, Marker3D) - if current_direction == "forward" and path_forward != null: - marker.reparent($PathForward/Follow) - elif current_direction == "right" and path_right != null: - marker.reparent($PathRight/Follow) - elif current_direction == "left" and path_left != null: - marker.reparent($PathLeft/Follow) + super(delta) func turn(direction): - current_direction = direction + if on_main_path: + current_direction = direction + paths[path_index+1] = get_path_by_direction(current_direction) -func get_length(): - return main_path.curve.get_baked_length() + path_forward.curve.get_baked_length() +#func has_next_path() -> bool: + #return on_main_path -func has_next_path() -> bool: - return on_main_path - - -func get_next_path() -> Path3D: - if not on_main_path: - return get_path_by_direction(current_direction) - return null +#func get_next_path() -> Path3D: + #if not on_main_path: + #return get_path_by_direction(current_direction) + #return null func get_path_by_direction(direction) -> Path3D: diff --git a/Tiles/TileTurn1.tscn b/Tiles/TileTurn1.tscn index 7f97db1..fe98e73 100644 --- a/Tiles/TileTurn1.tscn +++ b/Tiles/TileTurn1.tscn @@ -15,31 +15,31 @@ 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), +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 1), "tilts": PackedFloat32Array(0, 0) } point_count = 2 [sub_resource type="Curve3D" id="Curve3D_5ouye"] _data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -5), +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -5), "tilts": PackedFloat32Array(0, 0) } point_count = 2 [sub_resource type="Curve3D" id="Curve3D_dgywf"] _data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0), -"tilts": PackedFloat32Array(0, 0) +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 1, 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 = 2 +point_count = 3 [sub_resource type="Curve3D" id="Curve3D_25twk"] _data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -5, 0, 0), -"tilts": PackedFloat32Array(0, 0) +"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, 1, 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 = 2 +point_count = 3 [sub_resource type="BoxShape3D" id="BoxShape3D_uvh3q"] size = Vector3(9.80943, 5.486, 17.6183) @@ -70,10 +70,13 @@ rotation_mode = 1 loop = false tilt_enabled = false +[node name="ProgressMarker" type="Marker3D" parent="MainPath/Follow"] + [node name="PathForward" type="Path3D" parent="."] curve = SubResource("Curve3D_5ouye") [node name="Follow" type="PathFollow3D" parent="PathForward"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1) rotation_mode = 1 loop = false tilt_enabled = false @@ -82,7 +85,7 @@ tilt_enabled = false curve = SubResource("Curve3D_dgywf") [node name="Follow" type="PathFollow3D" parent="PathRight"] -transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 0, 0) +transform = Transform3D(0.999999, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 0, 1) rotation_mode = 1 loop = false tilt_enabled = false @@ -91,7 +94,7 @@ tilt_enabled = false curve = SubResource("Curve3D_25twk") [node name="Follow" type="PathFollow3D" parent="PathLeft"] -transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, 0) +transform = Transform3D(0.999999, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 0, 1) rotation_mode = 1 loop = false tilt_enabled = false diff --git a/character/character.gd b/character/character.gd index 4603193..db4df0d 100644 --- a/character/character.gd +++ b/character/character.gd @@ -3,47 +3,31 @@ class_name Character signal turn(direction) -@export var path: Path3D: - set = set_path @export var speed: float = 10.0 +@export var tile: Tile -var follow: PathFollow3D -var marker: Marker3D var total_distance: float = 0.0 + func _ready() -> void: - if path == null or path is not Path3D: - printerr("path is not set to a Path3D") + if tile == null or tile is not Tile: + printerr("tile is not set to a Tile") return - follow = Utils.find_first_node_of_type(path, PathFollow3D) as PathFollow3D - marker = Utils.find_first_node_of_type(path, Marker3D) as Marker3D func _process(delta: float): - if path == null: + if tile == null: return - global_position = marker.global_position - global_rotation = marker.global_rotation - if follow.progress_ratio >= 1: + + if Input.is_action_just_pressed("ui_right"): + turn.emit("right") + if Input.is_action_just_pressed("ui_left"): + turn.emit("left") + + global_position = tile.marker_position + global_rotation = tile.marker_rotation + if tile.follow.progress_ratio >= 1: return var travel: float = speed * delta - follow.progress += travel + tile.follow.progress += travel total_distance += travel - - -func set_path(new_path: Path3D): - if new_path is not Path3D: - printerr("path must be a Path3D") - return - path = new_path - 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) as Marker3D - if marker == null: - marker = Marker3D.new() - follow.add_child(marker) diff --git a/utils.gd b/utils.gd index 0853015..0431f80 100644 --- a/utils.gd +++ b/utils.gd @@ -1,6 +1,6 @@ extends Node -func find_first_node_of_type(node: Node, type: Variant) -> Node: +func get_first_node_of_type(node: Node, type: Variant) -> Node: for child in node.get_children(): if is_instance_of(child, type): return child diff --git a/world.gd b/world.gd index 769dd9b..6b73839 100644 --- a/world.gd +++ b/world.gd @@ -4,7 +4,10 @@ extends Node3D var change_distance: float var tiles: Array[Tile] -var current_tile: int = 0 +var tile_index: int = 0 + +var current_tile: Tile: + get = get_current_tile func _ready() -> void: @@ -12,15 +15,23 @@ func _ready() -> void: if tiles.size() == 0: printerr("There are no Tiles in the scene.") return - #change_distance = tiles[current_tile].length - if character.path == null: - character.path = tiles[current_tile].path + if character.tile == null: + character.tile = current_tile func _process(delta: float) -> void: - if is_equal_approx(character.follow.progress_ratio, 1): - current_tile += 1 - if current_tile >= tiles.size(): + if tiles.size() == 0: + return + if is_equal_approx(current_tile.follow.progress_ratio, 1) and has_next_tile(): + if current_tile.has_next_path(): return - character.path = tiles[current_tile].path - #change_distance = tiles[current_tile].length + tile_index += 1 + character.tile = current_tile + + +func has_next_tile() -> bool: + return tile_index + 1 < tiles.size() + + +func get_current_tile() -> Tile: + return tiles[tile_index] diff --git a/world.tscn b/world.tscn index a2096e8..b664e58 100644 --- a/world.tscn +++ b/world.tscn @@ -13,7 +13,8 @@ script = ExtResource("1_oloil") transform = Transform3D(0.763207, -0.546639, 0.344531, 0.296667, 0.770123, 0.564712, -0.574024, -0.328781, 0.749933, 3.45982, 2.03445, -6.78341) shadow_enabled = true -[node name="Character" parent="." instance=ExtResource("1_nfwx6")] +[node name="Character" parent="." node_paths=PackedStringArray("tile") instance=ExtResource("1_nfwx6")] +tile = NodePath("../Tile1") [node name="Tile1" parent="." instance=ExtResource("3_vddfn")]