Skip to content

Commit

Permalink
Merge pull request #88 from Malcolmnixon/use-helpers
Browse files Browse the repository at this point in the history
Modified PlayerBody and movement functions to use new ARVRHelpers
  • Loading branch information
BastiaanOlij authored Mar 5, 2022
2 parents 6022572 + 6602d48 commit f0e25fe
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 114 deletions.
83 changes: 35 additions & 48 deletions addons/godot-xr-tools/assets/PlayerBody.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ export var push_rigid_bodies := true
## GroundPhysicsSettings to apply - can only be typed in Godot 4+
export (Resource) var physics = null setget set_physics, get_physics

## Path to the ARVROrigin node
export (NodePath) var origin = null

## Path to the ARVRCamera node
export (NodePath) var camera = null

## ARVROrigin node
var origin_node: ARVROrigin = null
onready var origin_node := ARVRHelpers.get_arvr_origin(self)

## ARVRCamera node
var camera_node: ARVRCamera = null
onready var camera_node := ARVRHelpers.get_arvr_camera(self)

## Player KinematicBody node
onready var kinematic_node: KinematicBody = $KinematicBody
Expand Down Expand Up @@ -94,44 +88,8 @@ class SortProviderByOrder:
static func sort_by_order(a, b) -> bool:
return true if a.order < b.order else false

# Get our origin node, make sure we have consistent code here
func _get_origin_node() -> ARVROrigin:
var node : ARVROrigin = get_node_or_null(origin) if origin else get_parent()
return node

# Get our camera node
func _get_camera_node() -> ARVRCamera:
# if we have set a node, try and use it
var node : ARVRCamera

if camera:
node = get_node_or_null(camera)
if node:
return node

var o : ARVROrigin = _get_origin_node()
if !o:
return null

# else get by default name
node = o.get_node_or_null("ARVRCamera")
if node:
return node

# else find the first camera child
for child in o.get_children():
if child is ARVRCamera:
return child

# no luck
return null

# Called when the node enters the scene tree for the first time.
func _ready():
# Get the origin and camera nodes
origin_node = _get_origin_node()
camera_node = _get_camera_node()

# Get the movement providers ordered by increasing order
_movement_providers = get_tree().get_nodes_in_group("movement_providers")
_movement_providers.sort_custom(SortProviderByOrder, "sort_by_order")
Expand Down Expand Up @@ -318,13 +276,13 @@ func _guaranteed_physics():
# - Maximum slope is valid
func _get_configuration_warning():
# Check the origin node
var test_origin_node = _get_origin_node()
if !test_origin_node or !test_origin_node is ARVROrigin:
var test_origin_node = ARVRHelpers.get_arvr_origin(self)
if !test_origin_node:
return "Unable to find ARVR Origin node"

# Check the camera node
var test_camera_node = _get_camera_node()
if !test_camera_node or !test_camera_node is ARVRCamera:
var test_camera_node = ARVRHelpers.get_arvr_camera(self)
if !test_camera_node:
return "Unable to find ARVR Camera node"

# Verify the player radius is valid
Expand All @@ -342,3 +300,32 @@ func _get_configuration_warning():

# Passed basic validation
return ""

## Find the Player Body from a player node and an optional path
static func get_player_body(node: Node, var path: NodePath = "") -> PlayerBody:
var player_body: PlayerBody

# Try using the node path first
if path:
player_body = node.get_node(path) as PlayerBody
if player_body:
return player_body

# Get the origin
var arvr_origin := ARVRHelpers.get_arvr_origin(node)
if !arvr_origin:
return null

# Attempt to get by the default name
player_body = arvr_origin.get_node_or_null("PlayerBody") as PlayerBody
if player_body:
return player_body

# Search all children of the origin for the player body
for child in arvr_origin.get_children():
player_body = child as PlayerBody
if player_body:
return player_body

# Could not find player body
return null
2 changes: 0 additions & 2 deletions addons/godot-xr-tools/assets/PlayerBody.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ height = 1.4

[node name="PlayerBody" type="Node"]
script = ExtResource( 1 )
origin = null
camera = null

[node name="KinematicBody" type="KinematicBody" parent="." groups=["player_body"]]
collision_layer = 524288
Expand Down
13 changes: 4 additions & 9 deletions addons/godot-xr-tools/functions/Function_Climb_movement.gd
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ export var fling_multiplier := 1.0
export var velocity_averages := 5

## Pickup function for the left hand
export (NodePath) var left_pickup = null
export (NodePath) var left_pickup

## Pickup function for the right hand
export (NodePath) var right_pickup = null
export (NodePath) var right_pickup

# Is the player climbing
var is_climbing := false

# Node references
var _left_pickup_node : Function_Pickup = null
var _right_pickup_node : Function_Pickup = null
onready var _left_pickup_node: Function_Pickup = get_node(left_pickup)
onready var _right_pickup_node: Function_Pickup = get_node(right_pickup)

# Velocity averaging fields
var _distances = Array()
Expand All @@ -57,11 +57,6 @@ var _deltas = Array()
# Horizontal vector (multiply by this to get only the horizontal components
const horizontal := Vector3(1.0, 0.0, 1.0)

# Called when the node enters the scene tree for the first time.
func _ready():
_left_pickup_node = get_node(left_pickup)
_right_pickup_node = get_node(right_pickup)

func physics_movement(delta: float, player_body: PlayerBody):
# Get the left-hand climbable
var left_climbable = _left_pickup_node.picked_up_object
Expand Down
24 changes: 6 additions & 18 deletions addons/godot-xr-tools/functions/Function_Glide_movement.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,16 @@ export var horizontal_slew_rate := 1.0
## Slew rate to transition to gliding
export var vertical_slew_rate := 2.0

## Left ARVR Controller
export (NodePath) var left_controller = null

## Right ARVR Controller
export (NodePath) var right_controller = null

# Node references
var _left_controller_node: ARVRController = null
var _right_controller_node: ARVRController = null
onready var _left_controller_node := ARVRHelpers.get_left_controller(self)
onready var _right_controller_node := ARVRHelpers.get_right_controller(self)

# Is the player gliding
var is_gliding := false

# Horizontal vector (multiply by this to get only the horizontal components
const horizontal := Vector3(1.0, 0.0, 1.0)

# Called when the node enters the scene tree for the first time.
func _ready():
# Get the controllers
_left_controller_node = get_node(left_controller) if left_controller else get_node("../LeftHandController")
_right_controller_node = get_node(right_controller) if right_controller else get_node("../RightHandController")

func physics_movement(delta: float, player_body: PlayerBody):
# Skip if either controller is off
if !_left_controller_node.get_is_active() or !_right_controller_node.get_is_active():
Expand Down Expand Up @@ -125,13 +113,13 @@ func _set_is_gliding(gliding: bool):
# This method verifies the MovementProvider has a valid configuration.
func _get_configuration_warning():
# Verify the left controller
var test_left_controller_node = get_node_or_null(left_controller) if left_controller else get_node_or_null("../LeftHandController")
if !test_left_controller_node or !test_left_controller_node is ARVRController:
var test_left_controller_node = ARVRHelpers.get_left_controller(self)
if !test_left_controller_node:
return "Unable to find left ARVR Controller node"

# Verify the right controller
var test_right_controller_node = get_node_or_null(right_controller) if right_controller else get_node_or_null("../RightHandController")
if !test_right_controller_node or !test_right_controller_node is ARVRController:
var test_right_controller_node = ARVRHelpers.get_right_controller(self)
if !test_right_controller_node:
return "Unable to find right ARVR Controller node"

# Check glide parameters
Expand Down
23 changes: 1 addition & 22 deletions addons/godot-xr-tools/functions/Function_Wind_movement.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func _ready():
return

# Reparent the sense area to the camera
var camera = get_arvr_camera()
var camera := ARVRHelpers.get_arvr_camera(self)
if camera:
self.remove_child(_sense_area)
camera.add_child(_sense_area)
Expand Down Expand Up @@ -80,27 +80,6 @@ func physics_movement(delta: float, player_body: PlayerBody):
drag_factor = clamp(drag_factor, 0.0, 1.0)
player_body.velocity = lerp(player_body.velocity, wind_velocity, drag_factor)

# Get our camera node
func get_arvr_camera() -> ARVRCamera:
# Get the ARVROrigin node
var origin := get_arvr_origin()
if !origin:
return null

# Attempt to get using the default name
var camera := origin.get_node_or_null("ARVRCamera") as ARVRCamera
if camera:
return camera

# Find the first ARVRCamera child
for child in origin.get_children():
camera = child as ARVRCamera
if camera:
return camera

# Unable to find ARVRCamera
return null

# This method verifies the MovementProvider has a valid configuration.
func _get_configuration_warning():
# Call base class
Expand Down
20 changes: 5 additions & 15 deletions addons/godot-xr-tools/functions/MovementProvider.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,10 @@ extends Node
## Enable movement provider
export var enabled := true

# Get our origin node, we should be in a branch of this
func get_arvr_origin() -> ARVROrigin:
var parent = get_parent()
while parent:
if parent is ARVROrigin:
return parent
parent = parent.get_parent()

return null

# Get our player body, this should be a node on our ARVROrigin node.
func get_player_body() -> PlayerBody:
# get our origin node
var arvr_origin = get_arvr_origin()
var arvr_origin := ARVRHelpers.get_arvr_origin(self)
if !arvr_origin:
return null

Expand All @@ -40,16 +30,16 @@ func get_player_body() -> PlayerBody:
return null

# get our player node
var player_body = arvr_origin.get_node("PlayerBody")
if player_body and player_body is PlayerBody:
var player_body := arvr_origin.get_node("PlayerBody") as PlayerBody
if player_body:
return player_body

return null

# If missing we need to add our player body
func _create_player_body_node():
# get our origin node
var arvr_origin = get_arvr_origin()
var arvr_origin := ARVRHelpers.get_arvr_origin(self)
if !arvr_origin:
return

Expand Down Expand Up @@ -79,7 +69,7 @@ func physics_movement(delta: float, player_body: PlayerBody):
# This method verifies the MovementProvider has a valid configuration.
func _get_configuration_warning():
# Verify we're within the tree of an ARVROrigin node
var arvr_origin = get_arvr_origin()
var arvr_origin = ARVRHelpers.get_arvr_origin(self)
if !arvr_origin:
return "This node must be within a branch on an ARVROrigin node"

Expand Down

0 comments on commit f0e25fe

Please sign in to comment.