2025-03-04 01:40:21 +00:00
|
|
|
extends Node
|
2025-03-25 01:02:52 +00:00
|
|
|
|
2025-03-11 01:05:23 +00:00
|
|
|
@onready var crates: Node2D = $"../crates"
|
|
|
|
@onready var triggers: Node2D = $"../triggers"
|
2025-03-25 01:02:52 +00:00
|
|
|
@onready var coins: Node2D = $"../coins"
|
|
|
|
@onready var badguys: Node2D = $"../badguys"
|
|
|
|
@onready var player: Player = $"../CharacterBody2D"
|
2025-04-01 01:07:37 +00:00
|
|
|
@onready var ui: Control = $"../CanvasLayer/ui"
|
2025-03-04 01:40:21 +00:00
|
|
|
|
|
|
|
var bulletsFiredTotal = 0
|
|
|
|
var bulletsMadeTotal = 0
|
|
|
|
var bulletArray= []
|
|
|
|
|
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
|
|
|
|
|
|
|
# bullet factory - makes bullets
|
|
|
|
func bulletFactory():
|
|
|
|
var mybullet
|
|
|
|
if bulletArray.size() < 7:
|
|
|
|
print("new bullet at factory")
|
|
|
|
mybullet = bullet.instantiate()
|
|
|
|
mybullet.connect("hit", bulletHit)
|
|
|
|
else:
|
|
|
|
print("recycled bullet at factory")
|
|
|
|
mybullet = bulletArray.pop_back()
|
|
|
|
|
|
|
|
bulletArray.push_front(mybullet)
|
|
|
|
return mybullet
|
|
|
|
|
|
|
|
# order desk for bullets
|
|
|
|
func makeBullet(position, speed):
|
|
|
|
print("make a bullet")
|
|
|
|
var newBullet = bulletFactory()
|
|
|
|
owner.add_child(newBullet)
|
|
|
|
newBullet.setSpeed(speed)
|
|
|
|
newBullet.transform = position
|
|
|
|
|
|
|
|
func bulletHit(bullet, body):
|
|
|
|
print("Tell the game controller a bullet hit something")
|
2025-03-25 01:02:52 +00:00
|
|
|
Gamecontroller.bulletHit(body)
|
2025-03-11 01:05:23 +00:00
|
|
|
bullet.setSpeed(0)
|
|
|
|
bullet.position = Vector2(-100,-100)
|
|
|
|
|
|
|
|
func destroy(body):
|
|
|
|
body.queue_free()
|
|
|
|
|
|
|
|
func buildLevel():
|
|
|
|
#how many crates?
|
|
|
|
var totalCrates = 0
|
|
|
|
for obj in crates.get_children():
|
|
|
|
if obj is Crate:
|
|
|
|
totalCrates +=1
|
2025-03-25 01:02:52 +00:00
|
|
|
Gamecontroller.numberOfCrates(totalCrates)
|
2025-03-11 01:05:23 +00:00
|
|
|
|
2025-03-25 01:02:52 +00:00
|
|
|
#how many coins?
|
|
|
|
var totalCoins = 0
|
|
|
|
if coins:
|
|
|
|
for obj in coins.get_children():
|
|
|
|
if obj is Coin:
|
|
|
|
totalCoins +=1
|
|
|
|
obj.coinCollectedSignal.connect(Gamecontroller.coinCollected)
|
|
|
|
Gamecontroller.numberOfCoins(totalCoins)
|
|
|
|
#how many badguys?
|
|
|
|
var totalBadguys = 0
|
|
|
|
if badguys:
|
|
|
|
for obj in badguys.get_children():
|
|
|
|
if obj is Slime:
|
|
|
|
totalBadguys +=1
|
|
|
|
obj.playerDamageSignal.connect(Gamecontroller.playerDamage)
|
2025-04-01 01:07:37 +00:00
|
|
|
Gamecontroller.addBadGuy(obj)
|
2025-03-25 01:02:52 +00:00
|
|
|
Gamecontroller.numberOfBadguys(totalBadguys)
|
2025-03-11 01:05:23 +00:00
|
|
|
#wire up the triggers
|
|
|
|
for obj in triggers.get_children():
|
|
|
|
if obj is Trigger:
|
|
|
|
# wire up to GC
|
2025-03-25 01:02:52 +00:00
|
|
|
obj.areaTrigger.connect(Gamecontroller._on_trigger_area_trigger)
|
|
|
|
|
|
|
|
#listen to gamnecontroller
|
|
|
|
Gamecontroller.destroySignal.connect(destroy)
|
|
|
|
Gamecontroller.levelCompleteSignal.connect(loadLevel)
|
|
|
|
Gamecontroller.playerHurtSignal.connect(playerHurt)
|
2025-04-01 01:07:37 +00:00
|
|
|
Gamecontroller.playerHurtSignal.connect(ui.healthUpdate)
|
|
|
|
Gamecontroller.playerKillSignal.connect(playerKill)
|
|
|
|
Gamecontroller.timerUpdateSignal.connect(ui.timerUpdate)
|
|
|
|
Gamecontroller.coinUpdateSignal.connect(ui.coinsUpdate)
|
|
|
|
#listen to player
|
|
|
|
player.deathCompleteSignal.connect(Gamecontroller.deathComplete)
|
2025-03-25 01:02:52 +00:00
|
|
|
|
|
|
|
func loadLevel(level):
|
|
|
|
get_tree().change_scene_to_file(level)
|
2025-03-11 01:05:23 +00:00
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
buildLevel()
|
2025-03-25 01:02:52 +00:00
|
|
|
Gamecontroller.reset()
|
2025-04-01 01:07:37 +00:00
|
|
|
func playerHurt(health, _maxHealth):
|
2025-03-25 01:02:52 +00:00
|
|
|
player.hurtPlayer(health)
|
2025-04-01 01:07:37 +00:00
|
|
|
func playerKill():
|
|
|
|
player.killPlayer()
|