From bca23004438eff702f8e82480a59dddcde26e6d0 Mon Sep 17 00:00:00 2001 From: Altaezio Date: Sun, 3 Aug 2025 19:38:46 +0200 Subject: [PATCH] inventaire fonctionnel #13 --- .../resources/items/default_seed.tres | 11 +++++ common/inventory/scripts/generic_item.gd | 5 +++ common/inventory/scripts/generic_item.gd.uid | 1 + common/inventory/scripts/inventory.gd | 44 +++++++++++++++++++ common/inventory/scripts/inventory.gd.uid | 1 + common/inventory/scripts/items/seed_item.gd | 4 ++ .../inventory/scripts/items/seed_item.gd.uid | 1 + .../resources/actions/default_get_seed.tres | 7 +++ .../interactables/scripts/actions/get_seed.gd | 8 ++++ .../scripts/actions/get_seed.gd.uid | 1 + .../scripts/actions/water_plant.gd | 6 +-- .../interactables/scripts/interactable.gd | 2 +- entities/plants/default_plant.tscn | 37 ---------------- entities/player/scripts/player.gd | 12 +++-- gui/player_info/player_info.tscn | 8 ++++ gui/player_info/scripts/player_info.gd | 10 ++++- 16 files changed, 112 insertions(+), 46 deletions(-) create mode 100644 common/inventory/resources/items/default_seed.tres create mode 100644 common/inventory/scripts/generic_item.gd create mode 100644 common/inventory/scripts/generic_item.gd.uid create mode 100644 common/inventory/scripts/inventory.gd create mode 100644 common/inventory/scripts/inventory.gd.uid create mode 100644 common/inventory/scripts/items/seed_item.gd create mode 100644 common/inventory/scripts/items/seed_item.gd.uid create mode 100644 entities/interactables/resources/actions/default_get_seed.tres create mode 100644 entities/interactables/scripts/actions/get_seed.gd create mode 100644 entities/interactables/scripts/actions/get_seed.gd.uid delete mode 100644 entities/plants/default_plant.tscn 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/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/plants/default_plant.tscn b/entities/plants/default_plant.tscn deleted file mode 100644 index cff90d7..0000000 --- a/entities/plants/default_plant.tscn +++ /dev/null @@ -1,37 +0,0 @@ -[gd_scene load_steps=6 format=3 uid="uid://clpcqkdlj3d8e"] - -[ext_resource type="Script" uid="uid://cega715smavh3" path="res://entities/plants/scripts/plant.gd" id="1_d8u7e"] -[ext_resource type="Texture2D" uid="uid://c6vby5r0pfni2" path="res://entities/plants/assets/sprites/default_plant.png" id="4_dq24f"] -[ext_resource type="Texture2D" uid="uid://b3wom2xu26g43" path="res://entities/plants/assets/sprites/default_plant_glowing.png" id="5_2gcie"] - -[sub_resource type="CircleShape2D" id="CircleShape2D_cdbrd"] - -[sub_resource type="SpriteFrames" id="SpriteFrames_ocwgi"] -animations = [{ -"frames": [{ -"duration": 1.0, -"texture": ExtResource("4_dq24f") -}], -"loop": true, -"name": &"default", -"speed": 5.0 -}, { -"frames": [{ -"duration": 1.0, -"texture": ExtResource("5_2gcie") -}], -"loop": true, -"name": &"watered", -"speed": 5.0 -}] - -[node name="DefaultPlant" type="Area2D"] -script = ExtResource("1_d8u7e") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -scale = Vector2(4.01154, 4.01154) -shape = SubResource("CircleShape2D_cdbrd") - -[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] -scale = Vector2(0.160462, 0.160462) -sprite_frames = SubResource("SpriteFrames_ocwgi") diff --git a/entities/player/scripts/player.gd b/entities/player/scripts/player.gd index f8ebf89..bdbf474 100644 --- a/entities/player/scripts/player.gd +++ b/entities/player/scripts/player.gd @@ -1,7 +1,7 @@ extends CharacterBody2D class_name Player -signal player_stats_updated(player : Player) +signal player_updated(player: Player) var controlling_player : bool = true var planet : Planet # mis à jour par la classe Planet @@ -14,10 +14,14 @@ var max_energy : int = 10 var energy : int = max_energy : 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(): if controlling_player: @@ -27,7 +31,7 @@ func get_input(): action() 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) diff --git a/gui/player_info/player_info.tscn b/gui/player_info/player_info.tscn index 2a22b63..935a4b7 100644 --- a/gui/player_info/player_info.tscn +++ b/gui/player_info/player_info.tscn @@ -58,3 +58,11 @@ text = "0" label_settings = ExtResource("5_s4ggy") 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)