138 lines
3.5 KiB
GDScript
138 lines
3.5 KiB
GDScript
@tool
|
|
extends Sprite2D
|
|
|
|
signal sign_mark_offset_updated
|
|
|
|
# sign_mark 节点在 ready 时会直接读取
|
|
@export var sign_mark_offset := Vector2.ZERO:
|
|
set(val):
|
|
sign_mark_offset = val
|
|
sign_mark_offset_updated.emit(val)
|
|
# @export var entity_name: String = ""
|
|
@export_enum("none", "c01", "c02", "c03", "c04", "c05") var editor_filter := "none":
|
|
set(val):
|
|
editor_filter = val
|
|
if is_node_ready() and Engine.is_editor_hint():
|
|
notify_property_list_changed()
|
|
@export var note_sign_texture: Texture2D
|
|
@export var texture_cover: Texture2D
|
|
@export var texture_note: Texture2D
|
|
@export var note_centered := false
|
|
var content_key: String = ""
|
|
|
|
@onready var sign_mark = %Sign as Sign
|
|
@onready var area2d = %Area2D as Area2D
|
|
|
|
var content_dialogue = preload("res://asset/dialogue/inspect_content.dialogue") as DialogueResource
|
|
var inspecting = false
|
|
|
|
var ground_archive: GroundArchive
|
|
# 尝试互动的次数
|
|
var tried_times: int:
|
|
set(val):
|
|
tried_times = val
|
|
ground_archive.set_pair(name, "tried_times", val)
|
|
if tried_times >= 1 and sign_mark:
|
|
sign_mark.sprite2d.texture = note_sign_texture
|
|
|
|
func _ready() -> void:
|
|
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)
|
|
|
|
# setup default value
|
|
ground_archive = ArchiveManager.archive.ground_archive()
|
|
tried_times = ground_archive.get_value(name, "tried_times", 0)
|
|
|
|
|
|
func _on_interacted() -> void:
|
|
if not texture_cover:
|
|
push_error("entity/inspectable.gd: texture_cover or texture_note is not set")
|
|
return
|
|
if inspecting:
|
|
return
|
|
tried_times += 1
|
|
%Sfx.play()
|
|
# connect inspector quit signal
|
|
var inspector = SceneManager.get_inspector()
|
|
if inspector:
|
|
inspector.quit_and_hidden.connect(_on_quit_inspector)
|
|
var inspection_note = _get_tr_content()
|
|
inspector.pop_standard_inspection(
|
|
texture_cover, texture_note, inspection_note, note_centered
|
|
)
|
|
inspecting = true
|
|
sign_mark.display_sign = false
|
|
|
|
|
|
func _get_tr_content():
|
|
if content_key == "":
|
|
return ""
|
|
var inspection_note = ""
|
|
# get note content
|
|
var line_id = content_dialogue.titles.get(content_key)
|
|
while line_id and line_id != "end":
|
|
var line = content_dialogue.lines[line_id]
|
|
if line.has("text"):
|
|
inspection_note += tr(line.get("text")) + "\n"
|
|
line_id = line.get("next_id")
|
|
return inspection_note
|
|
|
|
|
|
func _on_quit_inspector():
|
|
var inspector = SceneManager.get_inspector()
|
|
if inspector:
|
|
# disconnect inspector quit signal
|
|
inspector.quit_and_hidden.disconnect(_on_quit_inspector)
|
|
inspecting = false
|
|
sign_mark.display_sign = true
|
|
|
|
|
|
func _on_cancel(_body = null):
|
|
inspecting = false
|
|
|
|
|
|
func _reset(_body):
|
|
inspecting = false
|
|
|
|
|
|
func _set(property: StringName, value: Variant) -> bool:
|
|
if property == "content_key":
|
|
content_key = value
|
|
return true
|
|
return false
|
|
|
|
|
|
func _get(property: StringName) -> Variant:
|
|
if property == "content_key":
|
|
return content_key
|
|
return null
|
|
|
|
|
|
func _get_property_list() -> Array[Dictionary]:
|
|
var titles = ""
|
|
# only show notes_ properties in editor
|
|
if Engine.is_editor_hint():
|
|
var ordered_titles = content_dialogue.get_ordered_titles()
|
|
if editor_filter and editor_filter != "none":
|
|
var filted_titles = ordered_titles.filter(_filter_property)
|
|
if filted_titles.size() > 0:
|
|
titles = ",".join(filted_titles)
|
|
else:
|
|
titles = ",".join(ordered_titles)
|
|
return [
|
|
{
|
|
"name": "content_key",
|
|
"type": TYPE_STRING,
|
|
"hint": PROPERTY_HINT_ENUM_SUGGESTION,
|
|
"hint_string": titles
|
|
}
|
|
]
|
|
|
|
|
|
func _filter_property(property: StringName) -> bool:
|
|
return property.find(editor_filter) >= 0
|