GodotCourse/scripts/gamecontroller.gd

50 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-08-13 01:10:32 +00:00
extends Node
2024-07-29 22:14:19 +00:00
2024-07-30 01:07:47 +00:00
var myBox = preload("res://scenes/crate.tscn")
2024-08-13 01:10:32 +00:00
@onready var box_trap = $"../BoxTrap"
@onready var timer = $Timer
@onready var player = $"../CharacterBody2D"
@onready var coins = $"../Coins"
@onready var enemies = $"../Enemies"
@onready var ui = $"../CanvasLayer/Control"
2024-07-29 22:14:19 +00:00
# Called when the node enters the scene tree for the first time.
func _ready():
GameManager.resetPlayer()
GameManager.resetCoinCollectedCount()
2024-07-29 22:14:19 +00:00
for n in coins.get_children():
if n is Coin:
n.coinCollected.connect(_on_area_2d_coin_collected)
for n in enemies.get_children():
if n is Enemy:
n.playerDamage.connect(playerDamage)
2024-07-29 22:14:19 +00:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if GameManager.player.currentHealth <= 0 and player.living:
player.defeated()
timer.start()
2024-07-29 22:14:19 +00:00
2024-07-30 01:07:47 +00:00
func _on_area_2d_areatrigger(effect, body):
match effect:
"alert":
for n in 5:
var box = myBox.instantiate()
2024-08-13 01:10:32 +00:00
owner.add_child(box)
2024-07-30 01:07:47 +00:00
box.transform = box_trap.global_transform
2024-08-13 01:10:32 +00:00
func resetWorld():
get_tree().reload_current_scene()
func _on_area_2d_coin_collected():
GameManager.coinCollected()
ui.coinsUpdate(GameManager.coinsCollectedTotal)
func playerDamage():
GameManager.playerDamage()
ui.healthUpdate(GameManager.player.currentHealth)
func resetGame():
timer.start()