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
|
18
scripts/game_manager.gd
Normal file
18
scripts/game_manager.gd
Normal 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
|
@@ -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:
|
||||
|
3
scripts/resources/collectable_resource.gd
Normal file
3
scripts/resources/collectable_resource.gd
Normal file
@@ -0,0 +1,3 @@
|
||||
extends Resource
|
||||
|
||||
@export var amount: int = 0
|
Reference in New Issue
Block a user