DariusGodotGame/dariusgodotproject/scripts/gamecontroller.gd

102 lines
2.5 KiB
GDScript

extends Node2D
var boxTotal := 4
var timer:=Timer.new()
var countdown := 10
var totalCrates := 0
var cratesDestroyed := 0
var totalCoins := 0
var totalCoinsCollected := 0
var totalBadGuys := 0
var player:Resource
#level information
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
var timers = [20, 15]
var currentLevel = 0
#Signals from GC
signal destroyBox(body)
signal destroyBadguy(area)
signal levelComplete (levelToLoad)
signal playerHurt(amt)
signal playerDeath
signal tick(timeRemaining)
signal coinsUpdate(coinsCollected, coinsAvailable)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player = load("res://scripts/res/playerstats.tres")
print("Player health " +str(player.health))
print("Number of levels: " +str(levels.size()))
reset()
add_child(timer)
timer.wait_time = 1
timer.one_shot = false
timer.connect("timeout", secondCounter)
timer.start()
pass # Replace with function body.
func reset():
totalCoinsCollected = 0
player.health = player.starting_health
countdown = timers[currentLevel]
tick.emit.call_deferred(countdown)
coinsUpdate.emit.call_deferred(totalCoinsCollected, totalCoins)
playerHurt.emit.call_deferred(player.health)
func secondCounter():
# print("one second")
countdown -=1
tick.emit(countdown)
if countdown <=0:
print("YOU LOSE")
levelComplete.emit(levels[currentLevel])
func coinCollected():
totalCoinsCollected +=1
coinsUpdate.emit(totalCoinsCollected, totalCoins)
print("GC knows coin collected")
func playerDamaged():
print("GC knows player taking damage")
player.health -=20
print("Health remaining: " +str(player.health))
if player.health > 0:
playerHurt.emit(player.health)
else:
playerDeath.emit()
func bulletHitBadguy(area):
#decide what to do
#how many bad guys reamining
#send a signal
destroyBadguy.emit(area)
func bulletHit(body):
print("Game controller knows bullet hit something")
if body.is_in_group("destructable"):
destroyBox.emit(body)
totalCrates -=1
cratesDestroyed +=1
if totalCrates <=0:
print("YOU WON!!")
currentLevel +=1
if currentLevel >= levels.size():
currentLevel = 0
levelComplete.emit(levels[currentLevel])
else:
print("Crates Remaining " +str(totalCrates))
# Coming from SceneManager
func countCrates(value):
totalCrates = value
func countCoins(value):
totalCoins = value
func countBadGuys(value):
totalBadGuys = value
func playerDead():
print("GC knows player is dead")
levelComplete.emit(levels[currentLevel])