xiandie/scene/entity/portal.gd

112 lines
2.9 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# @tool
class_name Portal2D extends Sprite2D
@export var enabled := true:
set(val):
enabled = val
if is_node_ready():
_check_sign_mark()
@export var immediately := true
@export_enum("left", "right", "1", "2", "3", "4", "5", "6", "7", "8", "9") var portal_name := "left":
set(value):
#if portal_name:
#remove_from_group("portal_"+portal_name)
portal_name = value
#add_to_group("portal_"+value)
name = "portal_" + value
@export var target_scene := "c02_s00"
@export_enum("none", "left", "right", "1", "2", "3", "4", "5", "6", "7", "8", "9")
var target_portal := "none":
set(value):
target_portal = value
if is_node_ready():
_check_sign_mark()
@export var default_texture: Texture2D
@export var opened_texture: Texture2D
@export var opened := false:
set(new_val):
opened = new_val
if is_node_ready():
_checkout_texture()
@onready var sfx = %Sfx as Sfx
@onready var sign_mark = %Sign as Sign
@onready var area2d = %Area2D as Area2D
var activated := false
var action_times := 0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
name = "portal_" + portal_name
_checkout_texture()
_check_sign_mark()
if Engine.is_editor_hint():
return
area2d.body_entered.connect(_reset)
area2d.body_exited.connect(_on_cancel)
sign_mark.interacted.connect(_on_interacted)
sign_mark.cancel.connect(_on_cancel)
# if GlobalConfig.DEBUG:
# var label = Label.new()
# label.text = portal_name
# label.name = "Label"
# add_child(label)
func _checkout_texture():
if opened and opened_texture:
texture = opened_texture
else:
texture = default_texture
func _check_sign_mark():
if target_portal == "none" or not enabled:
sign_mark.enabled = false
else:
sign_mark.enabled = true
func _on_interacted() -> void:
if target_portal == "none":
return
# 传送queue free 导致 sfx 无法播放,使用全局声源
sfx.global_play()
if GlobalConfig.DEBUG:
print("传送前往", target_scene, target_portal, " immediately=", immediately)
var ground_loader = SceneManager.get_ground_loader() as GroundLoader
if ground_loader:
ground_loader.transition_to_scene(target_scene, target_portal, immediately)
func _on_cancel(_body = null):
activated = false
func _reset(_body):
activated = true
# 暂时不启用自动传送
# func _input(event: InputEvent) -> void:
# # 长按自动传送
# if activated:
# if portal_name == "left" and target_portal == "right":
# if event.is_action("left"):
# action_times += 1
# elif event.is_action("right"):
# action_times = 0
# if action_times >= 7:
# activated = false
# action_times = 0
# %Sfx.play()
# _on_interacted()
# if portal_name == "right" and target_portal == "left":
# if event.is_action("right"):
# action_times += 1
# elif event.is_action("left"):
# action_times = 0
# if action_times >= 7:
# activated = false
# action_times = 0
# %Sfx.play()
# _on_interacted()