JanGodotGame/scripts/sceneManager.gd

93 lines
2.5 KiB
GDScript3
Raw Normal View History

2025-01-21 01:44:35 +00:00
extends Node
2025-02-04 02:03:41 +00:00
# @onready var game: Node2D = $".."
2025-01-21 01:44:35 +00:00
@onready var crates: Node2D = $"../crates"
2025-02-04 02:03:41 +00:00
@onready var coins: Node2D = $"../coins"
@onready var enemies: Node2D = $"../enemies"
2025-01-21 01:44:35 +00:00
2025-02-11 00:19:43 +00:00
@onready var player: CharacterBody2D = $"../CharacterBody2D"
2025-02-11 02:02:09 +00:00
@onready var ui: Control = $"../CanvasLayer/ui"
2025-02-11 00:19:43 +00:00
2025-01-21 01:44:35 +00:00
var theBullet = preload("res://scenes/bullet.tscn")
var bulletArray:Array = []
2025-02-04 02:03:41 +00:00
2025-01-21 01:44:35 +00:00
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
2025-02-04 02:03:41 +00:00
var totalCrates = 0
2025-02-04 02:03:41 +00:00
GameController.reset()
GameController.destroyBox.connect(boxDestroy)
2025-01-21 01:44:35 +00:00
for obj in crates.get_children():
if obj.is_in_group("destructables"):
totalCrates += 1
2025-02-04 02:03:41 +00:00
GameController.countCrates(totalCrates)
2025-02-04 02:03:41 +00:00
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)
2025-02-04 02:03:41 +00:00
2025-02-04 02:03:41 +00:00
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)
2025-02-11 00:19:43 +00:00
# Player interactions
GameController.playerHurt.connect(player.hurtPlayer)
GameController.playerDeath.connect(player.killPlayer)
2025-02-04 02:03:41 +00:00
# Subscribe to signals
GameController.levelComplete.connect(changeLevel)
2025-02-11 00:19:43 +00:00
player.deathAnimDone.connect(GameController.playerDead)
2025-02-11 02:02:09 +00:00
GameController.tick.connect(ui.timerUpdate)
GameController.playerHurt.connect(ui.healthUpdate, ConnectFlags.CONNECT_DEFERRED)
GameController.coinsUpdate.connect(ui.coinsUpdate)
2025-02-11 00:19:43 +00:00
GameController.reset()
2025-02-04 02:03:41 +00:00
func changeLevel(level):
print("New level")
get_tree().change_scene_to_file(level)
2025-01-21 01:44:35 +00:00
# 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
2025-01-28 01:51:04 +00:00
2025-01-21 01:44:35 +00:00
func onBulletHit(bullet, body):
bullet.position = Vector2(-100, -100)
bullet.setSpeed(0)
print("Scene manager knows bullet hit")
2025-02-04 02:03:41 +00:00
GameController.bulletHit(body)
2025-01-21 01:44:35 +00:00
func boxDestroy(body):
print("sceneMgr knows body destroyed")
body.queue_free()