From a913825d37d427243d8e6e9fb68b97be634c9aae Mon Sep 17 00:00:00 2001 From: Samuel Sarette Date: Thu, 20 Oct 2022 15:12:27 -0400 Subject: [PATCH] formatting cleanup for godot standards --- .gitignore | 7 +++ example_character/character.gd | 29 ++++----- hud/xr_floating_hud.gd | 13 +++- launcher/xr_or_flat_mode_launcher.gd | 26 ++++---- xr_cam_corrected.gd | 17 ++---- xr_character_input.gd | 21 +++---- xr_or_flat_mode_singleton.gd | 90 +++++++++++++--------------- 7 files changed, 94 insertions(+), 109 deletions(-) diff --git a/.gitignore b/.gitignore index e30d41d..0fd25fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,19 @@ +# Godot 4+ specific ignores +.godot/ # Godot-specific ignores .import/ export.cfg export_presets.cfg +# Imported translations (automatically generated from CSV files) +*.translation + # Mono-specific ignores .mono/ data_*/ +mono_crash.*.json # builds [Bb]uild/ + diff --git a/example_character/character.gd b/example_character/character.gd index 9807191..6756eca 100644 --- a/example_character/character.gd +++ b/example_character/character.gd @@ -1,27 +1,19 @@ extends KinematicBody - - ## ## Example XR / Flat -Controlled Character ## -## @desc: -## The script provides a simple example of a character controlled by the -## input, and using the camera helpers from, "XrOrFlatMode" singleton. +## The script provides a simple example of a character controlled by the +## input, and using the camera helpers from, "XrOrFlatMode" singleton. ## +enum Follow { NEITHER, FLAT, BOTH } -enum Follow { - Flat, - Both, - Neither -} - -export (float, 10.0, 60.0) var speed : float = 8.0 +export(float, 10.0, 60.0) var speed: float = 8.0 -export (Follow) var FollowStyle := Follow.Both +export(Follow) var follow_style := Follow.BOTH -export (float, 0.01, 1.0) var bump_vibrate = 0.75 +export(float, 0.01, 1.0) var bump_vibrate = 0.75 var _velocity := Vector3.ZERO @@ -30,15 +22,16 @@ var _was_on_wall := true onready var _anim = $AnimationPlayer + func _physics_process(_delta): if _velocity != Vector3.ZERO: # warning-ignore:return_value_discarded move_and_slide_with_snap(_velocity, Vector3.DOWN) - match FollowStyle: - Follow.Both: + match follow_style: + Follow.BOTH: XrOrFlatMode.camera_slide_to(translation, Vector3(0, 4.5, 5), Vector3(0, 0, 7.5)) - Follow.Flat: + Follow.FLAT: XrOrFlatMode.flat_camera_slide_to(translation, Vector3(0, 4.5, 5)) @@ -67,5 +60,3 @@ func _process_vibration(): if is_on_wall() and not _was_on_wall: XrOrFlatMode.vibrate(bump_vibrate, 0.0, 0.1) _was_on_wall = is_on_wall() - - diff --git a/hud/xr_floating_hud.gd b/hud/xr_floating_hud.gd index cb81235..6ddf8cc 100644 --- a/hud/xr_floating_hud.gd +++ b/hud/xr_floating_hud.gd @@ -1,14 +1,21 @@ extends Spatial +## +## XR Floating HUD +## +## The script ensures the HUD stays relative to the XR player's height. +## + var _offset := Vector3.ZERO + func _ready(): - _offset = global_translation - XrOrFlatMode._get_xr_origin().global_translation + _offset = global_translation - XrOrFlatMode.get_xr_origin().global_translation -func _process(delta): +func _process(_delta): # Keep the original X/Z offset - global_translation = XrOrFlatMode._get_xr_origin().global_translation + _offset + global_translation = XrOrFlatMode.get_xr_origin().global_translation + _offset # Apply the player height Y translation.y = XrOrFlatMode.xr_player_height() diff --git a/launcher/xr_or_flat_mode_launcher.gd b/launcher/xr_or_flat_mode_launcher.gd index c6c14a5..8643fb4 100644 --- a/launcher/xr_or_flat_mode_launcher.gd +++ b/launcher/xr_or_flat_mode_launcher.gd @@ -1,21 +1,21 @@ class_name XrOrFlatModeLauncher extends Node - ## ## XR / Flat Mode Game Launcher ## -## @desc: -## The script launches the game in either flat or xr modes based on -## either features added to the export options, command-line arguments, -## or autodetected based on whether the XR interface can be started, -## i.e. if a VR headset is connected or not. +## The script launches the game in either flat or xr modes based on +## either features added to the export options, command-line arguments, +## or autodetected based on whether the XR interface can be started, +## i.e. if a VR headset is connected or not. ## func _ready(): - if _check_os_features(): return - if _check_args(): return + if _check_os_features(): + return + if _check_args(): + return _autodetect() @@ -71,8 +71,8 @@ func _check_args() -> bool: func _autodetect() -> void: # if we didn't specify, autodetect print("Autodetecting XR or non-XR mode on whether a headset is connected...") - var xrInterface := ARVRServer.find_interface("OpenXR") - if xrInterface and xrInterface.initialize(): + var xr_interface := ARVRServer.find_interface("OpenXR") + if xr_interface and xr_interface.initialize(): launch_xr() else: launch_flat() @@ -81,7 +81,7 @@ func _autodetect() -> void: # Launch the XR scene func launch_xr() -> void: print("XR Mode Active") - XrOrFlatMode.CurrentMode = XrOrFlatMode.XR + XrOrFlatMode.current_mode = XrOrFlatMode.Mode.XR if get_tree().change_scene("res://example_level/xr.tscn") != OK: print("Failed to load initial scene, quitting...") get_tree().notification(NOTIFICATION_WM_QUIT_REQUEST) @@ -90,9 +90,7 @@ func launch_xr() -> void: # Launch the Flat Scene func launch_flat() -> void: print("Standard Non-XR Mode Active") - XrOrFlatMode.CurrentMode = XrOrFlatMode.Flat + XrOrFlatMode.current_mode = XrOrFlatMode.Mode.FLAT if get_tree().change_scene("res://example_level/flat.tscn") != OK: print("Failed to load initial scene, quitting...") get_tree().notification(NOTIFICATION_WM_QUIT_REQUEST) - - diff --git a/xr_cam_corrected.gd b/xr_cam_corrected.gd index f1c91df..7cde604 100644 --- a/xr_cam_corrected.gd +++ b/xr_cam_corrected.gd @@ -1,35 +1,30 @@ class_name CorrectedARVRCamera extends ARVRCamera - - ## ## XR Stage/Camera Rotation Mis-Match Correction Script ## -## @desc: -## This whole script is a workaround for sometimes the XR stage not -## being aligned with the initial camera rotation +## This whole script is a workaround for sometimes the XR stage not +## being aligned with the initial camera rotation ## - var _last_rotation := Vector3.ZERO var _correction_applied := false -func _process(delta): +func _process(_delta): if _last_rotation != Vector3.ZERO and not _correction_applied: - var rotationY = global_rotation.y + var rotation_y = global_rotation.y # Rotate towards where the Origin node is pointing - get_parent().global_rotation.y -= rotationY + get_parent().global_rotation.y -= rotation_y # Inform the Singleton of the offset applied - XrOrFlatMode.XrRotationYCorrection = rotationY + XrOrFlatMode.xr_y_rotation_correction = rotation_y # Never Run this code for the life of this XR Camera _correction_applied = true set_process(false) _last_rotation = global_rotation - diff --git a/xr_character_input.gd b/xr_character_input.gd index f0bb2c7..018f3ed 100644 --- a/xr_character_input.gd +++ b/xr_character_input.gd @@ -1,18 +1,14 @@ +tool class_name XRModeCharacterInput, "res://addons/godot-xr-tools/editor/icons/function.svg" extends Node -tool - - ## ## XR Character Input ## -## @desc: -## The script provides a means to capture the input from XR controller and -## passes it along to the "XrOrFlatMode" singleton for use with a character. +## The script provides a means to capture the input from XR controller and +## passes it along to the "XrOrFlatMode" singleton for use with a character. ## - # use enum from XRTools if they centralize these enum Buttons { VR_BUTTON_BY = 1, @@ -33,14 +29,15 @@ enum Buttons { } ## Button to trigger jump -export (Buttons) var jump_button_id = Buttons.VR_BUTTON_AX +export(Buttons) var jump_button_id = Buttons.VR_BUTTON_AX export var left_right_deadzone := 0.3 export var up_down_deadzone := 0.3 # Controller node -onready var _controller : ARVRController = get_parent() +onready var _controller: ARVRController = get_parent() + func _ready(): # If I don't handle end/begin events, replacing the headset stops character control @@ -68,17 +65,17 @@ func _process(_delta): return # Read the left/right joystick axis - var left_right : float = _controller.get_joystick_axis(0) + var left_right: float = _controller.get_joystick_axis(0) if abs(left_right) <= left_right_deadzone: left_right = 0 # Read the up/down joystick axis - var up_down : float = _controller.get_joystick_axis(1) * -1 # flip vertical + var up_down: float = _controller.get_joystick_axis(1) * -1 # flip vertical if abs(up_down) <= up_down_deadzone: up_down = 0 var input := Vector2(left_right, up_down) - XrOrFlatMode.XrCharacterInput = input + XrOrFlatMode.xr_character_input = input # This method verifies the movement provider has a valid configuration. diff --git a/xr_or_flat_mode_singleton.gd b/xr_or_flat_mode_singleton.gd index eec095c..1299455 100644 --- a/xr_or_flat_mode_singleton.gd +++ b/xr_or_flat_mode_singleton.gd @@ -1,52 +1,40 @@ extends Node - - ## ## XR / Flat Mode Autoload Singleton ## -## @desc: -## The script provides a means for the "What mode are we in" question -## to be asked from anywhere. Expected to use a Node name of "XrOrFlatMode". +## The script provides a means for the "What mode are we in" question +## to be asked from anywhere. Expected to use a Node name of "XrOrFlatMode". ## - # The Two Modes supported -enum Mode { - XR, - Flat -} - -# Shorthands "Mode.Flat" to simplify code -var Flat = Mode.Flat - -# Shorthands "Mode.XR" to simplify code -var XR = Mode.XR +enum Mode { XR, FLAT } # Current Mode (XR / Flat) -var CurrentMode = Mode.Flat setget _on_mode_set +var current_mode = Mode.FLAT setget _on_mode_set # Cache of character input value from the XrCharacterInput script -var XrCharacterInput := Vector2.ZERO +var xr_character_input := Vector2.ZERO + +# This is a workaround for sometimes the XR stage not being aligned with the initial camera rotation +var xr_y_rotation_correction: float = 0 # Cache of the active camera -var _camera : Camera +var _camera: Camera # Cache of the active XR Origin -var _xr_origin : ARVROrigin +var _xr_origin: ARVROrigin # Cache of the XrCharacterInput's Controller, for vibration -var _get_character_input_controller : ARVRController +var _get_character_input_controller: ARVRController -# This is a workaround for sometimes the XR stage not being aligned with the initial camera rotation -var XrRotationYCorrection : float = 0 -# Setter for "CurrentMode", ensures XR Server event subscribing +# Setter for "current_mode", ensures XR Server event subscribing func _on_mode_set(value): - if value == XR: + if value == Mode.XR: # warning-ignore:return_value_discarded ARVRServer.connect("openxr_session_exiting", self, "_on_xr_session_exiting") - CurrentMode = value + current_mode = value # Close the App when the session ends @@ -57,13 +45,13 @@ func _on_xr_session_exiting(): # Return input from the normal input or the XRCharacterInput script & variable func get_character_input() -> Vector2: - if CurrentMode == XR: - return XrCharacterInput + if current_mode == Mode.XR: + return xr_character_input # Technically, this code works in XR, but produces undesired results - return Input.get_vector("character_left", "character_right", - "character_forward", "character_backward", - 0.3) + return Input.get_vector( + "character_left", "character_right", "character_forward", "character_backward", 0.3 + ) # Get the camera @@ -74,8 +62,8 @@ func _get_camera() -> Camera: # Get the XR Origin -func _get_xr_origin() -> ARVROrigin: - if CurrentMode == Flat: +func get_xr_origin() -> ARVROrigin: + if current_mode == Mode.FLAT: return null if !is_instance_valid(_xr_origin): @@ -88,18 +76,18 @@ func _get_xr_origin() -> ARVROrigin: # Rotate Flat Camera (no XR) to look at target func flat_look_at(target: Vector3): - if CurrentMode == Flat: + if current_mode == Mode.FLAT: _get_camera().look_at(target, Vector3.UP) # Slide the Flat Camera or XR Origin to follow target with offset func camera_slide_to(target: Vector3, flat_offset: Vector3, xr_offset: Vector3): - if CurrentMode == Flat: + if current_mode == Mode.FLAT: return flat_camera_slide_to(target, flat_offset) # else XR var new_position = target + xr_offset - _get_xr_origin().global_transform.origin.x = new_position.x - _get_xr_origin().global_transform.origin.z = new_position.z + get_xr_origin().global_transform.origin.x = new_position.x + get_xr_origin().global_transform.origin.z = new_position.z # Slide the Flat Camera (no XR) to follow target with offset @@ -110,9 +98,9 @@ func flat_camera_slide_to(target: Vector3, offset: Vector3): # Rotate the Flat Camera or XR Origin to follow target func rotated_toward(target: Vector3) -> Vector3: - var reference_y : float = 0 - if CurrentMode == XR: - reference_y = _get_xr_origin().global_rotation.y + XrRotationYCorrection + var reference_y: float = 0 + if current_mode == Mode.XR: + reference_y = get_xr_origin().global_rotation.y + xr_y_rotation_correction else: reference_y = _get_camera().global_rotation.y return target.rotated(Vector3.UP, reference_y) @@ -123,27 +111,29 @@ func xr_player_height() -> float: func vibrate(weak_magnitude: float, strong_magnitude: float, duration: float) -> void: - if CurrentMode == Flat: + if current_mode == Mode.FLAT: Input.start_joy_vibration(0, weak_magnitude, strong_magnitude, duration) - else: #XR - var controller : ARVRController = _get_character_input_controller() - var weakAmount := clamp(weak_magnitude/2, 0.0, 0.5) - var strongAmount := clamp(strong_magnitude/2, 0.0, 0.5) - if strongAmount > 0.0: - strongAmount += 0.5 - controller.rumble = weakAmount + strongAmount + else: #XR + var controller: ARVRController = _get_character_input_controller() + var strong_amount := clamp(strong_magnitude / 2, 0.0, 0.5) + if strong_amount > 0.0: + controller.rumble = strong_amount + else: # weak amount + controller.rumble = clamp(weak_magnitude / 2, 0.0, 0.5) + yield(get_tree().create_timer(duration), "timeout") controller.rumble = 0.0 + func _get_character_input_controller() -> ARVRController: if is_instance_valid(_get_character_input_controller): return _get_character_input_controller # See if the right controller has the XRModeCharacterInput, else assume left - var right: ARVRController = ARVRHelpers.get_right_controller(_get_xr_origin()) + var right: ARVRController = ARVRHelpers.get_right_controller(get_xr_origin()) var xr_character_input = right.get_node_or_null("XRModeCharacterInput") as XRModeCharacterInput if xr_character_input: _get_character_input_controller = right else: - _get_character_input_controller = ARVRHelpers.get_left_controller(_get_xr_origin()) + _get_character_input_controller = ARVRHelpers.get_left_controller(get_xr_origin()) return _get_character_input_controller