xiandie/scene/little_game/弹珠游戏/ball.gd
2025-04-21 21:31:12 +08:00

69 lines
1.7 KiB
GDScript

@tool
extends RigidBody2D
signal hit_ball
signal hit_boundary
@export_enum("1", "2", "3") var id := "1":
set(value):
id = value
if Engine.is_editor_hint() and is_node_ready():
_set_up_texture()
@export var is_shooter := false:
set(value):
is_shooter = value
@export var enable := true:
set(value):
enable = value
if is_node_ready():
$CollisionShape2D.set_deferred("disabled", !value)
@onready var sprite2d = $Sprite2D as Sprite2D
const texture_dict = {
"1": preload("res://scene/little_game/弹珠游戏/ball_texture1.tres"),
"2": preload("res://scene/little_game/弹珠游戏/ball_texture2.tres"),
"3": preload("res://scene/little_game/弹珠游戏/ball_texture3.tres"),
}
func _ready() -> void:
contact_monitor = true
max_contacts_reported = 1
_set_up_texture()
body_entered.connect(_on_body_entered)
func rand_id():
# 随机 id
var rand = randi() % 3 + 1
id = str(rand)
_set_up_texture()
func _set_up_texture():
sprite2d.texture = texture_dict[id]
var recycling = false
func _on_body_entered(node: Node):
print("hit node", node.get_path())
if recycling or not is_shooter:
return
recycling = true
var tween = create_tween()
if node is StaticBody2D:
tween.tween_property(sprite2d, "modulate:a", 0.0, 1.0)
tween.tween_callback(hit_boundary.emit)
tween.tween_callback(queue_free)
return
tween.tween_property(sprite2d, "modulate:a", 0.0, 1.0)
tween.tween_property(node.sprite2d, "modulate:a", 0.0, 1.0)
tween.tween_callback(hit_ball.emit)
tween.tween_callback(node.queue_free)
tween.tween_callback(queue_free)
func _process(_delta: float) -> void:
# 根据 y 坐标缩放
var ball_scale = 0.6 + (global_position.y / 600.0)
sprite2d.scale = Vector2(ball_scale, ball_scale)