GodotCourse/week6/updated_project/scripts/gamecontroller.gd

59 lines
1.6 KiB
GDScript3
Raw Normal View History

2024-08-19 15:21:33 +00:00
extends Node
#This node is a scene manager. It is used to control all the visible
#assets in the viewport. However the scene manager does not encapsulate
#overall logic, and it does not persist data - that belongs to the GameManager,
#which is an autoload singleton.
var coinsCollected = 0
var crate = preload("res://scenes/crate.tscn")
@onready var boxtrap = $"../crates/boxtrap"
@onready var timer = $Timer
@onready var player = $"../CharacterBody2D"
2024-08-19 19:27:40 +00:00
@onready var ui = $"../UICanvas/UI"
@onready var crates = $"../crates"
@onready var coins = $"../coins"
@onready var enemies = $"../enemies"
2024-08-19 15:21:33 +00:00
func _ready():
2024-08-19 19:27:40 +00:00
#Note: since autoloads persist, reset player to full health
2024-08-19 15:21:33 +00:00
GameManager.resetPlayer()
2024-08-19 19:27:40 +00:00
#Note: Add listeners to collectibles
for n in coins.get_children():
if n is Coin:
n.coinCollected.connect(coinCollected)
for n in enemies.get_children():
if n is Enemy:
n.playerDamage.connect(_on_badguy_player_damage)
2024-08-19 15:21:33 +00:00
func _on_area_2d_areatrigger(effect, body):
match effect:
"alert":
#do stuff
if body.is_in_group("player"):
for n in 3:
var box = crate.instantiate()
owner.add_child(box)
box.position = boxtrap.position
func coinCollected():
print("scene manager saw coin collected")
GameManager.coinsCollected +=1
2024-08-19 18:34:28 +00:00
ui.coinsUpdate(GameManager.coinsCollected)
2024-08-19 15:21:33 +00:00
func resetGame():
timer.start(.5)
func _on_timer_timeout():
get_tree().reload_current_scene()
#Scene control loop
func _process(delta):
if GameManager.player.health < 0 && player.living:
player.die()
func _on_badguy_player_damage():
player.hurt()
2024-08-19 18:34:28 +00:00
ui.healthUpdate(GameManager.player.health)