xiandie/scene/entity/npc.gd

83 lines
2.2 KiB
GDScript

@tool
extends AnimatedSprite2D
@export var anonymous_title_suffix := "_匿名"
@export var character_name := ""
@export var anonymous := true:
set(val):
anonymous = val
if is_node_ready() and not Engine.is_editor_hint():
ArchiveManager.archive.npc_anonymous_states[character_name] = val
@onready var speaking_animation = %SpeakingAnimationPlayer
@onready var speaking_sign = %SpeakingSign2D as Node2D
@onready var sign_mark = %Sign as Sign
@onready var area2d = %Area2D as Area2D
var dialogue_title := ""
var dialogue_res = preload("res://asset/dialogue/npc.dialogue")
func _init() -> void:
if Engine.is_editor_hint():
return
if character_name and ArchiveManager.archive.npc_anonymous_states.has(character_name):
anonymous = ArchiveManager.archive.npc_anonymous_states[character_name]
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if animation:
play()
area2d.body_entered.connect(_reset)
area2d.body_exited.connect(_on_cancel)
sign_mark.interacted.connect(_on_interacted)
sign_mark.cancel.connect(_on_cancel)
func _on_interacted() -> void:
# %Sfx.play()
speaking_animation.play("speaking")
# play dialogue
if dialogue_title:
# 适配匿名效果
if anonymous and not dialogue_title.ends_with(anonymous_title_suffix):
dialogue_title += anonymous_title_suffix
elif not anonymous and dialogue_title.ends_with(anonymous_title_suffix):
dialogue_title = dialogue_title.substr(
0, dialogue_title.length() - anonymous_title_suffix.length()
)
DialogueManager.show_dialogue_balloon(dialogue_res, dialogue_title)
func _on_cancel(_body = null):
speaking_animation.play("RESET")
func _reset(_body = null):
speaking_animation.play("speaking")
func _get(property: StringName) -> Variant:
if property == "dialogue_title":
return dialogue_title
return null
func _set(property: StringName, value: Variant) -> bool:
if property == "dialogue_title":
dialogue_title = value
return true
return false
func _get_property_list() -> Array[Dictionary]:
return [
{
"name": "dialogue_title",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_ENUM_SUGGESTION,
"hint_string": ",".join(dialogue_res.titles.keys())
}
]