diff --git a/common/inventory/resources/items/default_seed.tres b/common/inventory/resources/items/default_seed.tres new file mode 100644 index 0000000..e44f7c9 --- /dev/null +++ b/common/inventory/resources/items/default_seed.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="SeedItem" load_steps=3 format=3 uid="uid://lrl2okkhyxmx"] + +[ext_resource type="Texture2D" uid="uid://dcgnamu7sb3ov" path="res://gui/player_info/assets/icons/bolt.svg" id="1_dy25s"] +[ext_resource type="Script" uid="uid://bypjcvlc15gsm" path="res://common/inventory/scripts/items/seed_item.gd" id="2_mgcdi"] + +[resource] +script = ExtResource("2_mgcdi") +plant_type = "" +name = "Boule" +icon = ExtResource("1_dy25s") +metadata/_custom_type_script = "uid://bypjcvlc15gsm" diff --git a/common/inventory/scripts/generic_item.gd b/common/inventory/scripts/generic_item.gd new file mode 100644 index 0000000..5bb6030 --- /dev/null +++ b/common/inventory/scripts/generic_item.gd @@ -0,0 +1,5 @@ +extends Resource +class_name GenericItem + +@export var name: String +@export var icon: Texture2D diff --git a/common/inventory/scripts/generic_item.gd.uid b/common/inventory/scripts/generic_item.gd.uid new file mode 100644 index 0000000..a477ac1 --- /dev/null +++ b/common/inventory/scripts/generic_item.gd.uid @@ -0,0 +1 @@ +uid://do1a37cqva05e diff --git a/common/inventory/scripts/inventory.gd b/common/inventory/scripts/inventory.gd new file mode 100644 index 0000000..ebf0752 --- /dev/null +++ b/common/inventory/scripts/inventory.gd @@ -0,0 +1,44 @@ +extends Resource +class_name Inventory + +signal inventory_changed(inventory: Inventory) + +@export var items: Array[GenericItem] = [] +@export var max_items: int = 10 + +func add_item(item: GenericItem): + if items.size() < max_items: + items.append(item) + emit_signal("inventory_changed", self) + return true + else: + return false + +func add_items(items_to_add: Array[GenericItem], fillup: bool = false): + if fillup: + var has_changed := false + for i in min(items_to_add.size(), max_items - items.size()): + items.append(items_to_add[i]) + has_changed = true + if has_changed: + emit_signal("inventory_changed", self) + return has_changed + elif !fillup && items.size() + items_to_add.size() < max_items: + items.append_array(items_to_add) + emit_signal("inventory_changed", self) + return true + + +func get_item(ind: int = 0): + return items[ind] + +func get_and_remove_item(ind: int = 0): + var item_removed: GenericItem = items.pop_at(ind) + emit_signal("inventory_changed", self) + return item_removed + +func swap_items(item_to_add: GenericItem, ind_to_get: int = 0): + var item_to_get := items[ind_to_get] + items[ind_to_get] = item_to_add + emit_signal("inventory_changed", self) + return item_to_get diff --git a/common/inventory/scripts/inventory.gd.uid b/common/inventory/scripts/inventory.gd.uid new file mode 100644 index 0000000..1b79328 --- /dev/null +++ b/common/inventory/scripts/inventory.gd.uid @@ -0,0 +1 @@ +uid://fnu2d6wna4yc diff --git a/common/inventory/scripts/items/seed_item.gd b/common/inventory/scripts/items/seed_item.gd new file mode 100644 index 0000000..f8f28e4 --- /dev/null +++ b/common/inventory/scripts/items/seed_item.gd @@ -0,0 +1,4 @@ +extends GenericItem +class_name SeedItem + +@export var plant_type: String diff --git a/common/inventory/scripts/items/seed_item.gd.uid b/common/inventory/scripts/items/seed_item.gd.uid new file mode 100644 index 0000000..99fddb0 --- /dev/null +++ b/common/inventory/scripts/items/seed_item.gd.uid @@ -0,0 +1 @@ +uid://bypjcvlc15gsm diff --git a/entities/interactables/plants/default_plant.tscn b/entities/interactables/plants/default_plant.tscn index 7d65b7a..0cdb0dc 100644 --- a/entities/interactables/plants/default_plant.tscn +++ b/entities/interactables/plants/default_plant.tscn @@ -1,10 +1,12 @@ -[gd_scene load_steps=8 format=3 uid="uid://clpcqkdlj3d8e"] +[gd_scene load_steps=10 format=3 uid="uid://clpcqkdlj3d8e"] [ext_resource type="Script" uid="uid://cega715smavh3" path="res://entities/interactables/plants/scripts/plant.gd" id="1_d8u7e"] +[ext_resource type="Resource" uid="uid://lrl2okkhyxmx" path="res://common/inventory/resources/items/default_seed.tres" id="2_5foug"] [ext_resource type="Script" uid="uid://yutflvdgdk04" path="res://entities/interactables/scripts/interactable_action.gd" id="2_rs46h"] [ext_resource type="Resource" uid="uid://bk0kop0m75pjy" path="res://entities/interactables/resources/actions/default_water_plant.tres" id="3_5foug"] [ext_resource type="Texture2D" uid="uid://c6vby5r0pfni2" path="res://entities/interactables/plants/assets/sprites/default_plant.png" id="4_dq24f"] [ext_resource type="Texture2D" uid="uid://b3wom2xu26g43" path="res://entities/interactables/plants/assets/sprites/default_plant_glowing.png" id="5_2gcie"] +[ext_resource type="Resource" uid="uid://chmmu2jlo2eu0" path="res://entities/interactables/resources/actions/default_get_seed.tres" id="5_5foug"] [sub_resource type="CircleShape2D" id="CircleShape2D_cdbrd"] @@ -29,7 +31,8 @@ animations = [{ [node name="DefaultPlant" type="Area2D"] script = ExtResource("1_d8u7e") -actions = Array[ExtResource("2_rs46h")]([ExtResource("3_5foug")]) +seed_item = ExtResource("2_5foug") +actions = Array[ExtResource("2_rs46h")]([ExtResource("3_5foug"), ExtResource("5_5foug")]) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] scale = Vector2(4.01154, 4.01154) diff --git a/entities/interactables/plants/scripts/plant.gd b/entities/interactables/plants/scripts/plant.gd index b609577..65cec21 100644 --- a/entities/interactables/plants/scripts/plant.gd +++ b/entities/interactables/plants/scripts/plant.gd @@ -1,7 +1,9 @@ extends Interactable class_name Plant -var watered : bool = false : set = set_watered +@export var seed_item : GenericItem + +var watered: bool = false: set = set_watered func set_watered(_watered): watered = _watered diff --git a/entities/interactables/resources/actions/default_get_seed.tres b/entities/interactables/resources/actions/default_get_seed.tres new file mode 100644 index 0000000..8dd4ed1 --- /dev/null +++ b/entities/interactables/resources/actions/default_get_seed.tres @@ -0,0 +1,7 @@ +[gd_resource type="Resource" script_class="GetSeedInteraction" load_steps=2 format=3 uid="uid://chmmu2jlo2eu0"] + +[ext_resource type="Script" uid="uid://j5xvcabrspyb" path="res://entities/interactables/scripts/actions/get_seed.gd" id="1_ys78p"] + +[resource] +script = ExtResource("1_ys78p") +metadata/_custom_type_script = "uid://j5xvcabrspyb" diff --git a/entities/interactables/scripts/actions/get_seed.gd b/entities/interactables/scripts/actions/get_seed.gd new file mode 100644 index 0000000..4fe3e3e --- /dev/null +++ b/entities/interactables/scripts/actions/get_seed.gd @@ -0,0 +1,8 @@ +extends InteractableAction +class_name GetSeedInteraction + +func action(p: Player, i : Interactable): + if i is Plant: + p.inventory.add_item(i.seed_item) + else : + printerr("No plant selected or interactable is not a plant") \ No newline at end of file diff --git a/entities/interactables/scripts/actions/get_seed.gd.uid b/entities/interactables/scripts/actions/get_seed.gd.uid new file mode 100644 index 0000000..6573007 --- /dev/null +++ b/entities/interactables/scripts/actions/get_seed.gd.uid @@ -0,0 +1 @@ +uid://j5xvcabrspyb diff --git a/entities/interactables/scripts/actions/water_plant.gd b/entities/interactables/scripts/actions/water_plant.gd index 2e9ec71..591c287 100644 --- a/entities/interactables/scripts/actions/water_plant.gd +++ b/entities/interactables/scripts/actions/water_plant.gd @@ -1,8 +1,8 @@ extends InteractableAction class_name WaterPlantAction -func action(_p: Player, _i : Interactable): - if _i is Plant: - _i.watered = true +func action(_p: Player, i : Interactable): + if i is Plant: + i.watered = true else : printerr("No plant selected or interactable is not a plant") \ No newline at end of file diff --git a/entities/interactables/scripts/interactable.gd b/entities/interactables/scripts/interactable.gd index 16c0091..8368561 100644 --- a/entities/interactables/scripts/interactable.gd +++ b/entities/interactables/scripts/interactable.gd @@ -1,7 +1,7 @@ extends Area2D class_name Interactable -@export var actions : Array[InteractableAction] = []; +@export var actions : Array[InteractableAction] = [] func interact(p : Player): for a in actions: diff --git a/entities/player/scripts/player.gd b/entities/player/scripts/player.gd index c6b9a3f..eee8351 100644 --- a/entities/player/scripts/player.gd +++ b/entities/player/scripts/player.gd @@ -1,17 +1,23 @@ extends CharacterBody2D class_name Player -signal player_stats_updated(player : Player) +signal player_updated(player: Player) @export var speed = 400 -var energy : int = 10 : +@onready var inventory: Inventory = Inventory.new() + +var energy: int = 10: set(v): energy = v - emit_signal("player_stats_updated", self) + emit_signal("player_updated", self) func _ready(): - emit_signal("player_stats_updated", self) + emit_signal("player_updated", self) + inventory.inventory_changed.connect(_on_inventory_updated) + +func _on_inventory_updated(_inventory: Inventory): + emit_signal("player_updated", self) func get_input(): calculate_direction() @@ -20,13 +26,13 @@ func get_input(): try_interact() func calculate_direction(): - var input_direction : Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down") + var input_direction: Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down") velocity = input_direction * speed if input_direction.x: $Sprite.flip_h = (input_direction.x < 0) func try_interact(): - var interactables : Array[Interactable] + var interactables: Array[Interactable] for area2D in $InteractArea2D.get_overlapping_areas(): if area2D is Interactable: @@ -36,7 +42,7 @@ func try_interact(): if len(interactables) > 1: # Sort them to the closer interactables.sort_custom( - func (el1 : Interactable, el2 : Interactable): + func(el1: Interactable, el2: Interactable): return el1.global_position.distance_to(global_position) > el2.global_position.distance_to(global_position) ) diff --git a/gui/player_info/player_info.tscn b/gui/player_info/player_info.tscn index ea4ffa3..eaf3fac 100644 --- a/gui/player_info/player_info.tscn +++ b/gui/player_info/player_info.tscn @@ -60,3 +60,11 @@ text = "0" label_settings = SubResource("LabelSettings_bye71") horizontal_alignment = 1 vertical_alignment = 1 + +[node name="Inventory" type="HBoxContainer" parent="."] +layout_mode = 0 +offset_left = 157.0 +offset_top = 86.0 +offset_right = 291.0 +offset_bottom = 126.0 +alignment = 1 diff --git a/gui/player_info/scripts/player_info.gd b/gui/player_info/scripts/player_info.gd index d2a45eb..1e1157e 100644 --- a/gui/player_info/scripts/player_info.gd +++ b/gui/player_info/scripts/player_info.gd @@ -1,4 +1,12 @@ extends Control -func _on_root_gui_player_stats_updated(player:Player): +func _on_root_gui_player_updated(player: Player): $EnergyInfo/Label.text = str(player.energy) + + var children = $Inventory.get_children() + for child in children: + child.free() + for item in player.inventory.items: + var item_rect = TextureRect.new() + item_rect.texture = item.icon + $Inventory.add_child(item_rect) diff --git a/gui/root_gui.tscn b/gui/root_gui.tscn index 6c16cc0..23cccf9 100644 --- a/gui/root_gui.tscn +++ b/gui/root_gui.tscn @@ -24,4 +24,4 @@ offset_bottom = 160.0 grow_horizontal = 1 grow_vertical = 1 -[connection signal="player_stats_updated" from="." to="PlayerInfo" method="_on_root_gui_player_stats_updated"] +[connection signal="player_updated" from="." to="PlayerInfo" method="_on_root_gui_player_updated"] diff --git a/gui/scripts/root_gui.gd b/gui/scripts/root_gui.gd index b9679a9..c5518b8 100644 --- a/gui/scripts/root_gui.gd +++ b/gui/scripts/root_gui.gd @@ -1,7 +1,7 @@ extends Control class_name RootGui -signal player_stats_updated(player : Player) +signal player_updated(player: Player) -func _on_player_player_stats_updated(player:Player): - emit_signal("player_stats_updated", player) +func _on_player_player_updated(player: Player): + emit_signal("player_updated", player) diff --git a/root.tscn b/root.tscn index 1aa8b45..5d962d1 100644 --- a/root.tscn +++ b/root.tscn @@ -53,4 +53,4 @@ position = Vector2(-269, 1) position = Vector2(2.22, 0) following = NodePath("../Entities/Player") -[connection signal="player_stats_updated" from="Entities/Player" to="CanvasLayer/RootGui" method="_on_player_player_stats_updated"] +[connection signal="player_updated" from="Entities/Player" to="CanvasLayer/RootGui" method="_on_player_player_updated"]