class_name SceneManager extends Node2D var bullet = preload("res://scenes/bullet.tscn") var grenade = preload("res://scenes/grenade.tscn") var bullet_array = [] var total_allowed_bullets : int = 7 @onready var coins: Node = $"../Coins" @onready var enemies: Node = $"../Enemies" func _ready() -> void: GameController.reset() build_level() func build_level() -> void: if coins: update_coins() var _total_enemies : int = 0 if enemies: update_enemies() # Wire up signals from GameController GameController.level_changed.connect(change_scene) GameController.destroyed.connect(destroy) func update_enemies() -> void: var total_enemies : int = 0 for enemy in enemies.get_children(): if enemy is Slimer: total_enemies = total_enemies + 1 if not enemy.player_slimed.is_connected(GameController.on_player_slimed): enemy.player_slimed.connect(GameController.on_player_slimed) GameController.add_enemy_to_level(enemy) func update_coins() -> void: var total_coins : int = 0 for coin in coins.get_children(): if coin is Coin: print_debug("Adding %s to the total" % str(coin)) if not coin.coin_collected.is_connected(GameController.on_coin_collected): coin.coin_collected.connect(GameController.on_coin_collected) print_debug("There are %s coins in the total" % str(total_coins)) if not coin.tree_exited.is_connected(update_coins): coin.tree_exited.connect(update_coins) total_coins += 1 GameController.set_total_coins(total_coins) func make_bullet(_bullet_position, _bullet_speed) -> void: print("make a bullet; put it in the world") var my_bullet = bullet_factory() my_bullet.transform = _bullet_position my_bullet.set_speed(_bullet_speed) func make_grenade(_grenade_position, _grenade_direction) -> void: print("SM make a grenade") var my_grenade : Grenade = grenade.instantiate() GameController.add_child(my_grenade) my_grenade.transform = _grenade_position my_grenade.apply_central_impulse(Vector2(_grenade_direction, -1) * 200) func bullet_factory() -> Bullet: var my_bullet : Bullet if bullet_array.size() < total_allowed_bullets: # make a new bullet my_bullet = bullet.instantiate() my_bullet.bullet_damaged.connect(GameController.bullet_damage) owner.add_child(my_bullet) else: my_bullet = bullet_array.pop_back() bullet_array.push_front(my_bullet) return my_bullet func _on_world_boundary_body_entered(body: Node2D) -> void: if body is Player: var current_scene = get_tree() current_scene.call_deferred("reload_current_scene") func change_scene(level) -> void: print_debug("Changing scene to level %s" % level) get_tree().call_deferred("change_scene_to_file", level) func destroy(body) -> void: body.queue_free()