xiandie/scene/ground/script/c02/s03_closeup抓药游戏.gd

123 lines
3.4 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends CanvasLayer
@warning_ignore("unused_signal")
signal exit(arg)
@onready var slot_bowl: Marker2D = %DropSlotBowl
@onready var drawers: Array[HoverLightClickArea] = [
%Drawer1, %Drawer2, %Drawer3, %Drawer4, %Drawer5, %Drawer6
]
@onready var draggables: Array[DraggableRigid] = [
%DraggableRigid1,
%DraggableRigid2,
%DraggableRigid3,
%DraggableRigid4,
%DraggableRigid5,
%DraggableRigid6
]
@onready var sfx_picked = $SfxPicked as AudioStreamPlayer
@onready var sfx_invalid_drop = $SfxInvalidDrop as AudioStreamPlayer
@onready var sfx_dropped = $SfxDropped as AudioStreamPlayer
@onready var sfx_ready_to_grind = $SfxReadyToGrind as AudioStreamPlayer
const correct_ingredients = [0, 4, 6] # 生姜,竹叶,甘蔗
var picked_ingredients: Array[int] = []
var holding_ingredient := -1
var all_picked = false
func _ready() -> void:
layer = GlobalConfig.CANVAS_LAYER_LITTLE_GAME
for i in 6:
toggle_draggable_visible(i, false)
var drawer = drawers[i]
drawer.interacted.connect(_on_drawer_interacted.bind(i))
var drag = draggables[i]
drag.picked.connect(_on_picked.bind(i))
drag.dropped.connect(_on_dropped.bind(i))
func toggle_draggable_freeze(freezing := true):
for d in draggables:
d.freezing = freezing
func toggle_draggable_visible(id: int, display := true):
var draggable = draggables[id]
draggable.visible = display
draggable.freeze = not display
var has_notified_put_to_bowl = false
func _on_drawer_interacted(id: int):
if picked_ingredients.has(id) or holding_ingredient > 0:
$SfxPickFailed.play()
SceneManager.pop_center_notification("ui_center_notify_c03药车不能再取药材")
else:
if not has_notified_put_to_bowl:
SceneManager.pop_center_notification("ui_center_notify_c03碗口放入药材")
has_notified_put_to_bowl = true
holding_ingredient = id
toggle_draggable_visible(id, true)
draggables[id].force_hold()
sfx_picked.play()
func _on_picked(_node: DraggableRigid, id: int):
$SfxPicked.play()
if holding_ingredient > 0 and holding_ingredient != id:
printerr("DraggableRigid _on_picked: holding_ingredient=", holding_ingredient, " but picked id=", id)
picked_ingredients.erase(id)
holding_ingredient = id
func _on_dropped(node: DraggableRigid, id: int):
var dropping_position = node.global_position
var dropped = false
if dropping_position.x < 210:
dropped = true
picked_ingredients.append(id)
else:
# 判断是否对应到 slots 在 limit_squared 范围内
var drawer = drawers[id]
if drawer.is_focused():
holding_ingredient = -1
dropped = true
node.update_position(drawer.global_position)
toggle_draggable_visible(id, false)
else:
for d in drawers:
if d.is_focused():
d.invalid_blink()
if dropped:
holding_ingredient = -1
# If all items are placed
all_picked = picked_ingredients.size() == correct_ingredients.size()
# 检查配方
for ingredient in correct_ingredients:
if not ingredient in picked_ingredients:
all_picked = false
break
# 配方 match
if all_picked:
print(picked_ingredients)
# 全部插入stage 进入下一阶段
sfx_ready_to_grind.play()
_setup_on_ready_to_grind()
else:
sfx_dropped.play()
else:
SceneManager.pop_center_notification("ui_center_notify_c03放到碗中或原处")
node.invalid_shake()
node.force_hold()
sfx_invalid_drop.play()
func _setup_on_ready_to_grind() -> void:
toggle_draggable_freeze(true)
# 禁用 drawer
for d in drawers:
d.freezing = true