Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More signals for teleporter function #513

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
217 changes: 217 additions & 0 deletions addons/godot-xr-tools/events/teleporter_event.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
class_name XRToolsTeleporterEvent

## Types of pointer events
enum Type {
## Teleporter activated
ACTIVATED, # 0

## Teleporter beam hit a suitable target
ENTERED, # 1

## Teleporter beam moved inside a suitable target
MOVED, # 2

## Teleporter beam left a suitable target
EXITED, # 3

## Teleporter's ability to perform teleportation changed
CAN_TELEPORT_CHANGED, # 4

## Player got teleportet to the teleporter's target location
TELEPORTED, # 5

## Teleporter deactivated
DEACTIVATED # 6
}

## Type of teleporter event
var event_type : Type

## Teleporter generating event
var teleporter : Node3D

## Teleporter activated
var is_teleporting : bool

## Teleporter's ability to perform teleportation
var can_teleport : bool

## Current teleporter target
var target : Node3D

## Teleporter target position
var position : Vector3

## Last target position
var last_position : Vector3


## Initialize a new instance of the XRToolsPointerEvent class
func _init(
p_event_type : Type,
p_teleporter : Node3D,
p_is_teleporting : bool,
p_can_teleport : bool,
p_target : Node3D,
p_position : Vector3,
p_last_position : Vector3) -> void:
event_type = p_event_type
teleporter = p_teleporter
is_teleporting = p_is_teleporting
can_teleport = p_can_teleport
target = p_target
position = p_position
last_position = p_last_position


## Report teleporter activated event
static func activated(
teleporter : Node3D,
can_teleport : bool,
target : Node3D,
at : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.ACTIVATED,
teleporter,
true,
can_teleport,
target,
at,
at
)
)

## Report teleporter beam entered target event
static func entered(
teleporter : Node3D,
is_teleporting : bool,
can_teleport : bool,
target : Node3D,
at : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.ENTERED,
teleporter,
is_teleporting,
can_teleport,
target,
at,
at
)
)


## Report teleporter beam moved inside target event
static func moved(
teleporter : Node3D,
is_teleporting : bool,
can_teleport : bool,
target : Node3D,
to : Vector3,
from : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.MOVED,
teleporter,
is_teleporting,
can_teleport,
target,
to,
from
)
)


## Report teleporter beam entered target event
static func exited(
teleporter : Node3D,
is_teleporting : bool,
can_teleport : bool,
target : Node3D,
last : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.EXITED,
teleporter,
is_teleporting,
can_teleport,
target,
last,
last
)
)


## Report teleportation event
static func can_teleport_changed(
teleporter : Node3D,
is_teleporting : bool,
can_teleport : bool,
target : Node3D,
position : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.CAN_TELEPORT_CHANGED,
teleporter,
is_teleporting,
can_teleport,
target,
position,
position
)
)


## Report teleportation event
static func teleported(
teleporter : Node3D,
is_teleporting : bool,
can_teleport : bool,
target : Node3D,
position : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.TELEPORTED,
teleporter,
is_teleporting,
can_teleport,
target,
position,
position
)
)


## Report teleporter deactivated event
static func deactivated(
teleporter : Node3D,
can_teleport : bool,
target : Node3D,
at : Vector3) -> void:
report(
XRToolsTeleporterEvent.new(
Type.DEACTIVATED,
teleporter,
false,
can_teleport,
target,
at,
at
)
)


## Report a pointer event
static func report(event : XRToolsTeleporterEvent) -> void:
# Fire event on pointer
if is_instance_valid(event.teleporter):
if event.teleporter.has_signal("teleporter_event"):
event.teleporter.emit_signal("teleporter_event", event)

# Fire event/method on the target if it's valid
if is_instance_valid(event.target):
if event.target.has_signal("teleporter_event"):
event.target.emit_signal("teleporter_event", event)
elif event.target.has_method("teleporter_event"):
event.target.pointer_event(event)
57 changes: 54 additions & 3 deletions addons/godot-xr-tools/functions/function_teleport.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ extends Node3D
## a teleport function on that controller.



## Signal emitted when this teleporter's state changed
signal teleporter_event(event)


# Default teleport collision mask of all
const DEFAULT_MASK := 0b1111_1111_1111_1111_1111_1111_1111_1111

Expand Down Expand Up @@ -84,8 +89,10 @@ var player_material : StandardMaterial3D = _DefaultMaterial : set = set_player_


var is_on_floor : bool = true
var is_teleporting : bool = false
var can_teleport : bool = true
var is_teleporting : bool = false: set = set_is_teleporting
var can_teleport : bool = false: set = set_can_teleport
var target_node : Node3D = null
var target_hit_pos : Vector3
var teleport_rotation : float = 0.0;
var floor_normal : Vector3 = Vector3.UP
var last_target_transform : Transform3D = Transform3D()
Expand Down Expand Up @@ -154,7 +161,7 @@ func _physics_process(delta):
# if we're not enabled no point in doing mode
if !enabled:
# reset these
is_teleporting = false;
is_teleporting = false
$Teleport.visible = false
$Target.visible = false

Expand Down Expand Up @@ -277,6 +284,9 @@ func _physics_process(delta):
if not valid_teleport_mask & collider_mask:
is_on_floor = false

if is_on_floor:
_update_target(intersects["collider"], collided_at)

# we are colliding, find our if we're colliding on a wall or
# floor, one we can do, the other nope...
cast_length += (collided_at - target_global_origin).length()
Expand Down Expand Up @@ -331,6 +341,7 @@ func _physics_process(delta):

# Teleport the player
player_body.teleport(new_transform)
XRToolsTeleporterEvent.teleported(self, is_teleporting, can_teleport, target_node, new_transform.origin)

# and disable
is_teleporting = false;
Expand Down Expand Up @@ -452,6 +463,27 @@ func _update_target_texture():
material.albedo_texture = target_texture


# Set property telling whether all conditions for successful teleportation are met
func set_can_teleport(new_value : bool) -> void:
if new_value != can_teleport:
if not new_value:
_clear_target()
can_teleport = new_value
XRToolsTeleporterEvent.can_teleport_changed(self, is_teleporting, can_teleport, target_node, target_hit_pos)


# Set is_teleporting property and notify about its changes
func set_is_teleporting(new_value : bool) -> void:
if new_value != is_teleporting:
if new_value:
XRToolsTeleporterEvent.activated(self, can_teleport, target_node, target_hit_pos)
else:
_clear_target()
can_teleport = false
XRToolsTeleporterEvent.deactivated(self, can_teleport, target_node, target_hit_pos)
is_teleporting = new_value


# Player height update handler
func _update_player_height() -> void:
if collision_shape:
Expand Down Expand Up @@ -493,3 +525,22 @@ func _update_player_scene() -> void:
func _update_player_material():
if player_material:
capsule.set_surface_override_material(0, player_material)


# Teleporter target doesn't hit something suitable
func _clear_target():
_update_target(null, Vector3.ZERO)


# Teleporter target hit something suitiable change handler
func _update_target(node: Node3D, hit_pos: Vector3) -> void:
if node == target_node:
if (hit_pos != target_hit_pos):
XRToolsTeleporterEvent.moved(self, is_teleporting, can_teleport, node, hit_pos, target_hit_pos)
else:
if target_node != null:
XRToolsTeleporterEvent.exited(self, is_teleporting, can_teleport, target_node, target_hit_pos)
if node != null:
XRToolsTeleporterEvent.entered(self, is_teleporting, can_teleport, node, hit_pos)
target_node = node
target_hit_pos = hit_pos
1 change: 0 additions & 1 deletion addons/godot-xr-tools/functions/function_teleport.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ height = 1.8

[node name="FunctionTeleport" type="Node3D"]
script = ExtResource("1")
player_material = ExtResource("4")

[node name="Teleport" type="MeshInstance3D" parent="."]
mesh = SubResource("1")
Expand Down
Loading