From 1bc9f30c44e10647e60f0577fe99cd756d62aca0 Mon Sep 17 00:00:00 2001 From: dusk Date: Tue, 3 Mar 2026 17:10:20 +0100 Subject: [PATCH] feat: Make variables for sizes --- chunk.gd | 56 +++++++++++++++++++++++++++------------------------ global.gd | 2 ++ level.gd | 14 +++++++------ level.tscn | 6 +----- pedestrian.gd | 12 ++++++----- project.godot | 4 ++++ 6 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 global.gd diff --git a/chunk.gd b/chunk.gd index 8ba301a..9a51bb9 100644 --- a/chunk.gd +++ b/chunk.gd @@ -3,9 +3,13 @@ extends Node3D var exits: Array = [false, false, false, false] +const STREET_WIDTH: float = 40 + func _ready() -> void: - pass + $Mesh.mesh.size.x = Global.chunk_size + $Mesh.mesh.size.y = Global.chunk_size + #exits = [ #randi() % 2 == 0, #randi() % 2 == 0, @@ -26,12 +30,12 @@ func update() -> void: #$n.position.y = 1 var vertices = PackedVector3Array() - vertices.push_back(Vector3(-5, 0.5, -50)) - vertices.push_back(Vector3(5, 0.5, -50)) - vertices.push_back(Vector3(-5, 0.5, 0)) - vertices.push_back(Vector3(5, 0.5, -50)) - vertices.push_back(Vector3(5, 0.5, 0)) - vertices.push_back(Vector3(-5, 0.5, 0)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, -Global.chunk_size/2)) + vertices.push_back(Vector3(STREET_WIDTH/2 , 0.5, -Global.chunk_size/2)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, 0)) + vertices.push_back(Vector3(STREET_WIDTH/2, 0.5, -Global.chunk_size/2)) + vertices.push_back(Vector3(STREET_WIDTH/2, 0.5, 0)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, 0)) # Initialize the ArrayMesh. var arr_mesh = ArrayMesh.new() @@ -47,12 +51,12 @@ func update() -> void: #$s.position.y = 1 var vertices = PackedVector3Array() - vertices.push_back(Vector3(-5, 0.5, 0)) - vertices.push_back(Vector3(5, 0.5, 0)) - vertices.push_back(Vector3(-5, 0.5, 50)) - vertices.push_back(Vector3(5, 0.5, 0)) - vertices.push_back(Vector3(5, 0.5, 50)) - vertices.push_back(Vector3(-5, 0.5, 50)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, 0)) + vertices.push_back(Vector3(STREET_WIDTH/2, 0.5, 0)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, Global.chunk_size/2)) + vertices.push_back(Vector3(STREET_WIDTH/2, 0.5, 0)) + vertices.push_back(Vector3(STREET_WIDTH/2, 0.5, Global.chunk_size/2)) + vertices.push_back(Vector3(-STREET_WIDTH/2, 0.5, Global.chunk_size/2)) # Initialize the ArrayMesh. var arr_mesh = ArrayMesh.new() @@ -68,12 +72,12 @@ func update() -> void: #$w.position.y = 1 var vertices = PackedVector3Array() - vertices.push_back(Vector3(-50, 0.5, -5)) - vertices.push_back(Vector3(0, 0.5, -5)) - vertices.push_back(Vector3(-50, 0.5, 5)) - vertices.push_back(Vector3(0, 0.5, -5)) - vertices.push_back(Vector3(0, 0.5, 5)) - vertices.push_back(Vector3(-50, 0.5, 5)) + vertices.push_back(Vector3(-Global.chunk_size/2, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(0, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(-Global.chunk_size/2, 0.5, STREET_WIDTH/2)) + vertices.push_back(Vector3(0, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(0, 0.5, STREET_WIDTH/2)) + vertices.push_back(Vector3(-Global.chunk_size/2, 0.5, STREET_WIDTH/2)) # Initialize the ArrayMesh. var arr_mesh = ArrayMesh.new() @@ -89,12 +93,12 @@ func update() -> void: #$e.position.y = 1 var vertices = PackedVector3Array() - vertices.push_back(Vector3(0, 0.5, -5)) - vertices.push_back(Vector3(50, 0.5, -5)) - vertices.push_back(Vector3(0, 0.5, 5)) - vertices.push_back(Vector3(50, 0.5, -5)) - vertices.push_back(Vector3(50, 0.5, 5)) - vertices.push_back(Vector3(0, 0.5, 5)) + vertices.push_back(Vector3(0, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(Global.chunk_size/2, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(0, 0.5, STREET_WIDTH/2)) + vertices.push_back(Vector3(Global.chunk_size/2, 0.5, -STREET_WIDTH/2)) + vertices.push_back(Vector3(Global.chunk_size/2, 0.5, STREET_WIDTH/2)) + vertices.push_back(Vector3(0, 0.5, STREET_WIDTH/2)) # Initialize the ArrayMesh. var arr_mesh = ArrayMesh.new() @@ -110,7 +114,7 @@ func update() -> void: var center_mesh = MeshInstance3D.new() center_mesh.position = Vector3(0,0.6,0) center_mesh.mesh = PlaneMesh.new() - center_mesh.mesh.size = Vector2(10,10) + center_mesh.mesh.size = Vector2(STREET_WIDTH,STREET_WIDTH) add_child(center_mesh) i += 1 diff --git a/global.gd b/global.gd new file mode 100644 index 0000000..3f1e2b1 --- /dev/null +++ b/global.gd @@ -0,0 +1,2 @@ +extends Node +var chunk_size: float = 150 diff --git a/level.gd b/level.gd index b7acb46..4c89db0 100644 --- a/level.gd +++ b/level.gd @@ -3,6 +3,8 @@ extends Node3D @export var dimension: int = 5 @export var path_count: int = 5 +@export var ped_spawn_rate: int = 60 + var pedestrian_scene = preload("res://pedestrian.tscn") func dir_to_vector(dir: int) -> Vector2i: @@ -32,11 +34,11 @@ func _ready() -> void: var station = Vector2i(randi() % dimension, 0) var spawn = Vector2i(dimension - station.x - 1, dimension - 1) - $Player.position.x = station.x * 100 - $Player.position.z = station.y * 100 + $Player.position.x = station.x * Global.chunk_size + $Player.position.z = station.y * Global.chunk_size - $Pedestrian.position.x = station.x * 100 - $Pedestrian.position.z = station.y * 100 + $Pedestrian.position.x = station.x * Global.chunk_size + $Pedestrian.position.z = station.y * Global.chunk_size var paths: Array = [] for path_idx in range(path_count): @@ -69,7 +71,7 @@ func _ready() -> void: var row: Array for y in range(dimension): var new_chunk = chunk_scn.instantiate() - new_chunk.position = Vector3(x * 101, 0, y * 101) + new_chunk.position = Vector3(x * Global.chunk_size+1, 0, y * Global.chunk_size+1) row.append(new_chunk) add_child(new_chunk) chunks.append(row) @@ -92,7 +94,7 @@ func _process(delta: float) -> void: var spawn_radius = $Player/SpawnArea/Collision.shape.radius + randf_range(-5, 5) var rand_unit_vec = Vector3.RIGHT.rotated(Vector3.UP, randf() * TAU) var ped_pos = $Player.position + rand_unit_vec * spawn_radius - if (randi() % 60 == 5): + if (randi() % ped_spawn_rate == 0): print("Pedestrian at ", ped_pos) var new_ped = pedestrian_scene.instantiate() add_child(new_ped) diff --git a/level.tscn b/level.tscn index 5863bff..89452b3 100644 --- a/level.tscn +++ b/level.tscn @@ -3,7 +3,6 @@ [ext_resource type="Script" uid="uid://dj0cgqed5n3ys" path="res://level.gd" id="1_0b4ue"] [ext_resource type="PackedScene" uid="uid://cmb3b7xrlboy3" path="res://player.tscn" id="1_u52ul"] [ext_resource type="Script" uid="uid://b5ebibi08tjvd" path="res://camera_3d.gd" id="2_vonw3"] -[ext_resource type="PackedScene" uid="uid://cqbu5r5pmnhc" path="res://chunk.tscn" id="3_f2txt"] [ext_resource type="PackedScene" uid="uid://c76lhqe6dqq50" path="res://pedestrian.tscn" id="4_f2txt"] [sub_resource type="Environment" id="Environment_vonw3"] @@ -25,9 +24,6 @@ environment = SubResource("Environment_vonw3") [node name="Player" parent="." unique_id=1536233267 instance=ExtResource("1_u52ul")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.8361101, 0) -[node name="Chunk" parent="." unique_id=731620195 instance=ExtResource("3_f2txt")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.08452606, -3.8146973e-06, 0.120262146) - [node name="MeshInstance3D2" type="MeshInstance3D" parent="." unique_id=306152407] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -11.830486, 7.5, -23.788916) mesh = SubResource("BoxMesh_vonw3") @@ -40,7 +36,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -11.874296, 3.5, -11.78) mesh = SubResource("BoxMesh_oi3di") [node name="Camera3D" type="Camera3D" parent="." unique_id=2083164402] -transform = Transform3D(1, 0, 0, 0, -4.371139e-08, 1, 0, -1, -4.371139e-08, 0, 113.43584, 0) +transform = Transform3D(1, 0, 0, 0, -4.371139e-08, 1, 0, -1, -4.371139e-08, 0, 47.751747, 0) current = true fov = 55.0 script = ExtResource("2_vonw3") diff --git a/pedestrian.gd b/pedestrian.gd index 598445a..e1124c5 100644 --- a/pedestrian.gd +++ b/pedestrian.gd @@ -1,12 +1,14 @@ extends StaticBody3D -@onready var x_movement = randi() % 10 -@onready var z_movement = randi() % 10 +var movement_rate = 200 + +@onready var x_movement = randi_range(-10, 10) +@onready var z_movement = randi_range(-10, 10) func _process(delta: float) -> void: - if (randi() % 80) == 1: - x_movement = randi() % 10 - z_movement = randi() % 10 + if (randi() % movement_rate) == 1: + x_movement = randi_range(-10, 10) + z_movement = randi_range(-10, 10) randomize() move_and_collide(Vector3(x_movement, 0, z_movement) * delta) diff --git a/project.godot b/project.godot index 8306a71..7efd0ef 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ run/main_scene="uid://dxap1hj0okhli" config/features=PackedStringArray("4.6", "GL Compatibility") config/icon="res://icon.svg" +[autoload] + +Global="*uid://ca3n3ljnvvdil" + [input] player_move_left={