1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-24 09:04:22 +00:00

Add inventory system, with only Gold for now

This commit is contained in:
Steffo 2024-04-21 23:49:34 +02:00
parent 92550ba865
commit e9f5c982b1
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
7 changed files with 49 additions and 11 deletions

View file

@ -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 = &""
@export var kind: StringName = &""
@onready var game: MainGame = MainGame.get_via_group(self)
func apply():
game.inventory.get_counter(kind).increase(quantity)

View file

@ -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()

View file

@ -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="."]

20
scenes/game/inventory.gd Normal file
View file

@ -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

View file

@ -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")]

View file

@ -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:

View file

@ -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