xiandie/scene/little_game/弹珠游戏/弹珠游戏.gd

117 lines
2.6 KiB
GDScript3
Raw Normal View History

2025-04-21 13:31:12 +00:00
extends CanvasLayer
2025-05-14 20:43:55 +00:00
signal exit(success)
2025-04-21 13:31:12 +00:00
@onready var pivot = $Pivot as Node2D
@onready var hand_pivot = $Pivot/HandPivot as Node2D
2025-04-21 13:31:12 +00:00
2025-05-14 20:43:55 +00:00
var dialogue_c02 = preload("res://asset/dialogue/c02.dialogue")
2025-04-21 13:31:12 +00:00
func _ready() -> void:
layer = GlobalConfig.CANVAS_LAYER_LITTLE_GAME
wave_hand()
2025-05-14 20:43:55 +00:00
DialogueManager.show_dialogue_balloon(dialogue_c02, "c02_弹珠游戏1")
2025-04-21 13:31:12 +00:00
func reload() -> void:
# 重新加载
var ball_scene = preload("res://scene/little_game/弹珠游戏/ball.tscn")
var ball = ball_scene.instantiate()
ball.is_shooter = true
pivot.add_child(ball)
pivot.move_child(ball, 0)
ball.rand_id()
var wave_tween: Tween
var shooting = false
2025-05-14 20:43:55 +00:00
2025-04-21 13:31:12 +00:00
func wave_hand() -> void:
# 转动 pivot
if wave_tween:
if not wave_tween.is_running():
wave_tween.play()
2025-04-21 13:31:12 +00:00
return
wave_tween = create_tween()
var timeout = 2.0
2025-05-14 20:43:55 +00:00
(
wave_tween
. tween_property(hand_pivot, "rotation", 1.0, timeout)
. set_ease(Tween.EASE_OUT)
. set_trans(Tween.TRANS_SINE)
)
(
wave_tween
. tween_property(hand_pivot, "rotation", 0.0, timeout)
. set_ease(Tween.EASE_IN)
. set_trans(Tween.TRANS_SINE)
)
(
wave_tween
. tween_property(hand_pivot, "rotation", -1.0, timeout)
. set_ease(Tween.EASE_OUT)
. set_trans(Tween.TRANS_SINE)
)
(
wave_tween
. tween_property(hand_pivot, "rotation", 0.0, timeout)
. set_ease(Tween.EASE_IN)
. set_trans(Tween.TRANS_SINE)
)
2025-04-21 13:31:12 +00:00
wave_tween.set_loops(9999999)
func shoot() -> void:
if shooting:
return
shooting = true
# 触发射击事件
if wave_tween and wave_tween.is_running():
wave_tween.stop()
2025-04-21 13:31:12 +00:00
var ball = pivot.get_child(0) as RigidBody2D
var direction = Vector2(0, -1).rotated(hand_pivot.rotation)
2025-04-21 13:31:12 +00:00
ball.linear_velocity = direction * 200
ball.angular_velocity = 20.0
2025-04-21 13:31:12 +00:00
ball.hit_ball.connect(_on_hit_ball)
ball.hit_boundary.connect(_on_hit_boundary)
2025-05-14 20:43:55 +00:00
var hit_count = 0
2025-04-21 13:31:12 +00:00
func _on_hit_ball():
print("_on_hit_ball")
reload()
wave_hand()
shooting = false
2025-05-14 20:43:55 +00:00
hit_count += 1
if hit_count >= 2:
# 触发游戏结束
print("round 1 game over")
hit_count = 0
game_over()
2025-04-21 13:31:12 +00:00
func _on_hit_boundary():
# 重开游戏
print("hit boundary, restart")
reload()
wave_hand()
shooting = false
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("interact"):
shoot()
2025-05-14 20:43:55 +00:00
func game_over() -> void:
exit.emit(true)
# 0:默认; 1:寻找弹珠(老虎钳可以换弹珠) 2:游戏结束
2025-05-21 20:16:27 +00:00
ArchiveManager.set_global_entry("c02_ball_game_stage", 2)
2025-05-14 20:43:55 +00:00
SceneManager.disable_prop_item("prop_弹珠")
SceneManager.pop_debug_dialog_info("弹珠", "游戏关卡待完善")
DialogueManager.show_dialogue_balloon(dialogue_c02, "c02_弹珠游戏5")