79 lines
1.9 KiB
GDScript
79 lines
1.9 KiB
GDScript
extends Node
|
|
|
|
@onready var crates: Node2D = $"../crates"
|
|
@onready var coins: Node2D = $"../coins"
|
|
@onready var enemies: Node2D = $"../enemies"
|
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
|
var bulletArray:Array = []
|
|
var bulletsMadeTotal :=0
|
|
|
|
|
|
var totalCrates := 0
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
#Give GC number of crates
|
|
Gamecontroller.destroyBox.connect(boxDestroy)
|
|
for obj in crates.get_children():
|
|
if obj.is_in_group("destructable"):
|
|
#increase the box counter
|
|
totalCrates +=1
|
|
var totalCoins = 0
|
|
for obj in coins.get_children():
|
|
if obj.is_in_group("collectibles"):
|
|
totalCoins +=1
|
|
obj.coinCollected.connect(Gamecontroller.coinCollected)
|
|
var totalEnemies = 0
|
|
for obj in enemies.get_children():
|
|
if obj.is_in_group("enemies"):
|
|
totalEnemies +=1
|
|
obj.playerDamage.connect(Gamecontroller.playerDamaged)
|
|
Gamecontroller.countBadGuys(totalEnemies)
|
|
Gamecontroller.countCoins(totalCoins)
|
|
Gamecontroller.countCrates(totalCrates)
|
|
|
|
#Subscribe to vital events from GC
|
|
Gamecontroller.levelComplete.connect(changeLevel)
|
|
|
|
Gamecontroller.reset()
|
|
|
|
|
|
func changeLevel(level):
|
|
get_tree().change_scene_to_file(level)
|
|
|
|
# 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 = bullet.instantiate()
|
|
myBullet.connect("hit", onBulletHit)
|
|
owner.add_child(myBullet)
|
|
else:
|
|
myBullet = bulletArray.pop_back()
|
|
|
|
bulletArray.push_front(myBullet)
|
|
|
|
return myBullet
|
|
|
|
func makeBullet(position, speed):
|
|
var myBullet = bulletFactory()
|
|
myBullet.transform = position
|
|
myBullet.setSpeed(speed)
|
|
return myBullet
|
|
|
|
func onBulletHit(bullet, body):
|
|
bullet.position = Vector2(-100,-100)
|
|
bullet.setSpeed(0)
|
|
|
|
print("Scene manager knows bullet hit")
|
|
Gamecontroller.bulletHit(body)
|
|
|
|
func boxDestroy(body):
|
|
body.queue_free()
|
|
|