June2026Game/Scripts/scene_manager.gd

79 lines
2.1 KiB
GDScript

extends Node2D
# hold control and drag + drop
@onready var game:GameController = $".."
@onready var crates: Node2D = $"../Crates"
@onready var player: Player = $"../Player"
var bullet = preload("res://Scenes/bullet.tscn")
var bulletArray:Array[Bullet] = []
var totalAllowedBullets:int = 7
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player.pushSignal.connect(pushTarget)
player.shootSignal.connect(makeBullet)
#signals from the GameController
game.destroySignal.connect(destroy)
buildLevel()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func buildLevel() -> void:
#tell GC how many crates...
updateCrates()
func updateCrates()->void:
var totalCrates = 0
if crates:
for obj in crates.get_children():
if obj is Crate:
if not obj.tree_exited.is_connected(updateCrates):
obj.tree_exited.connect(updateCrates)
totalCrates +=1
game.crateTotal(totalCrates)
func pushTarget(body,pushDirection)->void:
if body is RigidBody2D:
body.apply_central_impulse(pushDirection)
func stashBullet(bullet:Bullet)->void:
# hide bullet
var stashPosition:Vector2 = Vector2(-100,-100)
bullet.position = stashPosition
bullet.setSpeed(0)
bullet.set_process(false)
func destroy(body)->void:
match body:
var bod when bod is Bullet:
stashBullet(body)
var bod when bod is Crate:
body.queue_free()
_:
body.queue_free()
func bulletFactory()->Bullet:
var myBullet:Bullet
#how many bullets have been produced?
if bulletArray.size() <= totalAllowedBullets:
#make a new bullet
myBullet = bullet.instantiate()
add_sibling(myBullet)
if not myBullet.bulletHitSignal.is_connected(game.bulletDamage):
myBullet.bulletHitSignal.connect(game.bulletDamage)
else:
# gets oldest one - pop_back
myBullet = bulletArray.pop_back()
bulletArray.push_front(myBullet)
return myBullet
func makeBullet(spawnPosition, speed)->void:
print("make bullet")
var myBullet:Bullet = bulletFactory()
myBullet.transform = spawnPosition
myBullet.setSpeed(speed)
myBullet.set_process(true)