修复sign的多线程&多节点抢用问题,自动按进入顺序依次显示

This commit is contained in:
cakipaul 2025-01-07 19:44:10 +08:00
parent 86cd6933e3
commit 21b807dfeb
8 changed files with 51 additions and 55 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 817 B

After

Width:  |  Height:  |  Size: 807 B

Binary file not shown.

View File

@ -22,13 +22,11 @@ signal cancel
# 同时只能有一个物品被激活交互态,其他物品进入等待队列 # 同时只能有一个物品被激活交互态,其他物品进入等待队列
static var occupied: NodePath static var occupied: NodePath
# 使用互斥锁保证线程安全 static var _pending_activate_sign := [] as Array[NodePath]
# 使用互斥锁保证线程安全。在操作 occupied 或 _pending_activate_sign 时需要先锁定,操作完成后解锁
static var mutex = Mutex.new() static var mutex = Mutex.new()
static var _pending_callables: Array[Callable]
var own_callable
var activated = false var activated = false
# var sprite2d = Sprite2D.new() # var sprite2d = Sprite2D.new()
var base_scale = Vector2.ONE var base_scale = Vector2.ONE
@ -54,12 +52,16 @@ func _ready() -> void:
func activate(_body: Node2D) -> void: func activate(_body: Node2D) -> void:
# point_light.energy = 1.0 # point_light.energy = 1.0
if not is_node_ready():
await ready
var path := get_path()
mutex.lock() mutex.lock()
if occupied and occupied != get_path(): if occupied and occupied != path:
own_callable = activate.bind(_body) _pending_activate_sign.append(path)
_pending_callables.append(own_callable) mutex.unlock()
return
else: else:
occupied = get_path() occupied = path
activated = true activated = true
mutex.unlock() mutex.unlock()
if activated and show_sign: if activated and show_sign:
@ -71,13 +73,23 @@ func activate(_body: Node2D) -> void:
func disactivate(_body: Node2D) -> void: func disactivate(_body: Node2D) -> void:
activated = false mutex.lock()
if _unoccupy(): if activated:
while _pending_callables.size() > 0: if occupied == get_path():
var callable = _pending_callables.pop_front() as Callable occupied = ""
if callable.is_valid(): while _pending_activate_sign.size() > 0:
callable.call() var path = _pending_activate_sign.pop_front()
var _sign = get_node_or_null(path)
if _sign:
_sign.activate(null)
break break
else:
# make sure the sign is not in the pending list
_pending_activate_sign.erase(get_path())
# double check. because the sign may be activated by other body
if activated:
disactivate(_body)
mutex.unlock()
# point_light.energy = 0.0 # point_light.energy = 0.0
if show_sign: if show_sign:
create_tween().tween_property(self, "modulate:a", 0.0, 0.2) create_tween().tween_property(self, "modulate:a", 0.0, 0.2)
@ -90,26 +102,3 @@ func _unhandled_input(event: InputEvent) -> void:
elif event.is_action_pressed("cancel"): elif event.is_action_pressed("cancel"):
cancel.emit() cancel.emit()
get_viewport().set_input_as_handled() get_viewport().set_input_as_handled()
func _exit_tree() -> void:
if _unoccupy():
while _pending_callables.size() > 0:
var callable = _pending_callables.pop_front() as Callable
if callable.is_valid():
callable.call()
break
cancel.emit()
func _unoccupy() -> bool:
var self_occupied = false
mutex.lock()
if occupied == get_path():
occupied = ""
self_occupied = true
elif own_callable:
_pending_callables.erase(own_callable)
own_callable = null
mutex.unlock()
return self_occupied

View File

@ -60,7 +60,17 @@ inspection_note = "据闻奉贤县分水墩又有七人染受鼠疫,病状可
《申报》 《申报》
民国五年一月十三日" 民国五年一月十三日"
[node name="纸人" parent="Ground/DeployLayer" index="4" instance=ExtResource("5_nhtbp")] [node name="鼠疫海报local" parent="Ground/DeployLayer" index="4" instance=ExtResource("12_0fckv")]
position = Vector2(387, -25)
entity_title = "鼠疫海报"
texture_cover = ExtResource("7_wdwbi")
inspection_note = "据闻奉贤县分水墩又有七人染受鼠疫,病状可怖,闻医官药治无用,免职。
有地方人称鼠疫为妖邪作祟,甚为惊骇,一时人心惶惶,竟有聚众滋闹之事,政府以防疫事宜关系紧要,拟赴分水墩一带调查防疫事宜云。
《申报》
民国五年一月十三日"
[node name="纸人" parent="Ground/DeployLayer" index="5" instance=ExtResource("5_nhtbp")]
position = Vector2(601, 44) position = Vector2(601, 44)
texture = ExtResource("7_xsghn") texture = ExtResource("7_xsghn")
flip_h = true flip_h = true
@ -72,11 +82,11 @@ collision_mask = 0
[node name="CollisionShape2D" type="CollisionShape2D" parent="Ground/DeployLayer/纸人/StaticBody2D"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Ground/DeployLayer/纸人/StaticBody2D"]
shape = SubResource("RectangleShape2D_0xrg2") shape = SubResource("RectangleShape2D_0xrg2")
[node name="Ambush" parent="Ground/DeployLayer" index="5" instance=ExtResource("6_70vqn")] [node name="Ambush" parent="Ground/DeployLayer" index="6" instance=ExtResource("6_70vqn")]
position = Vector2(434, 60) position = Vector2(502, 56)
hook_cg = "c02_胖子说话" hook_cg = "c02_胖子说话"
[node name="HdEntity" parent="Ground/DeployLayer" index="6" node_paths=PackedStringArray("sprite_ref") instance=ExtResource("10_3c313")] [node name="HdEntity" parent="Ground/DeployLayer" index="7" node_paths=PackedStringArray("sprite_ref") instance=ExtResource("10_3c313")]
position = Vector2(147, 75) position = Vector2(147, 75)
sprite_name = "car" sprite_name = "car"
texture = ExtResource("11_x0h3x") texture = ExtResource("11_x0h3x")
@ -88,16 +98,6 @@ position = Vector2(1, -28)
[node name="CollisionShape2D" parent="Ground/DeployLayer/HdEntity/Area2D" index="0"] [node name="CollisionShape2D" parent="Ground/DeployLayer/HdEntity/Area2D" index="0"]
shape = SubResource("RectangleShape2D_3qhvr") shape = SubResource("RectangleShape2D_3qhvr")
[node name="LocalInspectable" parent="Ground/DeployLayer" index="7" instance=ExtResource("12_0fckv")]
position = Vector2(387, -25)
entity_title = "鼠疫海报"
texture_cover = ExtResource("7_wdwbi")
inspection_note = "据闻奉贤县分水墩又有七人染受鼠疫,病状可怖,闻医官药治无用,免职。
有地方人称鼠疫为妖邪作祟,甚为惊骇,一时人心惶惶,竟有聚众滋闹之事,政府以防疫事宜关系紧要,拟赴分水墩一带调查防疫事宜云。
《申报》
民国五年一月十三日"
[node name="FGSprite2D" parent="Ground/ParallaxForeground/FGParallaxLayer" index="0"] [node name="FGSprite2D" parent="Ground/ParallaxForeground/FGParallaxLayer" index="0"]
texture = null texture = null

View File

@ -30,7 +30,7 @@ normal_texture = ExtResource("4_nef6w")
[node name="Ground" parent="." instance=ExtResource("1_lheeb")] [node name="Ground" parent="." instance=ExtResource("1_lheeb")]
scene_config = SubResource("Resource_vdo00") scene_config = SubResource("Resource_vdo00")
[node name="BGSprite2D" parent="Ground" index="0"] [node name="BGSprite2D" parent="Ground" index="1"]
texture = SubResource("CanvasTexture_41q0k") texture = SubResource("CanvasTexture_41q0k")
[node name="PortalLeft" parent="Ground/DeployLayer" index="0"] [node name="PortalLeft" parent="Ground/DeployLayer" index="0"]
@ -78,7 +78,7 @@ texture = ExtResource("7_icddm")
position = Vector2(-12, -143) position = Vector2(-12, -143)
scale = Vector2(1.08, 1.08) scale = Vector2(1.08, 1.08)
[node name="DirectionalLight2D" parent="Ground" index="5"] [node name="DirectionalLight2D" parent="Ground" index="6"]
color = Color(0.368627, 0.447059, 0.882353, 1) color = Color(0.368627, 0.447059, 0.882353, 1)
[editable path="Ground"] [editable path="Ground"]

View File

@ -38,6 +38,12 @@ var items_dict := {}
func _ready() -> void: func _ready() -> void:
reload() reload()
if GlobalConfig.DEBUG:
locked = false
display = true
else:
locked = true
display = false
_toggle_display(false) _toggle_display(false)

View File

@ -18,7 +18,7 @@ grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
mouse_filter = 2 mouse_filter = 2
script = ExtResource("1_y2537") script = ExtResource("1_y2537")
locked = true tip_content = "哆莱米发索拉西123456789"
[node name="JournalPage" type="TextureRect" parent="JournalRoot"] [node name="JournalPage" type="TextureRect" parent="JournalRoot"]
show_behind_parent = true show_behind_parent = true

View File

@ -20,7 +20,7 @@ environment = ExtResource("9_jsof5")
[node name="GroundLoader" parent="." instance=ExtResource("10_8rc5n")] [node name="GroundLoader" parent="." instance=ExtResource("10_8rc5n")]
position = Vector2(1, 0) position = Vector2(1, 0)
ignore_archive = true ignore_archive = true
current_scene = "c01_s05" current_scene = "c02_s02"
[node name="MainPlayer" parent="." instance=ExtResource("6_6geb0")] [node name="MainPlayer" parent="." instance=ExtResource("6_6geb0")]
unique_name_in_owner = true unique_name_in_owner = true
@ -97,3 +97,4 @@ scale = Vector2(0.24, 0.24)
metadata/_edit_use_anchors_ = true metadata/_edit_use_anchors_ = true
[node name="Journal" parent="." instance=ExtResource("10_durpa")] [node name="Journal" parent="." instance=ExtResource("10_durpa")]
visible = false