Init commit
This commit is contained in:
commit
a4452b69c4
59 changed files with 1259 additions and 0 deletions
88
Scripts/game.gd
Normal file
88
Scripts/game.gd
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
extends Node2D
|
||||
|
||||
var score: int = 0
|
||||
var lives: int = 0
|
||||
var added_lives: int = 0
|
||||
var score_label: Label
|
||||
var lives_box: HBoxContainer
|
||||
var spawner: Node2D
|
||||
var player: CharacterBody2D
|
||||
var ui: CanvasLayer
|
||||
var game_over_ui: Control
|
||||
var menu_ui: Control
|
||||
var life_icon = preload("res://Scenes/life.tscn")
|
||||
var menu_scn = preload("res://Scenes/main_menu.tscn")
|
||||
var game_over_scn = preload("res://Scenes/game_over.tscn")
|
||||
var ship = preload("res://Scenes/ship.tscn")
|
||||
var life = preload("res://Sounds/extra_life.wav")
|
||||
|
||||
func _ready() -> void:
|
||||
menu_ui = menu_scn.instantiate()
|
||||
menu_ui.new_game.connect(_new_game)
|
||||
menu_ui.quit_game.connect(_quit_game)
|
||||
|
||||
await get_tree().process_frame
|
||||
ui.add_child(menu_ui)
|
||||
|
||||
func set_score(amount: int):
|
||||
score = amount
|
||||
score_label.text = "Score: " + str(score)
|
||||
|
||||
if floor(score / 10000.0) > added_lives:
|
||||
added_lives += 1
|
||||
set_lives(lives + 1)
|
||||
|
||||
var snd = AudioStreamPlayer.new()
|
||||
add_child(snd)
|
||||
snd.stream = life
|
||||
snd.play()
|
||||
snd.finished.connect(func(): snd.queue_free())
|
||||
|
||||
func set_lives(amount: int):
|
||||
lives = amount
|
||||
|
||||
var icons = lives_box.get_children()
|
||||
|
||||
var diff = lives - len(icons);
|
||||
if diff > 0:
|
||||
for i in range(diff):
|
||||
lives_box.add_child(life_icon.instantiate())
|
||||
else:
|
||||
if lives >= 0:
|
||||
for i in range(abs(diff)):
|
||||
lives_box.remove_child(icons[len(icons) - 1])
|
||||
icons.remove_at(len(icons) - 1)
|
||||
|
||||
if lives == 0:
|
||||
spawner.stop()
|
||||
for node in get_tree().get_nodes_in_group("enemies"):
|
||||
node.queue_free()
|
||||
|
||||
player.queue_free()
|
||||
|
||||
score_label.visible = false
|
||||
game_over_ui = game_over_scn.instantiate()
|
||||
game_over_ui.new_game.connect(_new_game)
|
||||
game_over_ui.set_score(score)
|
||||
ui.add_child(game_over_ui)
|
||||
|
||||
func _new_game():
|
||||
if score_label:
|
||||
score_label.visible = true
|
||||
set_lives(3)
|
||||
spawner.start()
|
||||
set_score(0)
|
||||
|
||||
if game_over_ui:
|
||||
game_over_ui.queue_free()
|
||||
|
||||
if menu_ui:
|
||||
menu_ui.queue_free()
|
||||
|
||||
player = ship.instantiate()
|
||||
player.position = get_viewport_rect().size / 2
|
||||
|
||||
get_tree().current_scene.add_child(player)
|
||||
|
||||
func _quit_game():
|
||||
get_tree().quit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue