JanuaryGodotGame/januaryproject/scripts/gamecontroller.gd

102 lines
2.5 KiB
GDScript3
Raw Permalink Normal View History

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
2025-01-21 01:43:55 +00:00
var player:Resource
#level information
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
2025-02-11 02:12:59 +00:00
var timers = [20, 25]
var currentLevel = 0
#Signals from GC
2025-01-21 01:43:55 +00:00
signal destroyBox(body)
2025-02-11 02:12:59 +00:00
signal destroyBadguy(area)
signal levelComplete(levelToLoad)
2025-02-11 02:12:59 +00:00
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()
func reset():
2025-02-11 02:12:59 +00:00
totalCoinsCollected = 0
player.health = player.starting_health
countdown = timers[currentLevel]
2025-02-11 02:12:59 +00:00
tick.emit.call_deferred(countdown)
coinsUpdate.emit.call_deferred(totalCoinsCollected, totalCoins)
playerHurt.emit.call_deferred(player.health)
func secondCounter():
2025-01-14 00:29:58 +00:00
countdown -=1
2025-02-11 02:12:59 +00:00
tick.emit(countdown)
if countdown <=0:
print("YOU LOSE")
levelComplete.emit(levels[currentLevel])
func coinCollected():
totalCoinsCollected += 1
2025-02-11 02:12:59 +00:00
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))
2025-02-11 02:12:59 +00:00
if player.health > 0:
playerHurt.emit(player.health)
else:
playerDeath.emit()
func bulletHitBadguy(area):
#decide what to do
#how many bad guys remaining
#send a signal
destroyBadguy.emit(area)
2025-01-14 01:58:50 +00:00
func bulletHit(body):
print("Game controller knows bullet hit something")
if body.is_in_group("destructable"):
2025-01-21 01:43:55 +00:00
destroyBox.emit(body)
totalCrates -=1
cratesDestroyed +=1
if totalCrates <=0:
print("YOU WON!!")
#refactor this - make it a signal
currentLevel +=1
if currentLevel >= levels.size():
currentLevel = 0
levelComplete.emit(levels[currentLevel])
else:
print("Crates Remaining "+str(totalCrates) )
# Coming from SceneManager
2025-01-21 01:43:55 +00:00
func countCrates(value):
totalCrates = value
func countCoins(value):
totalCoins = value
func countBadGuys(value):
totalBadGuys = value
2025-02-11 02:12:59 +00:00
func playerDead():
print("GC knows player death")
levelComplete.emit(levels[currentLevel])