Browse Source

:truck: Factorise seats and door logic

You only need to name your nodes "*Seat" or "*Door" to be automatically
detected.

Nope, no way to find nodes being in a group. This way would be easier to
maintain.
DricomDragon 2 months ago
parent
commit
74cd002167

+ 0 - 12
godot/component/entity/vehicles/biplan/biplan.gd

@@ -30,18 +30,6 @@ func trigger_direction(dir: Vector2) -> void:
 	target_pitch = dir.y * move_to_pitch
 
 
-func get_free_seat() -> Node3D:
-	return $DrivingSeat
-
-
-func get_door() -> Node3D:
-	return $FrontRightDoor
-
-
-func get_closest_door(from: Vector3) -> Node3D:
-	return get_door()
-
-
 func _physics_process(_delta) -> void:
 	# Command part
 	_apply_plane_rotation()

+ 2 - 2
godot/component/entity/vehicles/biplan/biplan.tscn

@@ -86,10 +86,10 @@ shape = SubResource("BoxShape3D_6m175")
 transform = Transform3D(1, 0, 0, 0, 0.980604, -0.195998, 0, 0.195998, 0.980604, 0, 0.107529, -0.828761)
 shape = SubResource("BoxShape3D_6m175")
 
-[node name="DrivingSeat" type="Marker3D" parent="." groups=["vehicle:seat"]]
+[node name="DrivingSeat" type="Marker3D" parent="."]
 transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 2.38419e-07, 0.462769, -0.53054)
 
-[node name="FrontRightDoor" type="Marker3D" parent="." groups=["vehicle:door"]]
+[node name="FrontRightDoor" type="Marker3D" parent="."]
 transform = Transform3D(0.84102, 0, -0.541004, 0, 1, 0, 0.541004, 0, 0.84102, -1.53016, 0.397801, 0.712034)
 
 [node name="VerticalFoil" type="CollisionShape3D" parent="."]

+ 2 - 4
godot/component/entity/vehicles/jeep/Jeep.tscn

@@ -83,10 +83,8 @@ damping_relaxation = 0.5
 
 [node name="OrangeJeepOpen_wheel4" parent="RearRightWheel" instance=ExtResource("3_skal6")]
 
-[node name="RearSeat" type="Marker3D" parent="." groups=["vehicle:seat"]]
-unique_name_in_owner = true
+[node name="RearSeat" type="Marker3D" parent="."]
 transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0.495948, -0.178343)
 
-[node name="RightDoor" type="Marker3D" parent="." groups=["vehicle:door"]]
-unique_name_in_owner = true
+[node name="RightDoor" type="Marker3D" parent="."]
 transform = Transform3D(-0.840567, 0, -0.541708, 0, 1, 0, 0.541708, 0, -0.840567, -0.821067, 0.693978, 0)

+ 0 - 12
godot/component/entity/vehicles/jeep/jeep.gd

@@ -21,18 +21,6 @@ func hand_brake(enabled: bool) -> void:
 		set_brake(0.0)
 
 
-func get_free_seat() -> Node3D:
-	return %RearSeat
-
-
-func get_door() -> Node3D:
-	return %RightDoor
-
-
-func get_closest_door(from: Vector3) -> Node3D:
-	return get_door()
-
-
 func drive_with(commander: LocalInput) -> void:
 	hand_brake(false)
 	super.drive_with(commander)

+ 0 - 12
godot/component/entity/vehicles/plane/tiny_plane.gd

@@ -25,18 +25,6 @@ func trigger_direction(dir: Vector2) -> void:
 	target_pitch = -dir.y * move_to_pitch
 
 
-func get_free_seat() -> Node3D:
-	return $DrivingSeat
-
-
-func get_door() -> Node3D:
-	return $UpDoor
-
-
-func get_closest_door(from: Vector3) -> Node3D:
-	return get_door()
-
-
 func _physics_process(_delta) -> void:
 	# Command part
 	_apply_plane_rotation()

+ 2 - 2
godot/component/entity/vehicles/plane/tiny_plane.tscn

@@ -36,8 +36,8 @@ shape = SubResource("BoxShape3D_w478f")
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.192811, 0.0385618)
 shape = SubResource("BoxShape3D_stelr")
 
-[node name="DrivingSeat" type="Marker3D" parent="." groups=["vehicle:seat"]]
+[node name="DrivingSeat" type="Marker3D" parent="."]
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.253056, -0.941579)
 
-[node name="UpDoor" type="Marker3D" parent="." groups=["vehicle:door"]]
+[node name="UpDoor" type="Marker3D" parent="."]
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.15208, -0.572886)

+ 20 - 6
godot/component/entity/vehicles/seated_vehicle.gd

@@ -7,11 +7,18 @@ const MINIMAL_CRASH_SPEED: float = 1.0 # m2/s2
 var _current_commander: LocalInput = null
 var _has_collided: bool = false
 
+@onready var _seat: Node3D = find_child("*Seat")
+@onready var _doors: Array = find_children("*Door", "Node3D") # Array[Node3D]
+
 
 func _init() -> void:
 	body_shape_entered.connect(_on_body_shape_entered)
 
 
+func _ready() -> void:
+	assert(_doors.all(func(door): return door is Node3D))
+
+
 ## Make the vehicle responds to driver commands
 func drive_with(commander: LocalInput) -> void:
 	commander.dir_changed.connect(_on_dir_changed)
@@ -51,19 +58,26 @@ func _crash_on(pos: Vector3) -> void:
 	get_parent_node_3d().add_child(crash_effect)
 
 
-## Need to be overridden to return an available seat
 func get_free_seat() -> Node3D:
-	return null
+	return _seat
 
 
-## Need to be overridden to return an arbitrary door
+## Can be overridden to return a specific door
 func get_door() -> Node3D:
-	return null
+	return _doors[0] as Node3D
 
 
-## Need to be overridden to return the nearest door from local position
 func get_closest_door(from: Vector3) -> Node3D:
-	return null
+	var distance = func distance_lambda(door: Node3D) -> float:
+		return (door.get_position() - from).length_squared()
+
+	var closest = func closest_lambda(previous_closest: Node3D, maybe_closest: Node3D) -> Node3D:
+		if distance.call(maybe_closest) < distance.call(previous_closest):
+			return maybe_closest
+		else:
+			return previous_closest
+
+	return _doors.reduce(closest)
 
 
 ## Need to be overridden to react to direction changed