86 lines
2.0 KiB
GDScript3
86 lines
2.0 KiB
GDScript3
extends Node
|
|
|
|
signal next_screen(screen_name)
|
|
|
|
var Ball = preload("res://ball/ball.tscn")
|
|
|
|
# Declare member variables here.
|
|
var ball = null # Ball on the field
|
|
var level = null # Current level
|
|
var total_score = 0 # Score
|
|
var paused = false
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
pass
|
|
|
|
func init(level_type):
|
|
total_score = 0
|
|
update_score()
|
|
new_level(level_type)
|
|
new_ball()
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(_delta):
|
|
if not paused:
|
|
if Input.is_action_just_pressed('ui_accept'):
|
|
ball.detach()
|
|
if Input.is_action_just_pressed("ui_menu"):
|
|
emit_signal("next_screen", "pause")
|
|
|
|
func pause():
|
|
paused = true
|
|
$Paddle.pause()
|
|
ball.pause()
|
|
$BGM.stop()
|
|
|
|
func unpause():
|
|
$Paddle.unpause()
|
|
ball.unpause()
|
|
$BGM.play()
|
|
paused = false
|
|
|
|
func new_level(level_type):
|
|
level = level_type.instance()
|
|
level.connect("level_ended", self, "_on_level_ended")
|
|
level.connect("score_increased", self, "_on_score_increased")
|
|
$LevelName.text = level.level_name
|
|
call_deferred("add_child", level)
|
|
|
|
func new_ball():
|
|
ball = Ball.instance()
|
|
ball.init($Paddle.position, true)
|
|
var _connection = $Paddle.connect("moved", ball, "update_paddle_position")
|
|
ball.connect("ball_lost", self, "_on_ball_lost")
|
|
call_deferred("add_child", ball)
|
|
|
|
func _on_ball_lost():
|
|
total_score -= 1000
|
|
update_score()
|
|
new_ball()
|
|
|
|
func update_score():
|
|
$Score.text = str(total_score)
|
|
|
|
func get_score():
|
|
return total_score
|
|
|
|
func _on_score_increased(score):
|
|
total_score += score
|
|
update_score()
|
|
|
|
func _on_level_ended():
|
|
pause()
|
|
$LevelCleared.show()
|
|
$EndLevelTimer.start()
|
|
$JingleEnd.play()
|
|
|
|
|
|
func _on_EndLevelTimer_timeout():
|
|
$LevelCleared.hide()
|
|
unpause()
|
|
ball.disconnect("ball_lost", self, "_on_ball_lost")
|
|
level.queue_free()
|
|
ball.queue_free()
|
|
emit_signal("next_screen", "game_won")
|