49 lines
1.1 KiB
GDScript
49 lines
1.1 KiB
GDScript
extends Node
|
|
|
|
@onready var game: GameController = $".."
|
|
@onready var crates: Node2D = $"../Crates"
|
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
|
var bulletArray = []
|
|
var totalAllowedBullets = 7
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
buildLevel()
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|
|
|
|
|
|
func buildLevel() -> void:
|
|
# count crates
|
|
var totalCrates = 0
|
|
for obj in crates.get_children():
|
|
if obj is Crate:
|
|
totalCrates += 1
|
|
# tell the game controller
|
|
game.numberOfCrates(totalCrates)
|
|
|
|
|
|
func bulletFactory():
|
|
var mybullet
|
|
if bulletArray.size() < totalAllowedBullets:
|
|
#new bullet
|
|
mybullet = bullet.instantiate()
|
|
mybullet.connect("bulletHit", game.bulletDamage)
|
|
owner.add_child(mybullet)
|
|
else:
|
|
#recycled bullet
|
|
mybullet = bulletArray.pop_back()
|
|
|
|
bulletArray.push_front(mybullet)
|
|
return mybullet
|
|
|
|
func makeBullet(position, speed):
|
|
print("Scene manager make bullet")
|
|
var mybullet = bulletFactory()
|
|
mybullet.setSpeed(speed)
|
|
mybullet.transform = position
|