JanGodotGame/scripts/sceneManager.gd

87 lines
2.3 KiB
GDScript

extends Node
# @onready var game: Node2D = $".."
@onready var crates: Node2D = $"../crates"
@onready var coins: Node2D = $"../coins"
@onready var enemies: Node2D = $"../enemies"
@onready var player: CharacterBody2D = $"../CharacterBody2D"
var theBullet = preload("res://scenes/bullet.tscn")
var bulletArray:Array = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var totalCrates = 0
GameController.reset()
GameController.destroyBox.connect(boxDestroy)
for obj in crates.get_children():
if obj.is_in_group("destructables"):
totalCrates += 1
GameController.countCrates(totalCrates)
var totalCoins = 0
for obj in coins.get_children():
if obj.is_in_group("collectibles"):
totalCoins += 1
obj.coinCollected.connect(GameController.coinCollected)
GameController.countCoins(totalCoins)
var totalEnemies = 0
for obj in enemies.get_children():
if obj.is_in_group("enemies"):
totalEnemies += 1
obj.playerHurt.connect(GameController.playerDamaged)
GameController.countBadGuys(totalEnemies)
GameController.countCrates(totalCrates)
# Player interactions
GameController.playerHurt.connect(player.hurtPlayer)
GameController.playerDeath.connect(player.killPlayer)
# Subscribe to signals
GameController.levelComplete.connect(changeLevel)
player.deathAnimDone.connect(GameController.playerDead)
GameController.reset()
func changeLevel(level):
print("New level")
get_tree().change_scene_to_file(level)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
pass
func bulletFactory():
print("Factory will make a bullet")
var myBullet
if bulletArray.size() < 4:
myBullet = theBullet.instantiate()
myBullet.connect("hit", onBulletHit)
owner.add_child(myBullet)
else:
myBullet = bulletArray.pop_back()
bulletArray.push_front(myBullet)
print("there are ", bulletArray.size(), " bullets")
return myBullet
func makeBullet(position, speed):
var myBullet = bulletFactory()
myBullet.transform = position
myBullet.setSpeed(speed)
return
func onBulletHit(bullet, body):
bullet.position = Vector2(-100, -100)
bullet.setSpeed(0)
print("Scene manager knows bullet hit")
GameController.bulletHit(body)
func boxDestroy(body):
print("sceneMgr knows body destroyed")
body.queue_free()