add collecting coins

This commit is contained in:
2024-12-30 01:00:26 +01:00
parent b862fa8084
commit d96963b6ef
14 changed files with 184 additions and 3 deletions

View File

@@ -0,0 +1,2 @@
class_name CanPickUpComponent
extends Node

View File

@@ -0,0 +1,22 @@
class_name CollectableComponent
extends Node
var root: Node
@export var area2d: Area2D
@export var collectable_data: Resource
signal collected(amount: int)
func _ready() -> void:
if area2d:
area2d.body_entered.connect(_on_area2d_body_entered)
else:
print("Collectable node missing Area2D child.")
root = get_parent()
func _on_area2d_body_entered(body: Node2D) -> void:
if body.has_node("CanPickUpComponent"):
collected.emit(collectable_data.amount)
root.queue_free()

View File

@@ -0,0 +1,20 @@
class_name ScoreComponent
extends Node
@onready var game_manager: GM = $"/root/GameManager"
func _ready():
await get_tree().process_frame
var coins = get_tree().get_nodes_in_group("coins")
for coin in coins:
coin.connect("collected", on_collected)
func on_collected(amount: int) -> void:
if not game_manager:
return
game_manager.add_coins(amount)
print("Coins: ", game_manager.get_coins())
# todo: play sound
# todo: update ui