Skip to content

Commit 7a9c366

Browse files
authored
Better format and static type Desktop Flight (#773)
1 parent 52ae3bd commit 7a9c366

File tree

1 file changed

+75
-57
lines changed

1 file changed

+75
-57
lines changed

addons/godot-xr-tools/desktop-support/movement_desktop_flight.gd

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
class_name XRToolsDesktopMovementFlight
33
extends XRToolsMovementProvider
44

5-
65
## XR Tools Movement Provider for Flying
76
##
87
## This script provides flying movement for the player. The control parameters
@@ -28,7 +27,6 @@ extends XRToolsMovementProvider
2827
## physics effects after flying) or whether additional effects such as
2928
## the default player gravity are applied.
3029

31-
3230
## Signal emitted when flight starts
3331
signal flight_started()
3432

@@ -37,110 +35,130 @@ signal flight_finished()
3735

3836

3937
## Movement provider order
40-
@export var order : int = 30
41-
38+
@export var order: int = 30
4239

4340
## Flight toggle button
44-
@export var flight_button : String = "ui_focus_next"
45-
@export var input_forward : String = "ui_up"
46-
@export var input_backward : String = "ui_down"
47-
@export var input_left : String = "ui_left"
48-
@export var input_right : String = "ui_right"
41+
@export var flight_button: String = "ui_focus_next"
42+
43+
## Input action for moving forwards
44+
@export var input_forward: String = "ui_up"
45+
46+
## Input action for moving backwards
47+
@export var input_backward: String = "ui_down"
48+
49+
## Input action for moving left
50+
@export var input_left: String = "ui_left"
51+
52+
## Input action for moving right
53+
@export var input_right: String = "ui_right"
4954

5055
## Flight speed from control
51-
@export var speed_scale : float = 5.0
56+
@export var speed_scale: float = 5.0
5257

5358
## Flight traction pulling flight velocity towards the controlled speed
54-
@export var speed_traction : float = 3.0
59+
@export var speed_traction: float = 3.0
5560

5661
## Flight acceleration from control
57-
@export var acceleration_scale : float = 0.0
62+
@export var acceleration_scale: float = 0.0
5863

5964
## Flight drag
60-
@export var drag : float = 0.1
65+
@export var drag: float = 0.1
6166

6267
## Guidance effect (virtual fins/wings)
63-
@export var guidance : float = 0.0
68+
@export var guidance: float = 0.0
6469

65-
## If true, flight movement is exclusive preventing further movement functions
66-
@export var exclusive : bool = true
70+
## Whether flight movement is exclusive, preventing further movement functions
71+
@export var exclusive: bool = true
6772

6873

6974
## Flight button state
70-
var _flight_button : bool = false
75+
var _flight_button: bool = false
7176

7277

73-
# Node references
74-
@onready var xr_start_node = XRTools.find_xr_child(
75-
XRTools.find_xr_ancestor(self,
76-
"*Staging",
77-
"XRToolsStaging"),"StartXR","Node")
78-
@onready var _camera := XRHelpers.get_xr_camera(self)
78+
## XRStart Node
79+
@onready var xr_start_node: Node = XRTools.find_xr_child(
80+
XRTools.find_xr_ancestor(
81+
self,
82+
"*Staging",
83+
"XRToolsStaging",
84+
),
85+
"StartXR",
86+
"Node",
87+
)
88+
## XR camera
89+
@onready var _camera: XRCamera3D = XRHelpers.get_xr_camera(self)
7990

8091

81-
# Add support for is_xr_class on XRTools classes
92+
## Add support for is_xr_class on XRTools classes
8293
func is_xr_class(xr_name: String) -> bool:
8394
return xr_name == "XRToolsDesktopMovementFlight" or super(xr_name)
8495

8596

86-
func _ready():
87-
# In Godot 4 we must now manually call our super class ready function
88-
super()
89-
90-
91-
# Process physics movement for flight
92-
func physics_movement(delta: float, player_body: XRToolsPlayerBody, disabled: bool):
97+
## Process physics movement for flight
98+
func physics_movement(
99+
delta: float,
100+
player_body: XRToolsPlayerBody,
101+
disabled: bool,
102+
) -> void:
93103
# Disable flying if requested, or if no controller
94-
if disabled or !enabled or !player_body.enabled or xr_start_node.is_xr_active():
104+
if (
105+
disabled
106+
or not enabled
107+
or not player_body.enabled
108+
or xr_start_node.is_xr_active()
109+
):
95110
set_flying(false)
96111
return
97112

98113
# Detect press of flight button
99-
var old_flight_button = _flight_button
114+
var old_flight_button: bool = _flight_button
115+
100116
_flight_button = Input.is_action_pressed(flight_button)
101-
if _flight_button and !old_flight_button:
102-
set_flying(!is_active)
117+
118+
if _flight_button and not old_flight_button:
119+
set_flying(not is_active)
103120

104121
# Skip if not flying
105-
if !is_active:
122+
if not is_active:
106123
return
107124

108-
# Select the pitch vector
109-
var pitch_vector: Vector3
110-
# Use the vertical part of the 'head' forwards vector
111-
pitch_vector = -_camera.transform.basis.z.y * player_body.up_player
125+
# Select the pitch vector as the vertical part of the 'head' forwards vector
126+
var pitch_vector: Vector3 = -_camera.transform.basis.z.y * player_body.up_player
112127

113-
# Select the bearing vector
114-
var bearing_vector: Vector3
115-
# Use the horizontal part of the 'head' forwards vector
116-
bearing_vector = -_camera.global_transform.basis.z \
128+
# Select the bearing vector as the horizontal part of the 'head' forwards vector
129+
var bearing_vector: Vector3 = -_camera.global_transform.basis.z \
117130
.slide(player_body.up_player)
118131

119132
# Construct the flight bearing
120133
var forwards := (bearing_vector.normalized() + pitch_vector).normalized()
121134
var side := forwards.cross(player_body.up_player)
122135

123136
# Construct the target velocity
124-
var input_dir = Input.get_vector(input_left, input_right, input_backward, input_forward)
125-
var joy_forwards :float= input_dir.y
126-
var joy_side :float= input_dir.x
127-
var heading := forwards * joy_forwards + side * joy_side
137+
var input_dir: Vector2 = Input.get_vector(
138+
input_left,
139+
input_right,
140+
input_backward,
141+
input_forward,
142+
)
143+
var joy_forwards: float = input_dir.y
144+
var joy_side: float = input_dir.x
145+
var heading: Vector3 = forwards * joy_forwards + side * joy_side
128146

129147
# Calculate the flight velocity
130-
var flight_velocity := player_body.velocity
148+
var flight_velocity: Vector3 = player_body.velocity
131149
flight_velocity *= 1.0 - drag * delta
132150
flight_velocity = flight_velocity.lerp(heading * speed_scale, speed_traction * delta)
133151
flight_velocity += heading * acceleration_scale * delta
134152

135153
# Apply virtual guidance effect
136154
if guidance > 0.0:
137-
var velocity_forwards := forwards * flight_velocity.length()
155+
var velocity_forwards: Vector3 = forwards * flight_velocity.length()
138156
flight_velocity = flight_velocity.lerp(velocity_forwards, guidance * delta)
139157

140158
# If exclusive then perform the exclusive move-and-slide
141159
if exclusive:
142160
player_body.velocity = player_body.move_player(flight_velocity)
143-
return true
161+
return
144162

145163
# Update velocity and return for additional effects
146164
player_body.velocity = flight_velocity
@@ -157,25 +175,25 @@ func set_flying(active: bool) -> void:
157175

158176
# Handle state change
159177
if is_active:
160-
emit_signal("flight_started")
178+
flight_started.emit()
161179
else:
162-
emit_signal("flight_finished")
180+
flight_finished.emit()
163181

164182

165-
# This method verifies the movement provider has a valid configuration.
183+
## Verifies the movement provider has a valid configuration.
166184
func _get_configuration_warnings() -> PackedStringArray:
167185
var warnings := super()
168186

169187
# Verify the camera
170-
if !XRHelpers.get_xr_camera(self):
188+
if not XRHelpers.get_xr_camera(self):
171189
warnings.append("Unable to find XRCamera3D")
172190

173191
# Verify the left controller
174-
if !XRHelpers.get_left_controller(self):
192+
if not XRHelpers.get_left_controller(self):
175193
warnings.append("Unable to find left XRController3D node")
176194

177195
# Verify the right controller
178-
if !XRHelpers.get_right_controller(self):
196+
if not XRHelpers.get_right_controller(self):
179197
warnings.append("Unable to find left XRController3D node")
180198

181199
# Return warnings

0 commit comments

Comments
 (0)