bricasse/screens/game_screen.gd

103 lines
2.5 KiB
GDScript3

extends Node
signal next_screen(screen_name)
var Ball = preload("res://ball/ball.tscn")
# List of levels
var Levels = [
preload("res://levels/level_01_01.tscn"),
preload("res://levels/level_01_02.tscn"),
preload("res://levels/level_01_03.tscn"),
preload("res://levels/level_01_04.tscn"),
preload("res://levels/level_01_05.tscn"),
preload("res://levels/level_02_01.tscn"),
preload("res://levels/level_02_02.tscn"),
preload("res://levels/level_02_03.tscn"),
]
# Declare member variables here.
var ball = null # Ball on the field
var current_level = 0 # Index of the current levels
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_index):
current_level = level_index
total_score = 0
update_score()
new_level()
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_select'):
ball.detach()
if Input.is_action_just_pressed("ui_cancel"):
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 = Levels[current_level].instance()
level.connect("level_ended", self, "_on_level_ended")
level.connect("score_increased", self, "_on_score_increased")
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()
current_level = current_level + 1
if current_level >= Levels.size():
emit_signal("next_screen", "game_won")
else:
new_level()
new_ball()