xiandie/scene/entity/portal.gd

111 lines
2.9 KiB
GDScript3
Raw Normal View History

@tool
class_name Portal2D extends Sprite2D
@export var enabled := true:
set(val):
enabled = val
_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
_check_sign_mark()
@export var default_texture: Texture2D
@export var opened_texture: Texture2D
@export var opened := false:
set(new_val):
opened = new_val
_checkout_texture()
2025-01-14 00:56:51 +00:00
@onready var sfx = %Sfx as Sfx
2025-01-08 00:51:09 +00:00
@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 not is_node_ready():
return
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
2025-01-14 00:56:51 +00:00
# 传送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()