class_name Paddle extends AnimatableBody2D ## Follows the player's finger (or mouse) along the bottom of the screen. enum Width { NORMAL, HALF, DOUBLE } const SCALE := { Width.NORMAL: 1.0, Width.HALF: 0.5, Width.DOUBLE: 2.0 } var width := Width.NORMAL var _screen_width := 0.0 var _shape := RectangleShape2D.new() @onready var _timer: Timer = $NormalTimer func _ready() -> void: _screen_width = get_viewport_rect().size.x $CollisionShape2D.shape = _shape _timer.timeout.connect(normal_size) normal_size() func _unhandled_input(event: InputEvent) -> void: if event is InputEventScreenTouch and event.is_pressed(): _move_to((event as InputEventScreenTouch).position.x) elif event is InputEventScreenDrag: _move_to((event as InputEventScreenDrag).position.x) ## Doubles the paddle for a while, or cancels a halving. func double_size() -> void: if width == Width.HALF: normal_size() return _set_width(Width.DOUBLE) _timer.start(Params.POWER_UP_PADDLE_SECONDS) ## Halves the paddle for a while, or cancels a doubling. func half_size() -> void: if width == Width.DOUBLE: normal_size() return _set_width(Width.HALF) _timer.start(Params.POWER_UP_PADDLE_SECONDS) func normal_size() -> void: _timer.stop() _set_width(Width.NORMAL) func _set_width(value: Width) -> void: width = value _shape.size = Vector2((Params.PADDLE_WIDTH + Params.PADDLE_HEIGHT) * SCALE[width], Params.PADDLE_HEIGHT) _move_to(position.x) queue_redraw() func _move_to(x: float) -> void: var half: float = Params.PADDLE_WIDTH / 2.0 * SCALE[width] position.x = clampf(x, half, _screen_width - half) func _draw() -> void: var half: float = Params.PADDLE_WIDTH / 2.0 * SCALE[width] var cap := Params.PADDLE_HEIGHT / 2.0 draw_line(Vector2(-half, 0), Vector2(half, 0), Params.COLOR_GREY, Params.PADDLE_HEIGHT) draw_circle(Vector2(-half, 0), cap, Params.COLOR_GREY) draw_circle(Vector2(half, 0), cap, Params.COLOR_GREY)