Shmup-01b/main.gd

95 lines
2.4 KiB
GDScript

extends Node2D
var enemy = preload("res://enemy.tscn")
var player = preload("res://player.tscn")
var instance = null
var playing = false
var enemy_count: int = 0
@onready var start_button = $CanvasLayer/CenterContainer/Start
@onready var game_over = $CanvasLayer/CenterContainer/GameOver
func _ready():
game_over.hide()
start_button.show()
EventBus.player_died.connect(_on_player_died)
EventBus.enemy_died.connect(_on_enemy_died)
func spawn_enemies():
get_tree().call_group("enemies", "queue_free")
for x in range(9):
for y in range(3):
var e = enemy.instantiate()
var pos = Vector2(x * (16 + 8) + 24, 16 * 4 + y * 16)
add_child(e)
e.start(pos)
enemy_count = get_tree().get_nodes_in_group("enemies").size()
print("Remaining enemies: ", enemy_count)
func _on_enemy_died(value: int):
Global.score += value
enemy_count -= 1
$CanvasLayer/UI.update_score(Global.score)
print_debug(enemy_count)
if enemy_count == 0:
print("All enemies defeated!")
win_game()
func _on_player_died():
# get_tree().call_group("enemies", "queue_free")
EventBus.enemy_win.emit()
# $Player.set_process(false)
# get_tree().call_group("enemies", "set_process", false)
instance.queue_free()
game_over.show()
await get_tree().create_timer(2).timeout
game_over.hide()
start_button.show()
playing = false
func new_game():
# Ensure enemies are cleared.
enemy_count = 0
get_tree().call_group("enemies", "queue_free")
get_tree().call_group("enemy_bullets", "queue_free")
print("Number of enemies at new_game: ",enemy_count)
# Reset score.
Global.score = 0
$CanvasLayer/UI.update_score(Global.score)
# instantiate the Player
instance = player.instantiate()
add_child(instance)
$Player.start()
# Tell the shield to recharge.
EventBus.initialize_shieldbar.emit()
# Tell the enemies to spawn!
spawn_enemies()
# Tell the game we're playing.
playing = true
print("New game started!")
func _input(EventInput):
if EventInput.is_action_pressed("shoot") and playing == false:
print("Input detected!")
start_button.hide()
new_game()
func _on_start_pressed():
start_button.hide()
new_game()
func win_game():
playing = false
game_over.show()
$Player._on_player_victory()
await get_tree().create_timer(2).timeout
game_over.hide()
start_button.show()