You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
80 lines
2.2 KiB
extends CharacterBody2D
|
|
## 挡板:跟随键盘或鼠标左右移动,支持加宽道具。
|
|
## 速度/边界在编辑器里可调。
|
|
|
|
@export var speed: float = 600.0
|
|
@export var use_mouse: bool = true
|
|
@export var left_bound: float = 32.0
|
|
@export var right_bound: float = 1248.0
|
|
|
|
@export var default_size: Vector2 = Vector2(96, 16) ## 初始宽高
|
|
@export var wide_size: Vector2 = Vector2(160, 16) ## 加宽后尺寸
|
|
@export var wide_duration: float = 10.0 ## 加宽持续时间(秒)
|
|
|
|
@onready var _collision_shape: CollisionShape2D = $CollisionShape2D
|
|
@onready var _visual: ColorRect = $ColorRect
|
|
@onready var _shape: RectangleShape2D = _collision_shape.shape
|
|
|
|
var _wide_timer: float = 0.0
|
|
var _is_wide_active: bool = false ## 边界是否已被缩过(防止多次加宽累积)
|
|
|
|
func _ready() -> void:
|
|
add_to_group("paddle")
|
|
_resize(default_size)
|
|
|
|
func _physics_process(_delta: float) -> void:
|
|
# 加宽倒计时
|
|
if _wide_timer > 0.0:
|
|
_wide_timer -= _delta
|
|
if _wide_timer <= 0.0:
|
|
_resize(default_size)
|
|
if _is_wide_active:
|
|
left_bound -= 32.0
|
|
right_bound += 32.0
|
|
_is_wide_active = false
|
|
|
|
var target_x: float = position.x
|
|
|
|
if use_mouse:
|
|
target_x = clamp(get_global_mouse_position().x, left_bound, right_bound)
|
|
else:
|
|
var dir: float = 0.0
|
|
if Input.is_action_pressed("ui_left"):
|
|
dir -= 1.0
|
|
if Input.is_action_pressed("ui_right"):
|
|
dir += 1.0
|
|
target_x = position.x + speed * dir * _delta
|
|
|
|
position.x = clamp(target_x, left_bound, right_bound)
|
|
velocity = Vector2.ZERO
|
|
|
|
func widen() -> void:
|
|
if not _is_wide_active:
|
|
_resize(wide_size)
|
|
left_bound += 32.0
|
|
right_bound -= 32.0
|
|
_is_wide_active = true
|
|
_wide_timer = wide_duration # 续期
|
|
|
|
func is_wide() -> bool:
|
|
return _wide_timer > 0.0
|
|
|
|
func reset_state() -> void:
|
|
# 关卡切换时调用:撤销加宽
|
|
if _is_wide_active:
|
|
_resize(default_size)
|
|
left_bound -= 32.0
|
|
right_bound += 32.0
|
|
_is_wide_active = false
|
|
_wide_timer = 0.0
|
|
|
|
func get_half_width() -> float:
|
|
return _shape.size.x / 2.0
|
|
|
|
func get_wide_timer() -> float:
|
|
return _wide_timer
|
|
|
|
func _resize(size: Vector2) -> void:
|
|
_shape.size = size
|
|
_visual.size = size
|
|
_visual.position = -size / 2.0 # 中心对齐
|
|
|