代码细节优化

This commit is contained in:
cakipaul 2025-01-12 20:15:18 +08:00
parent a982c3a2e2
commit 88eede1301
11 changed files with 199 additions and 114 deletions

View File

@ -17,7 +17,7 @@
老虎钳 [#texture=c02/老虎钳物品.png] [ID:prop_老虎钳] 老虎钳 [#texture=c02/老虎钳物品.png] [ID:prop_老虎钳]
3014旋转锁钥匙 [#texture=c02/3014旋转锁钥匙物品.png] [ID:prop_3014旋转锁钥匙] 3014旋转锁钥匙 [#texture=c02/3014旋转锁钥匙物品.png] [ID:prop_3014旋转锁钥匙]
小猫玩具 [#texture=c02/小猫玩具完整物品.png] [ID:prop_小猫玩具完整] 小猫玩具 [#texture=c02/小猫玩具完整物品.png] [ID:prop_小猫玩具完整]
=> END! => END
# notes 通用 # notes 通用
~ notes_纸条 ~ notes_纸条

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,8 @@
@tool @tool
extends Node2D extends Node2D
signal player_entered
@export var one_shot := true @export var one_shot := true
@export var freeze_time := 5.0 @export var freeze_time := 5.0
@export var hook_animation = "" @export var hook_animation = ""
@ -66,7 +68,6 @@ func _load_archive():
func _entered(_body): func _entered(_body):
print("ambush body_entered!")
if not one_shot: if not one_shot:
var time = Time.get_ticks_msec() var time = Time.get_ticks_msec()
var time_left = freeze_time - (time - played_time) * 0.001 var time_left = freeze_time - (time - played_time) * 0.001
@ -86,7 +87,8 @@ func _entered(_body):
DialogueManager.dialogue_ended.connect(_on_dialogue_ended, CONNECT_ONE_SHOT) DialogueManager.dialogue_ended.connect(_on_dialogue_ended, CONNECT_ONE_SHOT)
if one_shot: if one_shot:
played = true played = true
return player_entered.emit()
print("ambush body_entered!")
func _on_dialogue_ended(_res): func _on_dialogue_ended(_res):

View File

@ -1,16 +1,43 @@
@tool
extends Sprite2D extends Sprite2D
signal interacted(success: bool)
@export var entity_name: String = "" @export var entity_name: String = ""
@export var prop_key := ""
@export var interacted := false
@export var texture_before: Texture2D @export var texture_before: Texture2D
@export var texture_after: Texture2D @export var texture_after: Texture2D
@export var one_shot := true
@export var interacted_times := 0
var prop_key := ""
@onready var sfx = $Sfx as Sfx
@onready var sign_mark = %Sign as Sign @onready var sign_mark = %Sign as Sign
@onready var area2d = %Area2D as Area2D @onready var area2d = %Area2D as Area2D
static var item_config_res = preload("res://asset/dialogue/item_description.dialogue")
var items: PackedStringArray
func _reload_items() -> void:
var id = item_config_res.titles["PropItems"]
var current_line = item_config_res.lines[id]
while current_line:
if current_line.has("translation_key"):
items.append(current_line.translation_key)
if not current_line.has("next_id") or current_line.next_id == "end":
break
current_line = item_config_res.lines[current_line.next_id]
func _ready() -> void: func _ready() -> void:
if Engine.is_editor_hint():
_reload_items()
notify_property_list_changed()
return
if interacted_times and texture_after:
texture = texture_after
else:
texture = texture_before
area2d.body_entered.connect(_reset) area2d.body_entered.connect(_reset)
area2d.body_exited.connect(_on_cancel) area2d.body_exited.connect(_on_cancel)
sign_mark.interacted.connect(_on_interacted) sign_mark.interacted.connect(_on_interacted)
@ -26,5 +53,39 @@ func _on_cancel(_body = null) -> void:
func _on_interacted() -> void: func _on_interacted() -> void:
if one_shot and interacted_times:
return
sfx.play()
var key = SceneManager.get_current_selected_prop() var key = SceneManager.get_current_selected_prop()
print("prop_key", key) # print("prop_key", key)
if key != prop_key:
return
interacted_times += 1
if texture_after:
texture = texture_after
interacted.emit()
print("%s interacted with %s" % [entity_name, prop_key])
func _get_property_list() -> Array[Dictionary]:
return [
{
"name": "prop_key",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_ENUM_SUGGESTION,
"hint_string": ",".join(items),
}
]
func _get(property: StringName) -> Variant:
if property == "prop_key":
return prop_key
return null
func _set(property: StringName, value: Variant) -> bool:
if property == "prop_key":
prop_key = value
return true
return false

View File

@ -10,8 +10,10 @@ size = Vector2(35, 70)
[node name="Interactable" type="Sprite2D"] [node name="Interactable" type="Sprite2D"]
script = ExtResource("1_6nrd3") script = ExtResource("1_6nrd3")
prop_key = ""
[node name="Sfx" parent="." instance=ExtResource("2_bvj74")] [node name="Sfx" parent="." instance=ExtResource("2_bvj74")]
file = "开锁声.mp3"
[node name="Sign" parent="." instance=ExtResource("3_qsms8")] [node name="Sign" parent="." instance=ExtResource("3_qsms8")]
unique_name_in_owner = true unique_name_in_owner = true

View File

@ -1,6 +1,9 @@
@tool @tool
extends Sprite2D extends Sprite2D
signal start_inspecting
signal quit_inspecting
# @export var entity_config: EntityConfig: # @export var entity_config: EntityConfig:
# set(value): # set(value):
# entity_config = value # entity_config = value
@ -35,10 +38,8 @@ static var content_dialogue = (
var status := STATUS_NORAML var status := STATUS_NORAML
var blinking_tween: Tween var blinking_tween: Tween
var inspected_time = 0.0 var inspected_time = 0.0
func _ready() -> void: func _ready() -> void:
$InspectLayer.layer = GlobalConfig.CANVAS_LAYER_PROP_INSPECTOR $InspectLayer.layer = GlobalConfig.CANVAS_LAYER_PROP_INSPECTOR
sign_mark.interacted.connect(_on_interacted) sign_mark.interacted.connect(_on_interacted)
@ -78,6 +79,7 @@ func _on_interacted() -> void:
cover_rect.texture = texture_cover cover_rect.texture = texture_cover
tip_label.text = tip_cover tip_label.text = tip_cover
_blink_label(true) _blink_label(true)
start_inspecting.emit()
elif status == STATUS_INSPECTING_COVER: elif status == STATUS_INSPECTING_COVER:
sfx.play() sfx.play()
status = STATUS_INSPECTING_NOTES status = STATUS_INSPECTING_NOTES
@ -104,6 +106,8 @@ func _blink_label(init := true):
func _on_cancel(_body = null): func _on_cancel(_body = null):
# inspected_time = Time.get_ticks_msec() # inspected_time = Time.get_ticks_msec()
if status != STATUS_NORAML:
quit_inspecting.emit()
status = STATUS_NORAML status = STATUS_NORAML
var tween = create_tween() var tween = create_tween()
tween.tween_property(container, "modulate:a", 0.0, 0.15) tween.tween_property(container, "modulate:a", 0.0, 0.15)

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=3 uid="uid://ci5anaxsa1apl"] [gd_scene load_steps=10 format=3 uid="uid://ci5anaxsa1apl"]
[ext_resource type="Script" path="res://scene/entity/local_inspectable.gd" id="1_85el0"] [ext_resource type="Script" path="res://scene/entity/local_inspectable.gd" id="1_85el0"]
[ext_resource type="PackedScene" uid="uid://c85t6stvytvjn" path="res://scene/entity/ux/sfx.tscn" id="2_h0c2s"] [ext_resource type="PackedScene" uid="uid://c85t6stvytvjn" path="res://scene/entity/ux/sfx.tscn" id="2_h0c2s"]
@ -7,7 +7,6 @@
[ext_resource type="PackedScene" uid="uid://c4tipnj1cr1j3" path="res://scene/entity/ux/sign.tscn" id="4_do8tr"] [ext_resource type="PackedScene" uid="uid://c4tipnj1cr1j3" path="res://scene/entity/ux/sign.tscn" id="4_do8tr"]
[ext_resource type="SpriteFrames" uid="uid://c3s8u4ifaucpj" path="res://config/animation/entity_sprite_frames.tres" id="6_e77p4"] [ext_resource type="SpriteFrames" uid="uid://c3s8u4ifaucpj" path="res://config/animation/entity_sprite_frames.tres" id="6_e77p4"]
[ext_resource type="Script" path="res://scene/entity/ux/animated_sound_sprite_2d.gd" id="7_bhwlx"] [ext_resource type="Script" path="res://scene/entity/ux/animated_sound_sprite_2d.gd" id="7_bhwlx"]
[ext_resource type="Texture2D" uid="uid://cpht56skg054p" path="res://asset/art/scene/c01/s05_院长房间/e_座钟.png" id="8_53i12"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_4fuic"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_4fuic"]
resource_local_to_scene = true resource_local_to_scene = true
@ -82,7 +81,6 @@ layout_mode = 2
size_flags_horizontal = 4 size_flags_horizontal = 4
size_flags_vertical = 4 size_flags_vertical = 4
mouse_filter = 2 mouse_filter = 2
texture = ExtResource("8_53i12")
[node name="ContentLabel" type="Label" parent="InspectLayer/Root/Container"] [node name="ContentLabel" type="Label" parent="InspectLayer/Root/Container"]
unique_name_in_owner = true unique_name_in_owner = true

View File

@ -1,6 +1,8 @@
@tool @tool
extends Sprite2D extends Sprite2D
signal read_note
@export_enum("os", "ballon") var mode = "os" @export_enum("os", "ballon") var mode = "os"
@export_enum("items", "c01", "c02", "c03", "c04", "c05", "c06") var dialogue := "items": @export_enum("items", "c01", "c02", "c03", "c04", "c05", "c06") var dialogue := "items":
set(val): set(val):
@ -55,6 +57,7 @@ func _on_interacted() -> void:
_show_os() _show_os()
"ballon": "ballon":
_show_balloon() _show_balloon()
read_note.emit()
func _show_os(): func _show_os():

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=22 format=3 uid="uid://dlx5xxbg53rb8"] [gd_scene load_steps=23 format=3 uid="uid://dlx5xxbg53rb8"]
[ext_resource type="PackedScene" uid="uid://dayyx4jerj7io" path="res://scene/ground/ground.tscn" id="1_ff4yb"] [ext_resource type="PackedScene" uid="uid://dayyx4jerj7io" path="res://scene/ground/ground.tscn" id="1_ff4yb"]
[ext_resource type="Script" path="res://config/deploy/scene_config.gd" id="2_ve5ly"] [ext_resource type="Script" path="res://config/deploy/scene_config.gd" id="2_ve5ly"]
@ -38,6 +38,10 @@ size = Vector2(10, 60)
resource_local_to_scene = true resource_local_to_scene = true
size = Vector2(10, 60) size = Vector2(10, 60)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_40ng7"]
resource_local_to_scene = true
size = Vector2(35, 70)
[node name="S05院长房间" type="Node2D"] [node name="S05院长房间" type="Node2D"]
[node name="Ground" parent="." instance=ExtResource("1_ff4yb")] [node name="Ground" parent="." instance=ExtResource("1_ff4yb")]
@ -113,6 +117,11 @@ shape = SubResource("RectangleShape2D_dq2na")
[node name="Interactable" parent="Ground/DeployLayer" index="8" instance=ExtResource("14_lq1ou")] [node name="Interactable" parent="Ground/DeployLayer" index="8" instance=ExtResource("14_lq1ou")]
position = Vector2(210, 36) position = Vector2(210, 36)
one_shot = false
prop_key = "prop_令牌"
[node name="CollisionShape2D" parent="Ground/DeployLayer/Interactable/Area2D" index="0"]
shape = SubResource("RectangleShape2D_40ng7")
[node name="PointLight2D" type="PointLight2D" parent="Ground/AmbientLayer" index="0"] [node name="PointLight2D" type="PointLight2D" parent="Ground/AmbientLayer" index="0"]
visible = false visible = false
@ -136,3 +145,4 @@ texture = ExtResource("3_7u4bh")
[editable path="Ground/DeployLayer/桌椅"] [editable path="Ground/DeployLayer/桌椅"]
[editable path="Ground/DeployLayer/画框"] [editable path="Ground/DeployLayer/画框"]
[editable path="Ground/DeployLayer/鸡毛掸子"] [editable path="Ground/DeployLayer/鸡毛掸子"]
[editable path="Ground/DeployLayer/Interactable"]

View File

@ -31,7 +31,6 @@ class_name PropHud extends Control
var items_dict := {} var items_dict := {}
# 从配置文件加载 prop items # 从配置文件加载 prop items
var item_config_res = preload("res://asset/dialogue/item_description.dialogue") var item_config_res = preload("res://asset/dialogue/item_description.dialogue")
var item_config_title = "PropItems"
var texture_path_prefix = "res://asset/art/prop/" var texture_path_prefix = "res://asset/art/prop/"
var cached_inventory_textures := {} var cached_inventory_textures := {}
@ -75,17 +74,23 @@ func _on_focus_exited() -> void:
func _load_items(): func _load_items():
var current_line = await item_config_res.get_next_dialogue_line(item_config_title) var id = item_config_res.titles["PropItems"]
var current_line = item_config_res.lines[id]
while current_line: while current_line:
var texture_path = current_line.get_tag_value("texture") if current_line.has("tags") and current_line.has("translation_key"):
if texture_path: var wrapped := "texture="
var texture_path = ""
for t in current_line.tags:
if t.begins_with(wrapped):
texture_path = t.replace(wrapped, "").strip_edges()
break
var item = PropItem.new() var item = PropItem.new()
item.key = current_line.translation_key item.key = current_line.translation_key
item.texture_path = texture_path_prefix + texture_path item.texture_path = texture_path_prefix + texture_path
items_dict[item.key] = item items_dict[item.key] = item
if current_line.next_id == "end": if not current_line.has("next_id") or current_line.next_id == "end":
break break
current_line = await item_config_res.get_next_dialogue_line(current_line.next_id) current_line = item_config_res.lines[current_line.next_id]
func _load_from_archive() -> void: func _load_from_archive() -> void:
@ -166,7 +171,6 @@ func _on_panel_pressed() -> void:
grab_focus() grab_focus()
selected = true selected = true
_mouse_moved_on_listening() _mouse_moved_on_listening()
print("panel")
func _on_mouse_entered() -> void: func _on_mouse_entered() -> void:

View File

@ -247,50 +247,51 @@ func release_player():
func _draw() -> void: func _draw() -> void:
var animation = sprite.animation pass
if not animation: # # 绘制阴影,咱不启用
return # var animation = sprite.animation
if not animation_shadow_polygons.has(animation): # if not animation:
_build_shadow_polygons(animation) # return
var animation_polygons = animation_shadow_polygons[animation] # if not animation_shadow_polygons.has(animation):
if animation_polygons.has(sprite.frame): # _build_shadow_polygons(animation)
draw_polygon(animation_polygons[sprite.frame], [shadow_color]) # var animation_polygons = animation_shadow_polygons[animation]
else: # if animation_polygons.has(sprite.frame):
printerr("No shadow polygon found for frame %d" % sprite.frame) # draw_polygon(animation_polygons[sprite.frame], [shadow_color])
# else:
# printerr("No shadow polygon found for frame %d" % sprite.frame)
# func _build_shadow_polygons(animation):
func _build_shadow_polygons(animation): # var frames = sprite.sprite_frames
var frames = sprite.sprite_frames # var coords_dict = {}
var coords_dict = {} # for i in frames.get_frame_count(animation):
for i in frames.get_frame_count(animation): # var texture = frames.get_frame_texture(animation, i) as Texture2D
var texture = frames.get_frame_texture(animation, i) as Texture2D # if not texture:
if not texture: # continue
continue # var image = texture.get_image()
var image = texture.get_image() # var x_min = 10000
var x_min = 10000 # var x_max = -1
var x_max = -1 # for y in range(texture.get_height()):
for y in range(texture.get_height()): # for x in range(texture.get_width()):
for x in range(texture.get_width()): # var color = image.get_pixel(x, y)
var color = image.get_pixel(x, y) # if color.a > 0.0:
if color.a > 0.0: # x_min = min(x_min, x)
x_min = min(x_min, x) # x_max = max(x_max, x)
x_max = max(x_max, x) # if x_min >= x_max:
if x_min >= x_max: # continue
continue # var oval_ab: Vector2
var oval_ab: Vector2 # oval_ab.x = (x_max - x_min) * 0.5
oval_ab.x = (x_max - x_min) * 0.5 # oval_ab.y = max(3.0, oval_ab.x * 0.12)
oval_ab.y = max(3.0, oval_ab.x * 0.12) # var x_offset = (x_max - x_min) * 0.5 + x_min - texture.get_width() * 0.5
var x_offset = (x_max - x_min) * 0.5 + x_min - texture.get_width() * 0.5 # var coords: PackedVector2Array
var coords: PackedVector2Array # # build shadow oval shape with segments.
# build shadow oval shape with segments. # var segments = 16
var segments = 16 # for j in range(segments):
for j in range(segments): # var angle = PI * 2 / segments * j
var angle = PI * 2 / segments * j # var x = cos(angle) * oval_ab.x + x_offset
var x = cos(angle) * oval_ab.x + x_offset # var y = sin(angle) * oval_ab.y + shadow_y
var y = sin(angle) * oval_ab.y + shadow_y # coords.append(Vector2(x, y))
coords.append(Vector2(x, y)) # coords_dict[i] = coords
coords_dict[i] = coords # animation_shadow_polygons[animation] = coords_dict
animation_shadow_polygons[animation] = coords_dict
func _reset_os_and_shadow_position(): func _reset_os_and_shadow_position():