Skip to content

Commit

Permalink
Make gdlint compliant
Browse files Browse the repository at this point in the history
- Shorten descriptions
- Reorder 'extends'
- Remove whitespace
- Remove unused function
- Remove unnecessary else: return
- Fix 'start_xr.gd' static variable declared out of order
     - Unsure if this fix should be in this PR, but it clears the last of the gdlint errors even though this file was not previously changed in this PR.
  • Loading branch information
squidt committed Oct 22, 2024
1 parent 62c9906 commit e1ca3e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
61 changes: 28 additions & 33 deletions addons/godot-xr-tools/objects/snap_path.gd
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
@tool
extends XRToolsSnapZone
class_name XRToolsSnapPath
extends XRToolsSnapZone


## An [XRToolsSnapZone] that allows [XRToolsPickable] to be placed along a child [Path3D] node.
## They can either be placed along any point in the curve, or at discrete intervals
## by setting "snap_interval" above 0.0. Note: Attached [XRToolsPickable]s will face the +Z axis
## An [XRToolsSnapZone] that allows [XRToolsPickable] to be placed along a
## child [Path3D] node. They can either be placed along any point in the curve
## or at discrete intervals by setting "snap_interval" above '0.0'.
##
## Note: Attached [XRToolsPickable]s will face the +Z axis.


## Real world distance between intervals in Meters.
## Enabled when not 0
@export var snap_interval := 0.0:
set(v): snap_interval = absf(v)

@onready var path : Path3D


func _ready() -> void:
super._ready()

for c in get_children():
if c is Path3D:
path = c
Expand All @@ -33,7 +37,7 @@ func _get_configuration_warnings() -> PackedStringArray:
if c is Path3D:
path = c
return[]
return["This node has no path to place objects along. Consider adding a Path3D as a child to define its shape."]
return["This node requires a Path3D child node to define its shape."]


# Called when a target in our grab area is dropped
Expand All @@ -43,55 +47,55 @@ func _on_target_dropped(target: Node3D) -> void:
!is_instance_valid(target) or \
is_instance_valid(picked_up_object):
return

# Make a zone that will destruct once its object has left
var zone = _make_temp_zone()
var offset = _find_offset(path, target.global_position)

# if snap guide
if _has_snap_guide(target):
# comply with guide
offset = _find_closest_offset_with_length(path.curve, offset, _get_snap_guide(target).length)

# too large to place on path
if is_equal_approx(offset, -1.0):
return

# if snap_interval has been set, use it
if has_snap_interval():
offset = snappedf(offset, snap_interval)

# set position
zone.position = path.curve.sample_baked(offset)

# Add zone as a child
path.add_child(zone)
zone.owner = path

# Connect self-destruct with lambda
zone.has_dropped.connect(func(): zone.queue_free(), Object.ConnectFlags.CONNECT_ONE_SHOT)

# Use Pickable's Shapes as our Shapes
for c in target.get_children():
if c is CollisionShape3D:
PhysicsServer3D.area_add_shape(zone.get_rid(), c.shape.get_rid(), c.transform)
PhysicsServer3D.area_add_shape(zone.get_rid(), c.shape.get_rid(), c.transform)

# Force pickup
zone.pick_up_object(target)


# Make a zone that dies on dropping objects
func _make_temp_zone():
var zone = XRToolsSnapZone.new()

# connect lambda to play stash sounds when temp zone picks up
if has_node("AudioStreamPlayer3D"):
zone.has_picked_up.connect(\
func(object):\
$AudioStreamPlayer3D.stream = stash_sound;\
$AudioStreamPlayer3D.play()\
)

# XRToolsSnapZone manaul copy
zone.enabled = true
zone.stash_sound = stash_sound
Expand All @@ -102,13 +106,13 @@ func _make_temp_zone():
zone.grab_require = grab_require
zone.grab_exclude = grab_exclude
zone.initial_object = NodePath()

# CollisionObject3D manual copy
zone.disable_mode = disable_mode
zone.collision_layer = collision_layer
zone.collision_mask = collision_mask
zone.collision_priority = collision_priority

return zone


Expand All @@ -132,12 +136,12 @@ func _find_closest_offset_with_length(_curve: Curve3D, _offset: float, _length:
# p1 and p2 are the object's start and end respectively
var p1 = _offset
var p2 = _offset - _length

# a _curve's final point is its end, aka the furthest 'forward', which is why it is p1
# path_p1 and path_p2 are the curve's start and end respectively
var path_p1 := _curve.get_closest_offset(_curve.get_point_position(_curve.point_count-1))
var path_p2 := _curve.get_closest_offset(_curve.get_point_position(0))

# if at front (or beyond)
if is_equal_approx(p1, path_p1):
# if too large
Expand All @@ -155,11 +159,8 @@ func _find_closest_offset_with_length(_curve: Curve3D, _offset: float, _length:
# if ideal snap fits, take that
if ideal_snap >= p1_new:
return ideal_snap
else:
return more_snap

return more_snap
return path_p2 + _length

# otherwise: within bounds
return p1

Expand All @@ -169,13 +170,7 @@ func _snappedf_up(x, step) -> float:
return step * ceilf(x / step)


func _find_closest_point(_path: Path3D, _global_position: Vector3) -> Vector3:
# Transform target pos to local space
var local_position := _global_position * _path.global_transform
return _path.curve.get_closest_point(local_position)


func _find_offset(_path: Path3D, _global_position: Vector3) -> float:
# Transform target pos to local space
# transform target pos to local space
var local_pos: Vector3 = _global_position * _path.global_transform
return _path.curve.get_closest_offset(local_pos)
11 changes: 7 additions & 4 deletions addons/godot-xr-tools/objects/snap_path_guide.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
@tool
extends Marker3D
class_name XRToolsSnapPathGuide
extends Marker3D


## XRToolsSnapRailGuide depicts a guide for [XRToolsSnapPath] to judge the
## length of an [XRToolsPickable], helping place pickables within its bounds.
## Add as a child node to any [XRToolsPickable], then move negatively along
## the Z-Axis to define a length.

## XRToolsSnapRailGuide depicts a guide for [XRToolsSnapPath] to judge the length of an [XRToolsPickable].
## Add as a child node to any [XRToolsPickable], then move negatively along the Z-Axis to define a length
## that [XRToolsSnapPath] can use to place it within its [Path3D] bounds.

var length : float:
get:
Expand Down
4 changes: 2 additions & 2 deletions addons/godot-xr-tools/objects/snap_zone.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func _ready():
# Set collision shape radius
if has_node("CollisionShape3D") and "radius" in $CollisionShape3D.shape:
$CollisionShape3D.shape.radius = grab_distance

# Add important connections
if not body_entered.is_connected(_on_snap_zone_body_entered):
body_entered.connect(_on_snap_zone_body_exited)
if not body_exited.is_connected(_on_snap_zone_body_exited):
body_exited.connect(_on_snap_zone_body_exited)

# Perform updates
_update_snap_mode()

Expand Down
8 changes: 4 additions & 4 deletions addons/godot-xr-tools/xr/start_xr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ signal xr_ended
signal xr_failed_to_initialize


## XR active flag
static var _xr_active : bool = false


## Optional viewport to control
@export var viewport : Viewport

Expand All @@ -50,13 +54,9 @@ var xr_interface : XRInterface
## XR frame rate
var xr_frame_rate : float = 0


# Is a WebXR is_session_supported query running
var _webxr_session_query : bool = false

## XR active flag
static var _xr_active : bool = false


# Handle auto-initialization when ready
func _ready() -> void:
Expand Down

0 comments on commit e1ca3e1

Please sign in to comment.