Shmup-01b/main.gd
Henry f1eec30167 Decreased movement and shot delay of enemies based on number of enemies
remaining; increased tween speed of enemy bounce at 25% remaining.
2025-12-31 13:34:29 +00:00

135 lines
3.8 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:
set(value):
if value < 0:
print_debug("Enemy value set to below zero: ", get_stack())
if value > 27:
print_debug("Enemy value set to above 27!: ", get_stack())
enemy_count = value
const COLS: int = 9
const ROWS: int = 3
@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)
EventBus.enemy_hit.connect(_on_enemy_hit)
func spawn_enemies():
# print("Remaining enemies: ", enemy_count)
# await get_tree().process_frame
for x in range(COLS):
for y in range(ROWS):
var e = enemy.instantiate()
var pos = Vector2(x * (16 + 8) + 24, 16 * 4 + y * 16)
add_child(e)
e.start(pos)
enemy_count = ROWS * COLS
await get_tree().process_frame
print_debug("Remaining enemies: ", enemy_count, ", ", get_tree().get_nodes_in_group("enemies").size())
func _on_enemy_died():
enemy_count -= 1
print_debug(enemy_count)
if enemy_count == 0:
print_debug("Remaining enemies: ", enemy_count, ", ", get_tree().get_nodes_in_group("enemies").size())
win_game()
if enemy_count == ROWS * COLS / 2:
print_debug("Enemy count is 50%")
update_enemy_aggression(2,10)
if enemy_count == ROWS * COLS / 4:
print_debug("Enemy count is 25%")
update_enemy_aggression(1,1)
func update_enemy_aggression(low,high):
var nodes = get_tree().get_nodes_in_group("enemies")
for node in nodes:
if node is Area2D:
node.enemy_move_aggression = randf_range(low,high)
node.enemy_shoot_aggression = randf_range(low,high)
node.tween_speed = min(low,high) - .75
func _on_enemy_hit(value: int):
Global.score += value
$CanvasLayer/UI.update_score(Global.score)
func _on_player_died():
# get_tree().call_group("enemies", "queue_free")
# $Player.set_process(false)
# get_tree().call_group("enemies", "set_process", false)
enemy_win()
instance.queue_free()
game_over.show()
await get_tree().create_timer(2).timeout
game_over.hide()
$CanvasLayer/Title.show()
start_button.show()
playing = false
func new_game():
$CanvasLayer/Title.hide()
# Ensure enemies are cleared.
get_tree().call_group("enemies", "queue_free")
get_tree().call_group("enemy_bullets", "queue_free")
# print("Number of enemies at new_game: ",enemy_count)
enemy_count = 0
await get_tree().process_frame
# Reset score.
Global.score = 0
$CanvasLayer/UI.update_score(Global.score)
# Tell the shield to recharge.
# EventBus.initialize_shieldbar.emit()
# await EventBus.initialize_shieldbar
# instantiate the Player
# await get_tree().create_timer(1).timeout
instance = player.instantiate()
add_child(instance)
$Player.start()
# Tell the enemies to spawn!
#await get_tree().create_timer(.5).timeout
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()
$CanvasLayer/Title.show()
start_button.show()
func enemy_win() -> void:
print("Enemy win!")