capri/objects/portal/portal.gd

71 lines
2 KiB
GDScript

@tool
class_name Portal
extends Node2D
## color the portal is tinted as
@export_color_no_alpha var color: Color = Color.PURPLE:
set(value):
color = value
sprite.modulate = color
if linked_portal and linked_portal.color != color:
linked_portal.color = color
queue_redraw()
## Other connected portal. The linked
@export var linked_portal: Portal:
set(new_portal):
if new_portal == self:
return
if new_portal == null:
linked_portal = null
return
if linked_portal:
linked_portal.linked_portal = null
linked_portal = new_portal
if linked_portal:
if linked_portal.linked_portal != self:
linked_portal.linked_portal = self
linked_portal.color = color
queue_redraw()
## Multiplier to the player's velocity when they travel through the portal.
@export var speed_multiplier: float = 1.25
## Delay in seconds until an exited portal can be re-entered.
@export var teleport_cooldown: float = 0.1
@export_group("Internal References")
@export var sprite: Sprite2D
var check_for_player: bool = true
func _init() -> void:
set_notify_transform(true)
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSFORM_CHANGED:
queue_redraw()
if linked_portal:
linked_portal.queue_redraw()
func _draw() -> void:
if Engine.is_editor_hint():
if linked_portal:
draw_line(Vector2.ZERO, to_local(linked_portal.global_position), color, 1.5)
func _on_player_detector_body_entered(body: Node2D) -> void:
if check_for_player and body is Player and linked_portal:
body.global_position = linked_portal.global_position
body.launch(body.velocity * speed_multiplier)
linked_portal.check_for_player = false
create_tween().tween_property(
linked_portal, ^"check_for_player", true, 0.0
).set_delay(teleport_cooldown)
func _on_player_detector_body_exited(body: Node2D) -> void:
var tween = create_tween().set_process_mode(Tween.TWEEN_PROCESS_PHYSICS)
tween.tween_property(self, ^"check_for_player", true, 0.0).set_delay(teleport_cooldown)