GodotCourse/week2/updated_files/scripts/SceneManager.gd

38 lines
859 B
GDScript

extends Node
var bulletsFiredTotal := 0
var bulletsMadeTotal := 0
var bulletArray:Array = []
var bullet = preload("res://scenes/bullet.tscn")
#factory decides to issue new bullet, or
#recycle an old one
func bulletFactory():
var mybullet
if bulletArray.size() < 4:
mybullet = bullet.instantiate()
mybullet.connect("hit", onBulletHit)
owner.add_child(mybullet)
else:
mybullet = bulletArray.pop_back()
mybullet.setSpeed(700)
bulletArray.push_front(mybullet)
bulletsMadeTotal +=1
return mybullet
#order desk for new bullets
func makeBullet(position, speed):
var myBullet = bulletFactory()
myBullet.transform = position
myBullet.setSpeed(speed)
return myBullet
func onBulletHit(bullet, body):
#deactivate bullet
#put back in queue
bullet.setSpeed(1)
#tell the GameController the bullet hit something
#GameController.bulletHit(body)