Add RoadManager with a road generation script

This commit is contained in:
reo 2025-03-11 03:33:05 +03:00
parent 7958bd288f
commit 0542d04a24
4 changed files with 71 additions and 33 deletions

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=8 format=3 uid="uid://d4ljkc7l57udr"]
[gd_scene load_steps=9 format=3 uid="uid://d4ljkc7l57udr"]
[ext_resource type="PackedScene" uid="uid://dtncvl5af4fhr" path="res://objects/road.glb" id="1_uwrxv"]
[ext_resource type="Script" uid="uid://hufd4xlq7b6k" path="res://scripts/road_manager.gd" id="1_lbhrr"]
[ext_resource type="PackedScene" uid="uid://mu3b314l3soy" path="res://scenes/road.tscn" id="1_lnu2h"]
[ext_resource type="Script" uid="uid://cs0y5pe8qnj2r" path="res://scripts/player.gd" id="2_lnu2h"]
[ext_resource type="PackedScene" uid="uid://cewenjpq8wbig" path="res://objects/testcar.glb" id="2_yqjtg"]
@ -30,45 +31,19 @@ environment = SubResource("Environment_iywne")
transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0)
shadow_enabled = true
[node name="RoadBlocks" type="Node3D" parent="."]
[node name="RoadManager" type="Node3D" parent="." node_paths=PackedStringArray("player")]
script = ExtResource("1_lbhrr")
player = NodePath("../Player")
[node name="Road" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -10.549, 9.53674e-07, 0.0019331)
[node name="Road2" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, 9.5, 0, 0)
[node name="Road3" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -50.613, 9.53674e-07, 0.0019331)
[node name="Road4" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -30.564, 0, 0)
[node name="Road5" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -89.5907, 9.53674e-07, 0.0019331)
[node name="Road6" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -69.5417, 0, 0)
[node name="Road7" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -129.655, 9.53674e-07, 0.0019331)
[node name="Road8" parent="RoadBlocks" instance=ExtResource("1_uwrxv")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, -109.606, 0, 0)
[node name="Road" parent="RoadManager" instance=ExtResource("1_lnu2h")]
[node name="Player" type="CharacterBody3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.449395)
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
script = ExtResource("2_lnu2h")
[node name="TestCar" parent="Player" instance=ExtResource("2_yqjtg")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9.53674e-07, -0.463639)
[node name="FrontLeftWheel" type="Node3D" parent="Player/TestCar"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.8, 0.3, 1.2)
[node name="FrontRightWheel" type="Node3D" parent="Player/TestCar"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.8, 0.3, 1.2)
[node name="CollisionShape3D" type="CollisionShape3D" parent="Player"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.279745, 1.10964, -0.462006)
shape = SubResource("BoxShape3D_yqjtg")

8
scenes/road.tscn Normal file
View file

@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://mu3b314l3soy"]
[ext_resource type="PackedScene" uid="uid://dtncvl5af4fhr" path="res://objects/road.glb" id="1_mv8x7"]
[node name="Road" type="Node3D"]
[node name="RoadObject" parent="." instance=ExtResource("1_mv8x7")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0)

54
scripts/road_manager.gd Normal file
View file

@ -0,0 +1,54 @@
extends Node3D
@export var player: CharacterBody3D
@export var spawn_distance: float = 100.0
@export var despawn_distance: float = 200.0
var road_scene = preload("res://scenes/road.tscn")
var roads = []
var road_length: float
func _ready():
# Calculate road length from existing instance
var temp_road = road_scene.instantiate()
# Get the MeshInstance3D from the road scene structure
var mesh_instance = temp_road.get_node("RoadObject") as MeshInstance3D
if mesh_instance:
var aabb = mesh_instance.mesh.get_aabb()
road_length = aabb.size.x * mesh_instance.scale.x
else:
push_error("Could not find RoadObject MeshInstance3D in road scene")
road_length = 20.0 # Fallback value
temp_road.queue_free()
# Initialize with existing road
var initial_road = $Road
roads.append(initial_road)
func _process(delta):
if not player:
return
var player_x = player.global_position.x
# Spawn new roads ahead
var last_road = roads[-1]
var last_road_end = last_road.global_position.x + road_length
if player_x + spawn_distance > last_road_end:
spawn_road(last_road_end)
# Despawn old roads behind
for road in roads.duplicate():
var road_end = road.global_position.x + road_length
if player_x - road_end > despawn_distance:
despawn_road(road)
func spawn_road(position_x: float):
var new_road = road_scene.instantiate()
new_road.global_position = Vector3(position_x, 0, 0)
add_child(new_road)
roads.append(new_road)
func despawn_road(road: Node3D):
road.queue_free()
roads.erase(road)

View file

@ -0,0 +1 @@
uid://hufd4xlq7b6k