From 549d0c82db26f142ce21e12811b07568adc9d6f9 Mon Sep 17 00:00:00 2001 From: dusk Date: Tue, 3 Mar 2026 16:20:54 +0100 Subject: [PATCH] feat: Spawn and despawn pedestrians --- level.gd | 11 ++++++++++- pedestrian.gd | 6 ++++++ pedestrian.tscn | 20 +++++++++++--------- player.gd | 21 ++++++++++++++++++++- player.tscn | 45 ++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 87 insertions(+), 16 deletions(-) diff --git a/level.gd b/level.gd index 5ba5a09..b7acb46 100644 --- a/level.gd +++ b/level.gd @@ -3,6 +3,8 @@ extends Node3D @export var dimension: int = 5 @export var path_count: int = 5 +var pedestrian_scene = preload("res://pedestrian.tscn") + func dir_to_vector(dir: int) -> Vector2i: assert(dir >= 0 and dir <= 3) var polarity = dir % 2 # 0: negative, 1: positive @@ -87,4 +89,11 @@ func _ready() -> void: # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: - pass + 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): + print("Pedestrian at ", ped_pos) + var new_ped = pedestrian_scene.instantiate() + add_child(new_ped) + new_ped.position = ped_pos diff --git a/pedestrian.gd b/pedestrian.gd index 03bc00f..598445a 100644 --- a/pedestrian.gd +++ b/pedestrian.gd @@ -10,3 +10,9 @@ func _process(delta: float) -> void: randomize() move_and_collide(Vector3(x_movement, 0, z_movement) * delta) + + +func _on_pedestrian_area_area_exited(area: Area3D) -> void: + if area.name == "DespawnArea": + get_parent().call_deferred("remove_child", self) + queue_free() diff --git a/pedestrian.tscn b/pedestrian.tscn index 172e96d..d435d21 100644 --- a/pedestrian.tscn +++ b/pedestrian.tscn @@ -2,10 +2,6 @@ [ext_resource type="Script" uid="uid://beqs2m2yf1igp" path="res://pedestrian.gd" id="1_y5q06"] -[sub_resource type="CylinderShape3D" id="CylinderShape3D_6pjml"] -height = 0.5 -radius = 4.0 - [sub_resource type="CylinderMesh" id="CylinderMesh_6pjml"] top_radius = 1.0 bottom_radius = 1.0 @@ -14,17 +10,23 @@ height = 0.1 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_y5q06"] albedo_color = Color(0.23, 0.23, 0.23, 1) +[sub_resource type="CylinderShape3D" id="CylinderShape3D_6pjml"] +height = 0.5 +radius = 4.0 + [node name="Pedestrian" type="StaticBody3D" unique_id=896871991] collision_layer = 0 collision_mask = 0 script = ExtResource("1_y5q06") -[node name="Collision" type="CollisionShape3D" parent="." unique_id=1710819275] -shape = SubResource("CylinderShape3D_6pjml") -debug_color = Color(2.8878452e-07, 0.5747548, 0.82784265, 0.41960785) - [node name="Mesh" type="MeshInstance3D" parent="." unique_id=58534625] mesh = SubResource("CylinderMesh_6pjml") surface_material_override/0 = SubResource("StandardMaterial3D_y5q06") -[node name="Area" type="Area3D" parent="." unique_id=332853531] +[node name="PedestrianArea" type="Area3D" parent="." unique_id=332853531] + +[node name="Collision" type="CollisionShape3D" parent="PedestrianArea" unique_id=1710819275] +shape = SubResource("CylinderShape3D_6pjml") +debug_color = Color(2.8878452e-07, 0.5747548, 0.82784265, 0.41960785) + +[connection signal="area_exited" from="PedestrianArea" to="." method="_on_pedestrian_area_area_exited"] diff --git a/player.gd b/player.gd index 2d5cde4..f9d2206 100644 --- a/player.gd +++ b/player.gd @@ -2,9 +2,12 @@ extends CharacterBody3D const BASE_STAMINA = 50 const BASE_SPEED = 50 const SPRINT_MULT = 1.6 +const SLOW_MULT = 0.5 const STAMINA_COST = 15 const STAMINA_RECOVER = 10 +var pedestrian_area_count = 0 + var stamina: float = BASE_STAMINA @@ -12,9 +15,12 @@ func _process(delta: float) -> void: $StaminaLabel.text = str(int(stamina)) var dir = Input.get_vector("player_move_left", "player_move_right", "player_move_up", "player_move_down") - var speed: float = BASE_SPEED + if (pedestrian_area_count > 0): + speed *= SLOW_MULT + + if Input.is_action_pressed("player_sprint") and stamina > 0: stamina -= STAMINA_COST * delta speed *= SPRINT_MULT @@ -24,3 +30,16 @@ func _process(delta: float) -> void: self.velocity = Vector3(dir.x * speed, 0, dir.y * speed) self.move_and_slide() + + +func _on_area_area_entered(area: Area3D) -> void: + if area.name == "PedestrianArea": + pedestrian_area_count += 1 + print(pedestrian_area_count) + + +func _on_area_area_exited(area: Area3D) -> void: + if area.name == "PedestrianArea": + pedestrian_area_count -= 1 + print(pedestrian_area_count) + diff --git a/player.tscn b/player.tscn index e4507b4..cdc13ce 100644 --- a/player.tscn +++ b/player.tscn @@ -8,23 +8,58 @@ size = Vector2(1, 1) [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4flbx"] albedo_color = Color(1, 0, 0, 1) -[sub_resource type="BoxShape3D" id="BoxShape3D_sh265"] -size = Vector3(0.5, 0.1, 0.5) +[sub_resource type="CylinderShape3D" id="CylinderShape3D_i3pqv"] +height = 0.5 +radius = 0.75 + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_4flbx"] +height = 0.5 +radius = 1.0 + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_onrkg"] +height = 1.0 +radius = 25.0 + +[sub_resource type="CylinderShape3D" id="CylinderShape3D_hqtel"] +height = 0.5 +radius = 40.0 [node name="Player" type="CharacterBody3D" unique_id=17046866] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.153225, 0) script = ExtResource("1_4flbx") -[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=1740724392] +[node name="Mesh" type="MeshInstance3D" parent="." unique_id=1740724392] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0) mesh = SubResource("PlaneMesh_sh265") surface_material_override/0 = SubResource("StandardMaterial3D_4flbx") -[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=271233394] +[node name="Collision" type="CollisionShape3D" parent="." unique_id=271233394] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0) -shape = SubResource("BoxShape3D_sh265") +shape = SubResource("CylinderShape3D_i3pqv") +debug_color = Color(0.41852123, 0.4635067, 0.996484, 0.41960785) [node name="StaminaLabel" type="Label" parent="." unique_id=762689230] offset_right = 99.0 offset_bottom = 46.0 theme_override_colors/font_color = Color(0.83137256, 0, 0.18039216, 1) + +[node name="Hurtbox" type="Area3D" parent="." unique_id=2004081669] + +[node name="Collision" type="CollisionShape3D" parent="Hurtbox" unique_id=1101412773] +shape = SubResource("CylinderShape3D_4flbx") +debug_color = Color(0.99958235, 0, 0.12291277, 0.41960785) + +[node name="SpawnArea" type="Area3D" parent="." unique_id=847883204] + +[node name="Collision" type="CollisionShape3D" parent="SpawnArea" unique_id=742727591] +shape = SubResource("CylinderShape3D_onrkg") +debug_color = Color(0.5381443, 0.5430501, 0.4771669, 0.41960785) + +[node name="DespawnArea" type="Area3D" parent="." unique_id=161247633] + +[node name="Collision" type="CollisionShape3D" parent="DespawnArea" unique_id=1796984889] +shape = SubResource("CylinderShape3D_hqtel") +debug_color = Color(0.6328197, 0.113640614, 0, 0.41960785) + +[connection signal="area_entered" from="Hurtbox" to="." method="_on_area_area_entered"] +[connection signal="area_exited" from="Hurtbox" to="." method="_on_area_area_exited"]