From 4a39252688dae86acee54ad395a139a4cfa7dd1f Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Sat, 12 Oct 2024 16:16:43 -0700 Subject: [PATCH] add auto generation of turns --- Tiles/TileTurnLeft1.tscn | 12 +----------- Tiles/TileTurnRight1.tscn | 14 ++------------ Tiles/tile_turn.gd | 5 ++++- character/character.gd | 2 +- tile_generator.gd | 29 ++++++++++++++++++++++------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Tiles/TileTurnLeft1.tscn b/Tiles/TileTurnLeft1.tscn index 3bc2cb1..0fd785e 100644 --- a/Tiles/TileTurnLeft1.tscn +++ b/Tiles/TileTurnLeft1.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://c0xv7v6ako25a"] +[gd_scene load_steps=11 format=3 uid="uid://c0xv7v6ako25a"] [ext_resource type="Script" path="res://Tiles/tile_turn.gd" id="1_f1mql"] [ext_resource type="PackedScene" uid="uid://dh2nm1sphyfqx" path="res://Tiles/TilePath.tscn" id="2_yvi2s"] @@ -29,13 +29,6 @@ _data = { } point_count = 2 -[sub_resource type="Curve3D" id="Curve3D_f5rtl"] -_data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, 5, 0, -5), -"tilts": PackedFloat32Array(0, 0, 0) -} -point_count = 3 - [sub_resource type="Curve3D" id="Curve3D_a3s3y"] _data = { "points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, -5, 0, -5), @@ -67,9 +60,6 @@ curve = SubResource("Curve3D_r80si") [node name="PathForward" parent="." instance=ExtResource("2_yvi2s")] curve = SubResource("Curve3D_gpbgb") -[node name="PathRight" parent="." instance=ExtResource("2_yvi2s")] -curve = SubResource("Curve3D_f5rtl") - [node name="PathLeft" parent="." instance=ExtResource("2_yvi2s")] curve = SubResource("Curve3D_a3s3y") diff --git a/Tiles/TileTurnRight1.tscn b/Tiles/TileTurnRight1.tscn index 096b444..fd7391f 100644 --- a/Tiles/TileTurnRight1.tscn +++ b/Tiles/TileTurnRight1.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://b88nuwe42bhc1"] +[gd_scene load_steps=11 format=3 uid="uid://b88nuwe42bhc1"] [ext_resource type="Script" path="res://Tiles/tile_turn.gd" id="1_wj6en"] [ext_resource type="PackedScene" uid="uid://dkcjoccsgbggg" path="res://obstacles/Obstacle.tscn" id="2_tnkvo"] @@ -36,13 +36,6 @@ _data = { } point_count = 3 -[sub_resource type="Curve3D" id="Curve3D_jhtrd"] -_data = { -"points": PackedVector3Array(0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, -5, 0, -5), -"tilts": PackedFloat32Array(0, 0, 0) -} -point_count = 3 - [sub_resource type="BoxShape3D" id="BoxShape3D_uvh3q"] size = Vector3(9.80943, 5.486, 17.6183) @@ -70,11 +63,8 @@ curve = SubResource("Curve3D_gpbgb") [node name="PathRight" parent="." instance=ExtResource("3_e2l5m")] curve = SubResource("Curve3D_f5rtl") -[node name="PathLeft" parent="." instance=ExtResource("3_e2l5m")] -curve = SubResource("Curve3D_jhtrd") - [node name="SpawnPoint" type="Marker3D" parent="."] -transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 5, 0, 0) +transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 5, 0, -5) [node name="Area3D" type="Area3D" parent="."] diff --git a/Tiles/tile_turn.gd b/Tiles/tile_turn.gd index f4c13c7..edcbea1 100644 --- a/Tiles/tile_turn.gd +++ b/Tiles/tile_turn.gd @@ -38,8 +38,11 @@ func load_path() -> void: func turn(direction: String) -> void: if on_main_path: + var next_path: TilePath = get_path_by_direction(direction) + if next_path == null: + return current_direction = direction - paths[path_index+1] = get_path_by_direction(current_direction) + paths[path_index+1] = next_path func get_path_by_direction(direction: String) -> TilePath: diff --git a/character/character.gd b/character/character.gd index c3c569a..c25de93 100644 --- a/character/character.gd +++ b/character/character.gd @@ -3,7 +3,7 @@ class_name Character signal turn(direction: String) -@export var speed: float = 10.0 +@export var speed: float = 20.0 @export var tile: Tile var total_distance: float = 0.0 diff --git a/tile_generator.gd b/tile_generator.gd index 0215dac..e185fc1 100644 --- a/tile_generator.gd +++ b/tile_generator.gd @@ -7,6 +7,9 @@ var tile_index: int = 0 var run_started: bool = false var tile_parent: Node var character: Character +var min_turn: int = 3 +var max_turn: int = 10 +var turn_counter: int = randi_range(min_turn, max_turn) var current_tile: Tile: get = get_current_tile @@ -35,9 +38,7 @@ func start_run(tile_parent: Node) -> void: character = Utils.get_first_node_of_type(get_tree().root, Character) as Character get_current_tiles() if tiles.size() == 0: - var tile: Tile = generate_tile() - tiles.append(tile) - tile_parent.add_child(tile) + add_first_tile() character.tile = current_tile run_started = true @@ -48,17 +49,31 @@ func get_current_tiles() -> void: tile.left_screen.connect(tile_left_screen, CONNECT_ONE_SHOT) -func generate_tile() -> Tile: - var index = randi_range(0, tile_set.strait_tiles.size()-1) - var tile_scene: PackedScene = tile_set.strait_tiles[index] +func generate_tile(tiles: Array[PackedScene]) -> Tile: + var index = randi_range(0, tiles.size()-1) + var tile_scene: PackedScene = tiles[index] var tile: Tile = tile_scene.instantiate() as Tile tile.left_screen.connect(tile_left_screen, CONNECT_ONE_SHOT) return tile +func add_first_tile() -> void: + var tile: Tile = generate_tile(tile_set.strait_tiles) + tiles.append(tile) + tile_parent.add_child(tile) + tile.global_position = Vector3.ZERO + tile.global_rotation = Vector3.ZERO + + func add_tile() -> void: var last_tile: Tile = tiles[tiles.size()-1] - var tile: Tile = generate_tile() + var tile: Tile + if turn_counter <= 0: + tile = generate_tile(tile_set.turn_tiles) + turn_counter = randi_range(min_turn, max_turn) + else: + tile = generate_tile(tile_set.strait_tiles) + turn_counter -= 1 tiles.append(tile) tile_parent.add_child(tile) tile.global_position = last_tile.spawn_point.global_position