122 lines
3.2 KiB
GDScript
122 lines
3.2 KiB
GDScript
class_name GameController extends Node2D
|
|
|
|
# Game Data
|
|
var totalCrates = 4
|
|
var cratesDestroyed = 0
|
|
var coinsCollected =0
|
|
var totalCoinsAvailable = 0
|
|
|
|
var enemies = 0
|
|
|
|
signal destroySignal(body)
|
|
signal levelCompleteSignal(level)
|
|
signal playerHurtSignal(healthRemaining, maxHealth)
|
|
signal playerKillSignal
|
|
signal timerUpdateSignal(timeRemaining, totalTimeForLevel)
|
|
signal coinUpdateSignal(coinsCollectedTotal, coinsAvailableTotal)
|
|
|
|
#level info
|
|
var timer:= Timer.new()
|
|
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn","res://scenes/level3.tscn"]
|
|
var timers = [20,15,25]
|
|
var currentLevel = 0
|
|
var countdown=0
|
|
|
|
var player:Resource
|
|
var slime:Resource
|
|
|
|
var enemiesDict={}
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
player = load("res://scripts/res/playerStats.tres")
|
|
slime = load("res://scripts/res/slimeStats.tres")
|
|
|
|
countdown = timers[currentLevel]
|
|
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.max_health
|
|
coinsCollected = 0
|
|
|
|
func secondCounter():
|
|
countdown -=1
|
|
timerUpdateSignal.emit(countdown, timers[currentLevel])
|
|
if countdown <=0:
|
|
print("YOU LOSE")
|
|
levelCompleteSignal.emit(levels[currentLevel])
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|
|
|
|
|
|
func _on_trigger_area_trigger(effect, body) -> void:
|
|
print("Do effect "+effect)
|
|
match effect:
|
|
"destroy":
|
|
#write code to destroy
|
|
if body is Crate:
|
|
destroySignal.emit(body)
|
|
cratesDestroyed +=1
|
|
"powerup":
|
|
if body is Player:
|
|
print("power up the player")
|
|
|
|
func bulletHit(body):
|
|
print("Gamecontroller knows bullet hit")
|
|
if body is Crate:
|
|
print("You hit a crate")
|
|
totalCrates -=1
|
|
destroySignal.emit(body)
|
|
print("Crates Remaining :: "+str(totalCrates - cratesDestroyed))
|
|
if totalCrates <=0:
|
|
print("You won!!!")
|
|
currentLevel +=1
|
|
if currentLevel >= levels.size():
|
|
currentLevel = 0
|
|
levelCompleteSignal.emit(levels[currentLevel])
|
|
|
|
func numberOfCrates(value):
|
|
totalCrates = value
|
|
print("Game Controller knows crates "+str(totalCrates) )
|
|
func numberOfCoins(value):
|
|
totalCoinsAvailable = value
|
|
|
|
func coinCollected(body, coin):
|
|
print("GC knows coin collected")
|
|
if body is Player:
|
|
#increase coins collected count
|
|
coinsCollected +=1
|
|
coinUpdateSignal.emit(coinsCollected, totalCoinsAvailable)
|
|
destroySignal.emit(coin)
|
|
func playerDamage(body, badguy):
|
|
if body is Player:
|
|
print("GC knows player taking damage")
|
|
print("Player health "+str(player.health)+" takes damage "+str(slime.meleeDamage))
|
|
player.health -= enemiesDict[badguy]["damage"]
|
|
if player.health > 0:
|
|
playerHurtSignal.emit(player.health, player.max_health)
|
|
else:
|
|
print("kill em")
|
|
playerKillSignal.emit()
|
|
print(enemiesDict[badguy]["health"])
|
|
|
|
func addBadGuy(enemy):
|
|
print("adding an enemy")
|
|
#use a dictionary
|
|
var enemyStats = {
|
|
"health": slime.health,
|
|
"damage": slime.meleeDamage
|
|
}
|
|
enemiesDict[enemy] = enemyStats
|
|
|
|
func numberOfBadguys(value):
|
|
enemies = value
|
|
func deathComplete():
|
|
print("GC knows death is complete")
|
|
levelCompleteSignal.emit(levels[currentLevel])
|