2026-03-10 01:04:51 +00:00
|
|
|
class_name GameController extends Node2D
|
2026-03-03 02:00:36 +00:00
|
|
|
|
2026-03-10 01:04:51 +00:00
|
|
|
var crateTotal = 0
|
|
|
|
|
signal destroySignal(body)
|
|
|
|
|
signal teleportSignal(body)
|
|
|
|
|
signal levelChangeSignal(level)
|
2026-03-30 21:35:07 +00:00
|
|
|
|
|
|
|
|
var coinsCollected = 0
|
2026-03-10 01:04:51 +00:00
|
|
|
|
|
|
|
|
var timer := Timer.new()
|
2026-03-30 21:35:07 +00:00
|
|
|
|
2026-03-30 21:50:14 +00:00
|
|
|
#Level Handling
|
2026-03-30 21:35:07 +00:00
|
|
|
var levels=["res://scenes/game.tscn","res://scenes/level2.tscn"]
|
|
|
|
|
var timers=[16,10]
|
|
|
|
|
var currentLevel = 0
|
|
|
|
|
var timeAvailable = timers[currentLevel]
|
|
|
|
|
|
|
|
|
|
var enemiesDict={}
|
|
|
|
|
var playerStartingHealth = 50
|
|
|
|
|
var playerHealth = 50
|
2026-03-03 02:00:36 +00:00
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
|
func _ready() -> void:
|
2026-03-17 00:58:20 +00:00
|
|
|
get_window().grab_focus()
|
2026-03-10 01:04:51 +00:00
|
|
|
add_child(timer)
|
|
|
|
|
timer.wait_time = 1
|
|
|
|
|
timer.one_shot= false
|
|
|
|
|
timer.connect("timeout", secondCounter)
|
|
|
|
|
timer.start()
|
2026-03-03 02:00:36 +00:00
|
|
|
|
2026-03-30 21:50:14 +00:00
|
|
|
#Scene Handling
|
|
|
|
|
# called by the scenemanager upon scene load
|
2026-03-30 21:35:07 +00:00
|
|
|
func reset()->void:
|
|
|
|
|
# reset the time
|
|
|
|
|
timeAvailable = timers[currentLevel]
|
|
|
|
|
enemiesDict.clear()
|
|
|
|
|
playerHealth = playerStartingHealth
|
2026-03-30 21:50:14 +00:00
|
|
|
|
|
|
|
|
#Time Handling
|
2026-03-10 01:04:51 +00:00
|
|
|
func secondCounter()->void:
|
|
|
|
|
timeAvailable -=1
|
|
|
|
|
if timeAvailable <=0:
|
|
|
|
|
print("YOU LOSE BABY!!")
|
2026-03-30 21:35:07 +00:00
|
|
|
levelChangeSignal.emit(levels[currentLevel])
|
2026-03-03 02:00:36 +00:00
|
|
|
|
2026-03-30 21:50:14 +00:00
|
|
|
#Coin Handling
|
2026-03-30 21:35:07 +00:00
|
|
|
func _on_coin_collected(body:Player, coin:Coin)-> void:
|
|
|
|
|
coinsCollected +=1
|
|
|
|
|
destroySignal.emit(coin)
|
|
|
|
|
|
|
|
|
|
#Any time coins are destroyed, game controller is informed how many left
|
|
|
|
|
func totalCoins(value)-> void:
|
|
|
|
|
if value <=0:
|
|
|
|
|
print("You WON!!!")
|
|
|
|
|
currentLevel +=1
|
|
|
|
|
if currentLevel >= levels.size():
|
|
|
|
|
currentLevel = 0
|
|
|
|
|
levelChangeSignal.emit(levels[currentLevel])
|
2026-03-30 21:50:14 +00:00
|
|
|
|
|
|
|
|
#Triggers are area traps
|
2026-03-10 01:04:51 +00:00
|
|
|
func _on_trigger(body: Variant, effect, trigger) -> void:
|
2026-03-03 02:00:36 +00:00
|
|
|
print("GC knows trigger...."+effect)
|
|
|
|
|
if body is Crate:
|
|
|
|
|
match effect:
|
|
|
|
|
"destroy":
|
2026-03-10 01:04:51 +00:00
|
|
|
destroySignal.emit(body)
|
|
|
|
|
"teleport":
|
|
|
|
|
print("GC teleport an object")
|
|
|
|
|
teleportSignal.emit(body)
|
|
|
|
|
|
|
|
|
|
if body is Player:
|
|
|
|
|
match effect:
|
|
|
|
|
"powerup":
|
|
|
|
|
print("GC knows about powerup")
|
|
|
|
|
timeAvailable +=5
|
|
|
|
|
destroySignal.emit(trigger)
|
2026-03-30 21:50:14 +00:00
|
|
|
|
|
|
|
|
#Enemy Handling
|
2026-03-30 21:35:07 +00:00
|
|
|
func _on_slime_damage(body, slime)->void:
|
|
|
|
|
# print("Slime attacked player for "+str(enemiesDict[slime]["damage"]))
|
|
|
|
|
playerHealth -= enemiesDict[slime]["damage"]
|
|
|
|
|
if playerHealth<=0:
|
|
|
|
|
print("PLAYER KILLED!")
|
|
|
|
|
#reload current level
|
|
|
|
|
levelChangeSignal.emit(levels[currentLevel])
|
|
|
|
|
func totalEnemies(value)->void:
|
|
|
|
|
print("Number of Enemies: "+str(value))
|
|
|
|
|
func addEnemyToLevel(slime:Slime)->void:
|
|
|
|
|
#random number up to 10
|
|
|
|
|
var randDamage:int = randi()%10
|
|
|
|
|
var enemyStat = {
|
|
|
|
|
"health":50,
|
|
|
|
|
"damage":randDamage
|
|
|
|
|
}
|
|
|
|
|
#store in the enemy Dictionary
|
|
|
|
|
enemiesDict[slime]=enemyStat
|
|
|
|
|
func removeEnemyFromLevel(slime)->void:
|
|
|
|
|
enemiesDict.erase(slime)
|
|
|
|
|
|
2026-03-10 01:04:51 +00:00
|
|
|
func crateUpdate(cratesAmount)->void:
|
|
|
|
|
crateTotal = cratesAmount
|
|
|
|
|
print("GC updated crates: "+str(crateTotal))
|
2026-03-17 00:58:20 +00:00
|
|
|
if crateTotal <=0:
|
|
|
|
|
print("You WON!!!")
|
2026-03-30 21:35:07 +00:00
|
|
|
currentLevel +=1
|
|
|
|
|
if currentLevel >= levels.size():
|
|
|
|
|
currentLevel = 0
|
|
|
|
|
levelChangeSignal.emit(levels[currentLevel])
|
2026-03-17 00:58:20 +00:00
|
|
|
func bulletDamage(body:Node2D, bullet:Bullet)->void:
|
|
|
|
|
if body is Crate:
|
|
|
|
|
#destroy crate
|
|
|
|
|
destroySignal.emit(body)
|
|
|
|
|
destroySignal.emit(bullet)
|
2026-03-30 21:50:14 +00:00
|
|
|
#Update to damage Slime Enemies
|
2026-03-30 21:35:07 +00:00
|
|
|
if body is Slime:
|
|
|
|
|
#damage the slime
|
|
|
|
|
enemiesDict[body]["health"] -= 10
|
|
|
|
|
if enemiesDict[body]["health"] <=0:
|
|
|
|
|
removeEnemyFromLevel(body)
|
|
|
|
|
destroySignal.emit(body)
|
|
|
|
|
destroySignal.emit(bullet)
|