56 lines
1.3 KiB
GDScript3
56 lines
1.3 KiB
GDScript3
extends KinematicBody2D
|
|
|
|
# signale
|
|
signal ball_lost
|
|
|
|
# Declare member variables here.
|
|
# Velocity
|
|
export var speed = 360
|
|
export var initial_angle = -PI / 4
|
|
var velocity = Vector2(speed, 0).rotated(initial_angle)
|
|
var paddle_offset = Vector2(0, 0)
|
|
# Attached to paddle ?
|
|
# When attached to paddle, the ball follow the movement of the paddle
|
|
var attached = true
|
|
var launched_from_paddle = true
|
|
var paused = false
|
|
var sound
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
pass
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
#func _process(delta):
|
|
# pass
|
|
|
|
func init(paddle_pos, att):
|
|
position = paddle_pos + paddle_offset
|
|
attached = att
|
|
|
|
func update_paddle_position(pos):
|
|
if attached:
|
|
position = pos + paddle_offset
|
|
|
|
func detach():
|
|
attached = false
|
|
|
|
func _physics_process(delta):
|
|
if not paused and not attached:
|
|
var collision = move_and_collide(velocity * delta)
|
|
if collision:
|
|
velocity = velocity.bounce(collision.normal)
|
|
if collision.collider.has_method("hit"):
|
|
collision.collider.hit()
|
|
$Plop.play()
|
|
|
|
func _on_VisibilityNotifier2D_screen_exited():
|
|
emit_signal("ball_lost")
|
|
queue_free()
|
|
|
|
func pause():
|
|
paused = true
|
|
|
|
func unpause():
|
|
paused = false
|