41 lines
1020 B
GDScript3
41 lines
1020 B
GDScript3
|
extends Node
|
||
|
|
||
|
@onready var game: GameController = $".."
|
||
|
@onready var crates: Node2D = $"../Crates"
|
||
|
var bullet = preload("res://scenes/bullet.tscn")
|
||
|
var bullet_array = []
|
||
|
var total_allowed_bullets = 7
|
||
|
|
||
|
# Called when the node enters the scene tree for the first time.
|
||
|
func _ready() -> void:
|
||
|
build_level()
|
||
|
|
||
|
func build_level() -> void:
|
||
|
# count crates
|
||
|
var total_crates = 0
|
||
|
for obj in crates.get_children():
|
||
|
if obj is Crate:
|
||
|
total_crates += 1
|
||
|
# tell game controller
|
||
|
game.number_of_crates(total_crates)
|
||
|
|
||
|
func bullet_factory():
|
||
|
var my_bullet
|
||
|
if bullet_array.size() < total_allowed_bullets:
|
||
|
# new bullet
|
||
|
my_bullet = bullet.instantiate()
|
||
|
my_bullet.connect('bullet_hit', game.bullet_damage)
|
||
|
owner.add_child(my_bullet)
|
||
|
else:
|
||
|
# recycle bullet
|
||
|
my_bullet = bullet_array.pop_back()
|
||
|
|
||
|
bullet_array.push_front(my_bullet)
|
||
|
return my_bullet
|
||
|
|
||
|
func make_bullet(position, speed):
|
||
|
print('Scene manager makes a bullet')
|
||
|
var my_bullet = bullet_factory()
|
||
|
my_bullet.set_speed(speed)
|
||
|
my_bullet.transform = position
|