#13 inventaire fonctionnel
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
8
entities/interactables/scripts/actions/get_seed.gd
Normal file
8
entities/interactables/scripts/actions/get_seed.gd
Normal file
@@ -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")
|
||||
1
entities/interactables/scripts/actions/get_seed.gd.uid
Normal file
1
entities/interactables/scripts/actions/get_seed.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://j5xvcabrspyb
|
||||
@@ -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")
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user