REM/scripts/door.gd
2026-03-26 20:37:29 -06:00

75 lines
2.8 KiB
GDScript

extends Node3D
var toggle = true
var interactable = true
var wait: bool = false
@export var animation_player: AnimationPlayer
@onready var Player: Node3D = get_tree().get_first_node_in_group("player")
@onready var player_ray: Node3D = get_tree().get_first_node_in_group("player_ray")
@onready var Sign: Node3D = get_tree().get_first_node_in_group("open_close_sign")
@onready var timer: Node3D = get_tree().get_first_node_in_group("timer")
var door_key = {"Door": "pantry_key"}
var door_state = {"Door": "door_open", "OpenCloseDoor": "door_open"}
func _physics_process(_delta: float) -> void:
if self.name == "OpenCloseDoor":
if player_ray.is_colliding() and player_ray.get_collider() == self and wait == false:
if player_ray.global_rotation_degrees.y >= -90 and player_ray.global_rotation_degrees.y <= 90 and Sign.toggle == true:
$"../../InteractLabel".show()
if (player_ray.global_rotation_degrees.y < -90 or player_ray.global_rotation_degrees.y > 90) and Sign.toggle == false:
$"../../InteractLabel2".show()
else:
$"../../InteractLabel".hide()
$"../../InteractLabel2".hide()
func interact():
if interactable == true:
if self.is_in_group("locked_door"):
for i in Player.keys.size():
if Player.keys[i] == door_key[self.name]:
door_animation()
else:
if self.name == "OpenCloseDoor":
##IF the player is facing a certain direction and the sign facing they can see is open
##THEN play the door animation
##print(Ray.global_rotation_degrees.y)
if player_ray.global_rotation_degrees.y >= -90 and player_ray.global_rotation_degrees.y <= 90 and Sign.toggle == true:
self_closing_door("forwards")
elif (player_ray.global_rotation_degrees.y < -90 or player_ray.global_rotation_degrees.y > 90) and Sign.toggle == false:
self_closing_door("backwards")
##Open any non-special and non-locked door
else:
door_animation()
func door_animation():
interactable = false
match door_state[self.name]:
"door_open":
door_state[self.name] = "door_close"
"door_close":
door_state[self.name] = "door_open"
animation_player.play(door_state[self.name])
## waits for a one second timer to finish before leaving the interactable if loop
## the false statement makes it so when the game is paused the timer is too
await get_tree().create_timer(0.5, false).timeout
interactable = true
func self_closing_door(direction: String):
interactable = false
wait = true
animation_player.play("door_open_" + direction)
## waits for a one second timer to finish before leaving the interactable if loop
## the false statement makes it so when the game is paused the timer is too
await get_tree().create_timer(3, false).timeout
animation_player.play_backwards("door_open_" + direction)
await get_tree().create_timer(0.5, false).timeout
interactable = true
wait = false