add collecting coins
This commit is contained in:
2
scripts/components/can_pickup.gd
Normal file
2
scripts/components/can_pickup.gd
Normal file
@@ -0,0 +1,2 @@
|
||||
class_name CanPickUpComponent
|
||||
extends Node
|
22
scripts/components/collectable.gd
Normal file
22
scripts/components/collectable.gd
Normal 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()
|
20
scripts/components/score.gd
Normal file
20
scripts/components/score.gd
Normal 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
|
Reference in New Issue
Block a user