diff --git a/behaviours/collectible.gd b/behaviours/collectible.gd index 2ed6967..63d85c4 100644 --- a/behaviours/collectible.gd +++ b/behaviours/collectible.gd @@ -3,11 +3,15 @@ extends Node class_name Collectible -## Marker class that stores the value of a collectible item. - - ## How many of that item this [Collectible] represents. @export var quantity: int = 1 ## What kind of item this [Collectible] represents. -@export var kind: StringName = &"" \ No newline at end of file +@export var kind: StringName = &"" + + +@onready var game: MainGame = MainGame.get_via_group(self) + + +func apply(): + game.inventory.get_counter(kind).increase(quantity) \ No newline at end of file diff --git a/entities/gold.gd b/entities/gold.gd index 97e8bcb..9edb2dd 100644 --- a/entities/gold.gd +++ b/entities/gold.gd @@ -2,8 +2,10 @@ extends Node2D class_name Gold -@onready var move_towards: MoveTowards = $"%MoveTowards" -@onready var collect_sound_spawner: Spawner = $"%CollectSoundSpawner" +@onready var game: MainGame = MainGame.get_via_group(self) +@onready var move_towards: MoveTowards = %"MoveTowards" +@onready var collect_sound_spawner: Spawner = %"CollectSoundSpawner" +@onready var collectible: Collectible = %"Collectible" func magnetize(cursor: Cursor) -> void: @@ -13,6 +15,7 @@ func demagnetize() -> void: move_towards.target = null func collect() -> void: + collectible.apply() collect_sound_spawner.spawn() queue_free() diff --git a/entities/gold.tscn b/entities/gold.tscn index e0617b8..44584a3 100644 --- a/entities/gold.tscn +++ b/entities/gold.tscn @@ -16,7 +16,8 @@ radius = 64.0 [node name="Gold" type="Node2D"] script = ExtResource("1_lbls1") -[node name="Collectable" parent="." instance=ExtResource("2_j75yq")] +[node name="Collectible" parent="." instance=ExtResource("2_j75yq")] +unique_name_in_owner = true kind = &"Gold" [node name="Sprite" type="Sprite2D" parent="."] diff --git a/scenes/game/inventory.gd b/scenes/game/inventory.gd new file mode 100644 index 0000000..eaa56e9 --- /dev/null +++ b/scenes/game/inventory.gd @@ -0,0 +1,20 @@ +extends Node +class_name Inventory + + +signal new_counter_created(kind: StringName, counter: Counter) + + +@export var counter_scene: PackedScene + + +func get_counter(kind: StringName) -> Counter: + var path: NodePath = NodePath(kind) + var counter: Counter = get_node_or_null(path) + if counter != null: + return counter + counter = counter_scene.instantiate() + counter.name = kind + add_child(counter) + new_counter_created.emit(kind, counter) + return counter diff --git a/scenes/game/inventory.tscn b/scenes/game/inventory.tscn new file mode 100644 index 0000000..1864338 --- /dev/null +++ b/scenes/game/inventory.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=3 format=3 uid="uid://cu6mvnfa01nb6"] + +[ext_resource type="Script" path="res://scenes/game/inventory.gd" id="1_yji81"] +[ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="2_vhog3"] + +[node name="Inventory" type="Node"] +script = ExtResource("1_yji81") + +[node name="Gold" parent="." instance=ExtResource("2_vhog3")] diff --git a/scenes/game/main_game.gd b/scenes/game/main_game.gd index ef5172c..c6612e2 100644 --- a/scenes/game/main_game.gd +++ b/scenes/game/main_game.gd @@ -2,9 +2,9 @@ extends Node2D class_name MainGame -@onready var gold_counter: Counter = $"GoldCounter" @onready var camera: GameCamera = $"GameCamera" @onready var default_spawn_parent: Node2D = %"DefaultSpawnParent" +@onready var inventory: Inventory = %"Inventory" static func get_via_group(node: Node) -> MainGame: diff --git a/scenes/game/main_game.tscn b/scenes/game/main_game.tscn index 87d2515..b0f24fe 100644 --- a/scenes/game/main_game.tscn +++ b/scenes/game/main_game.tscn @@ -2,8 +2,8 @@ [ext_resource type="Script" path="res://scenes/game/main_game.gd" id="1_wiglu"] [ext_resource type="PackedScene" uid="uid://dm068vaseh45n" path="res://scenes/game/game_camera.tscn" id="2_db5xs"] +[ext_resource type="PackedScene" uid="uid://cu6mvnfa01nb6" path="res://scenes/game/inventory.tscn" id="2_jhbbf"] [ext_resource type="Texture2D" uid="uid://d13j4br4hxek6" path="res://scenes/game/tileset_grass.png" id="2_o7bg5"] -[ext_resource type="PackedScene" uid="uid://brvbtvt4em32" path="res://behaviours/counter.tscn" id="3_p6jw3"] [ext_resource type="PackedScene" uid="uid://col1q3elvkfwk" path="res://scenes/game/cursor.tscn" id="5_g504x"] [ext_resource type="PackedScene" uid="uid://bc2bm8lbol18w" path="res://entities/sheep.tscn" id="6_j2kdp"] [ext_resource type="PackedScene" uid="uid://4d3ksr3171x4" path="res://entities/imp.tscn" id="7_7od2n"] @@ -822,6 +822,9 @@ radius = 48.0 [node name="MainGame" type="Node2D" groups=["game"]] script = ExtResource("1_wiglu") +[node name="Inventory" parent="." instance=ExtResource("2_jhbbf")] +unique_name_in_owner = true + [node name="TileMap" type="TileMap" parent="."] scale = Vector2(2, 2) tile_set = SubResource("TileSet_g2dkm") @@ -830,8 +833,6 @@ layer_0/tile_data = PackedInt32Array(720915, 262144, 4, 655379, 0, 8, 589843, 26 [node name="GameCamera" parent="." instance=ExtResource("2_db5xs")] -[node name="GoldCounter" parent="." instance=ExtResource("3_p6jw3")] - [node name="DefaultSpawnParent" type="Node2D" parent="."] unique_name_in_owner = true