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

18
scripts/game_manager.gd Normal file
View File

@@ -0,0 +1,18 @@
class_name GM
extends Node
var player_state = {
"coins": 0,
}
func add_coins(amount: int) -> void:
player_state["coins"] += amount
func set_coins(amount: int) -> void:
player_state["coins"] = amount
func get_coins() -> int:
return player_state["coins"]
func remove_coins(amount: int) -> void:
player_state["coins"] -= amount

View File

@@ -48,6 +48,9 @@ func _physics_process(delta):
if Input.is_action_pressed("jump") and (is_on_floor() or coyote_mode):
jump()
if Input.is_action_just_pressed("down"):
position.y += 1
var direction = Input.get_axis("left", "right")
if direction:

View File

@@ -0,0 +1,3 @@
extends Resource
@export var amount: int = 0