71 lines
1.8 KiB
GDScript
71 lines
1.8 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var pivot = $Pivot as Node2D
|
|
@onready var hand_pivot = $Pivot/HandPivot as Node2D
|
|
|
|
|
|
func _ready() -> void:
|
|
layer = GlobalConfig.CANVAS_LAYER_LITTLE_GAME
|
|
wave_hand()
|
|
|
|
|
|
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
|
|
|
|
func wave_hand() -> void:
|
|
# 转动 pivot
|
|
if wave_tween:
|
|
if not wave_tween.is_running():
|
|
wave_tween.play()
|
|
return
|
|
wave_tween = create_tween()
|
|
var timeout = 2.0
|
|
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)
|
|
wave_tween.set_loops(9999999)
|
|
|
|
|
|
func shoot() -> void:
|
|
if shooting:
|
|
return
|
|
shooting = true
|
|
# 触发射击事件
|
|
if wave_tween and wave_tween.is_running():
|
|
wave_tween.stop()
|
|
var ball = pivot.get_child(0) as RigidBody2D
|
|
var direction = Vector2(0, -1).rotated(hand_pivot.rotation)
|
|
ball.linear_velocity = direction * 200
|
|
ball.angular_velocity = 20.0
|
|
ball.hit_ball.connect(_on_hit_ball)
|
|
ball.hit_boundary.connect(_on_hit_boundary)
|
|
|
|
func _on_hit_ball():
|
|
print("_on_hit_ball")
|
|
reload()
|
|
wave_hand()
|
|
shooting = false
|
|
|
|
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()
|