feat: Spawn and despawn pedestrians

This commit is contained in:
Dusk 2026-03-03 16:20:54 +01:00
parent 2f20c8a283
commit 549d0c82db
5 changed files with 87 additions and 16 deletions

View File

@ -3,6 +3,8 @@ extends Node3D
@export var dimension: int = 5 @export var dimension: int = 5
@export var path_count: int = 5 @export var path_count: int = 5
var pedestrian_scene = preload("res://pedestrian.tscn")
func dir_to_vector(dir: int) -> Vector2i: func dir_to_vector(dir: int) -> Vector2i:
assert(dir >= 0 and dir <= 3) assert(dir >= 0 and dir <= 3)
var polarity = dir % 2 # 0: negative, 1: positive 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. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: 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

View File

@ -10,3 +10,9 @@ func _process(delta: float) -> void:
randomize() randomize()
move_and_collide(Vector3(x_movement, 0, z_movement) * delta) 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()

View File

@ -2,10 +2,6 @@
[ext_resource type="Script" uid="uid://beqs2m2yf1igp" path="res://pedestrian.gd" id="1_y5q06"] [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"] [sub_resource type="CylinderMesh" id="CylinderMesh_6pjml"]
top_radius = 1.0 top_radius = 1.0
bottom_radius = 1.0 bottom_radius = 1.0
@ -14,17 +10,23 @@ height = 0.1
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_y5q06"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_y5q06"]
albedo_color = Color(0.23, 0.23, 0.23, 1) 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] [node name="Pedestrian" type="StaticBody3D" unique_id=896871991]
collision_layer = 0 collision_layer = 0
collision_mask = 0 collision_mask = 0
script = ExtResource("1_y5q06") 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] [node name="Mesh" type="MeshInstance3D" parent="." unique_id=58534625]
mesh = SubResource("CylinderMesh_6pjml") mesh = SubResource("CylinderMesh_6pjml")
surface_material_override/0 = SubResource("StandardMaterial3D_y5q06") 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"]

View File

@ -2,9 +2,12 @@ extends CharacterBody3D
const BASE_STAMINA = 50 const BASE_STAMINA = 50
const BASE_SPEED = 50 const BASE_SPEED = 50
const SPRINT_MULT = 1.6 const SPRINT_MULT = 1.6
const SLOW_MULT = 0.5
const STAMINA_COST = 15 const STAMINA_COST = 15
const STAMINA_RECOVER = 10 const STAMINA_RECOVER = 10
var pedestrian_area_count = 0
var stamina: float = BASE_STAMINA var stamina: float = BASE_STAMINA
@ -12,9 +15,12 @@ func _process(delta: float) -> void:
$StaminaLabel.text = str(int(stamina)) $StaminaLabel.text = str(int(stamina))
var dir = Input.get_vector("player_move_left", "player_move_right", "player_move_up", "player_move_down") var dir = Input.get_vector("player_move_left", "player_move_right", "player_move_up", "player_move_down")
var speed: float = BASE_SPEED var speed: float = BASE_SPEED
if (pedestrian_area_count > 0):
speed *= SLOW_MULT
if Input.is_action_pressed("player_sprint") and stamina > 0: if Input.is_action_pressed("player_sprint") and stamina > 0:
stamina -= STAMINA_COST * delta stamina -= STAMINA_COST * delta
speed *= SPRINT_MULT speed *= SPRINT_MULT
@ -24,3 +30,16 @@ func _process(delta: float) -> void:
self.velocity = Vector3(dir.x * speed, 0, dir.y * speed) self.velocity = Vector3(dir.x * speed, 0, dir.y * speed)
self.move_and_slide() 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)

View File

@ -8,23 +8,58 @@ size = Vector2(1, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4flbx"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4flbx"]
albedo_color = Color(1, 0, 0, 1) albedo_color = Color(1, 0, 0, 1)
[sub_resource type="BoxShape3D" id="BoxShape3D_sh265"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_i3pqv"]
size = Vector3(0.5, 0.1, 0.5) 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] [node name="Player" type="CharacterBody3D" unique_id=17046866]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.153225, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.153225, 0)
script = ExtResource("1_4flbx") 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) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0)
mesh = SubResource("PlaneMesh_sh265") mesh = SubResource("PlaneMesh_sh265")
surface_material_override/0 = SubResource("StandardMaterial3D_4flbx") 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) 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] [node name="StaminaLabel" type="Label" parent="." unique_id=762689230]
offset_right = 99.0 offset_right = 99.0
offset_bottom = 46.0 offset_bottom = 46.0
theme_override_colors/font_color = Color(0.83137256, 0, 0.18039216, 1) 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"]