diff --git a/main.gd b/main.gd index cc8b5ef..a8085d0 100644 --- a/main.gd +++ b/main.gd @@ -1,15 +1,10 @@ extends Node -const CURSOR_ARROW := preload("res://bg/cursor/cursor_arrow.png") -const CURSOR_POINTING_HAND := preload("res://bg/cursor/cursor_pointing_hand.png") -const CURSOR_DRAG := preload("res://bg/cursor/cursor_drag.png") + func _ready(): - Input.set_custom_mouse_cursor(CURSOR_ARROW) - Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND, Input.CURSOR_POINTING_HAND) - Input.set_custom_mouse_cursor(CURSOR_DRAG, Input.CURSOR_DRAG) func _on_main_menu_enter_level(): diff --git a/main.tscn b/main.tscn index 7d30d68..c2d3a80 100644 --- a/main.tscn +++ b/main.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=5 format=3 uid="uid://c17fbsiogbgo1"] +[gd_scene load_steps=6 format=3 uid="uid://c17fbsiogbgo1"] [ext_resource type="PackedScene" uid="uid://c07co5p46apu7" path="res://objects/main_menu/main_menu.tscn" id="1_fk6j6"] [ext_resource type="Script" path="res://main.gd" id="1_nb6uf"] [ext_resource type="PackedScene" uid="uid://d3geq38s5fjc6" path="res://bg/dynamic_bg/dynamic_bg.tscn" id="2_8k4il"] [ext_resource type="AudioStream" uid="uid://cr3nkhf0fejm5" path="res://levels/base_level/bgm.wav" id="3_n81it"] +[ext_resource type="PackedScene" uid="uid://prht3u5pnjls" path="res://scripts/cursor_manager/cursor_manager.tscn" id="5_pqych"] [node name="Main" type="Node"] script = ExtResource("1_nb6uf") @@ -20,5 +21,7 @@ stream = ExtResource("3_n81it") volume_db = -4.685 attenuation = 0.0001 +[node name="CursorManager" parent="." instance=ExtResource("5_pqych")] + [connection signal="enter_level" from="MainMenu" to="." method="_on_main_menu_enter_level"] [connection signal="finished" from="BGMPlayer" to="." method="_on_bgm_player_finished"] diff --git a/objects/card/card.gd b/objects/card/card.gd index 702974c..b7cb0ca 100644 --- a/objects/card/card.gd +++ b/objects/card/card.gd @@ -51,8 +51,6 @@ func put_down(): $SFXPutDown.play() entered_block.set_card(self) - - Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) # 设置鼠标指针形状为手形 else: queue_free() @@ -112,7 +110,6 @@ func _ready(): func _process(_delta: float) -> void: if self.is_dragging: # 如果正在拖拽 self.global_position = get_global_mouse_position().round() # 将卡牌位置设置为鼠标位置的全局位置,四舍五入取整 - Input.set_default_cursor_shape(Input.CURSOR_DRAG) # 设置鼠标指针形状为拖拽形状 var offset := 0 if self.is_shaking: @@ -143,12 +140,10 @@ func _input_event(_viewport: Object, event: InputEvent, _shape_idx: int) -> void func _on_mouse_entered(): $HighlightSprite.visible = true - Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) # 设置鼠标指针形状为手形 func _on_mouse_exited(): $HighlightSprite.visible = false - Input.set_default_cursor_shape(Input.CURSOR_ARROW) # 设置鼠标指针形状为箭头 func _on_tree_exiting(): diff --git a/objects/card_base/card_base.gd b/objects/card_base/card_base.gd index 4d677b4..bde96ec 100644 --- a/objects/card_base/card_base.gd +++ b/objects/card_base/card_base.gd @@ -74,6 +74,11 @@ func get_word() -> String: return $Word.get_word() +## 获取剩余卡牌数量。 +func get_card_count() -> int: + return self.card_count + + ## 进行通关后的处理。 func set_victory() -> void: # 开始淡出 @@ -116,14 +121,12 @@ func _on_mouse_entered(): is_mouse_on = true if available_stat == DEFAULT: available_stat = HIGHLIGHT - Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) func _on_mouse_exited(): is_mouse_on = false if available_stat == HIGHLIGHT: available_stat = DEFAULT - Input.set_default_cursor_shape(Input.CURSOR_ARROW) func _on_cards_child_exiting_tree(node: Node) -> void: diff --git a/scripts/cursor_manager/cursor_manager.gd b/scripts/cursor_manager/cursor_manager.gd new file mode 100644 index 0000000..1e7c73a --- /dev/null +++ b/scripts/cursor_manager/cursor_manager.gd @@ -0,0 +1,38 @@ +## 用于检测与鼠标重合的、[member Area2D.z_index] 最高的 [Area2D],并根据其状态设置鼠标光标形状。 +class_name CursorManager extends Area2D + + +const CURSOR_ARROW := preload("res://bg/cursor/cursor_arrow.png") ## 鼠标光标为指针时的素材。 +const CURSOR_POINTING_HAND := preload("res://bg/cursor/cursor_pointing_hand.png") ## 鼠标光标为指向时的素材。 +const CURSOR_DRAG := preload("res://bg/cursor/cursor_drag.png") ## 鼠标光标为拖拽时的素材。 + + +func _ready(): + Input.set_custom_mouse_cursor(CURSOR_ARROW) + Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND, Input.CURSOR_POINTING_HAND) + Input.set_custom_mouse_cursor(CURSOR_DRAG, Input.CURSOR_DRAG) + + +func _process(_delta): + self.global_position = get_global_mouse_position() + + # 获取 z_index 最高的 Area2D + var top_area: Area2D = null + for area in get_overlapping_areas(): + if top_area == null or area.z_index > top_area.z_index: + top_area = area + + if top_area == null: + Input.set_custom_mouse_cursor(CURSOR_ARROW) + elif top_area is Card: + if top_area.is_dragging: + Input.set_custom_mouse_cursor(CURSOR_DRAG) + else: + Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND) + elif top_area is CardBase: + if top_area.get_card_count() > 0: + Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND) + else: + Input.set_custom_mouse_cursor(CURSOR_ARROW) + else: + Input.set_custom_mouse_cursor(CURSOR_ARROW) diff --git a/scripts/cursor_manager/cursor_manager.tscn b/scripts/cursor_manager/cursor_manager.tscn new file mode 100644 index 0000000..eeefd0c --- /dev/null +++ b/scripts/cursor_manager/cursor_manager.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=3 uid="uid://prht3u5pnjls"] + +[ext_resource type="Script" path="res://scripts/cursor_manager/cursor_manager.gd" id="1_v3lev"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_stfy2"] +size = Vector2(0, 0) + +[node name="CursorManager" type="Area2D"] +script = ExtResource("1_v3lev") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_stfy2")