JanuaryGame/GodotProject/scripts/gamecontroller.gd

101 lines
2.4 KiB
GDScript

extends Node2D
var timer:= Timer.new()
var countdown = 10
var totalCrates := 0
var cratesDestroyed := 0
var totalCoins := 0
var totalCoinsCollected := 0
var totalEnemies := 0
#level information
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
var timers = [20,15]
var currentLevel = 0
var player:Resource
# signals from game controller
signal destroyBadguy(area)
signal destroyBox(body)
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():
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()
func reset():
countdown = timers[currentLevel]
player.health = player.starting_health
totalCoinsCollected = 0
tick.emit.call_deferred(countdown)
coinsUpdate.emit.call_deferred(totalCoinsCollected,totalCoins)
playerHurt.emit.call_deferred(player.health)
func secondCounter():
countdown -=1
tick.emit(countdown)
if countdown <=0:
print("you lose")
levelComplete.emit(levels[currentLevel])
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func coinCollected():
totalCoinsCollected+=1
coinsUpdate.emit(totalCoinsCollected, totalCoins)
print(totalCoinsCollected)
func playerDamaged():
player.health -= 20
print("game controller knows player damaged, health is now "+str(player.health))
if player.health > 0:
playerHurt.emit(player.health)
else:
playerDeath.emit()
func bullethitBadguy(area):
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
body.queue_free()
if totalCrates <=0:
print("you won")
#refactor this --make it a single
currentLevel +=1
if currentLevel >= levels.size():
currentLevel = 0
levelComplete.emit(levels[currentLevel])
else:
print("crates remaining: "+str(totalCrates))
# scene manager stuff
func countCrates(value):
totalCrates = value
func countCoins(value):
totalCoins = value
func playerDead():
print("gc knows player is dead")
levelComplete.emit(levels[currentLevel])