65 lines
1.4 KiB
GDScript
65 lines
1.4 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var pivot = $Pivot 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 and wave_tween.is_running():
|
|
return
|
|
wave_tween = create_tween()
|
|
wave_tween.tween_property(pivot, "rotation", 0.5, 2.0).set_ease(Tween.EASE_IN_OUT)
|
|
wave_tween.tween_property(pivot, "rotation", -0.5, 2.0).set_ease(Tween.EASE_IN_OUT)
|
|
wave_tween.set_loops(9999999)
|
|
|
|
|
|
func shoot() -> void:
|
|
if shooting:
|
|
return
|
|
shooting = true
|
|
# 触发射击事件
|
|
if wave_tween and wave_tween.is_running():
|
|
wave_tween.kill()
|
|
var ball = pivot.get_child(0) as RigidBody2D
|
|
var direction = Vector2(0, -1).rotated(pivot.rotation)
|
|
ball.linear_velocity = direction * 200
|
|
ball.angular_velocity = 10.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()
|