#37 ajout d'item outils ainsi que de visualisation de leur zone d'effet (avec la classe ActionArea

This commit is contained in:
Zacharie Guet 2025-08-19 11:29:20 +02:00
parent b0efeff809
commit 1f301815be
18 changed files with 233 additions and 89 deletions

View File

@ -1,7 +1,7 @@
[gd_resource type="Resource" script_class="SeedItem" load_steps=3 format=3 uid="uid://lrl2okkhyxmx"]
[gd_resource type="Resource" script_class="Seed" load_steps=3 format=3 uid="uid://lrl2okkhyxmx"]
[ext_resource type="Texture2D" uid="uid://dcgnamu7sb3ov" path="res://gui/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"]
[ext_resource type="Script" uid="uid://bypjcvlc15gsm" path="res://common/inventory/scripts/items/seed.gd" id="2_mgcdi"]
[resource]
script = ExtResource("2_mgcdi")

View File

@ -31,15 +31,26 @@ func add_items(items_to_add: Array[Item], fillup: bool = false):
func lenght() -> int:
return len(items)
func get_item(ind: int = 0):
func set_item(item : Item, ind: int = 0) -> bool:
if ind >= max_items:
return false
while len(items) <= ind:
items.append(null)
items[ind] = item
emit_signal("inventory_changed", self)
return true
func get_item(ind: int = 0) -> Item:
if len(items) <= ind:
return null;
return items[ind]
func pop_item(ind: int = 0):
func pop_item(ind: int = 0) -> Item:
var item_removed: Item = items.pop_at(ind)
emit_signal("inventory_changed", self)
return item_removed
func swap_items(item_to_add: Item, ind_to_get: int = 0):
func swap_items(item_to_add: Item, ind_to_get: int = 0) -> Item:
var item_to_get := items[ind_to_get]
items[ind_to_get] = item_to_add
emit_signal("inventory_changed", self)

View File

@ -1,6 +1,6 @@
@tool
extends Item
class_name SeedItem
class_name Seed
@export var plant_type: PlantType :
set(v):

View File

@ -0,0 +1,12 @@
extends ToolItem
class_name Shovel
func can_use(player : Player) -> bool:
var areas = player.action_area.get_overlapping_areas()
for area in areas :
if area is Plant:
return true
return false
func use(_player : Player) -> bool:
return false

View File

@ -0,0 +1 @@
uid://dya38x1h1uiyg

View File

@ -0,0 +1,11 @@
extends Item
class_name ToolItem
@export var area_width: float = 50
@export var area_distance: float = 50
func generate_action_area():
return ActionArea.new(
area_width,
area_distance
)

View File

@ -0,0 +1 @@
uid://jom8ulyopso

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=2 format=3 uid="uid://cd3re3552pt7m"]
[gd_scene load_steps=2 format=3 uid="uid://b0ecjfdnvhjjf"]
[ext_resource type="Script" uid="uid://dedg615xudpoq" path="res://entities/interactables/item_object/script/item_object.gd" id="1_hxea8"]

View File

@ -1,3 +1,4 @@
@tool
extends Interactable
class_name ItemObject
@ -21,13 +22,15 @@ func _ready():
generate_collision(10)
func interact(player : Player) -> bool:
if player.inventory.lenght() < player.inventory.max_items:
player.inventory.add_item(item)
pickup_animation(player)
var swapped_item = player.inventory.get_item()
player.get_item(item)
if swapped_item:
item = swapped_item
else :
var swaped_item = player.inventory.swap_items(item)
item = swaped_item
pickup_animation(player)
return true
func pickup_animation(player : Player):

View File

@ -1,4 +1,4 @@
extends Node2D
extends Area2D
class_name Plant
const PLANT_AREA_WIDTH = 10
@ -6,61 +6,66 @@ const PLANT_SPRITE_SCALE = 0.15
enum State {PLANTED, GROWING, MATURE}
var plant_type : PlantType
var planet : Planet
@export var plant_type : PlantType
@export var planet : Planet
var state : State = State.PLANTED : set = change_state
var day : int = 0
@onready var plant_sprite : Sprite2D = generate_sprite()
@onready var plant_area : Area2D = generate_area()
@onready var collision_shape : CollisionShape2D = generate_collision_shape()
func _init(_plant_type, _planet):
plant_type = _plant_type
planet = _planet
func _init(_plant_type = null, _planet = null):
plant_type = _plant_type
planet = _planet
func generate_sprite() -> Sprite2D:
var sprite = Sprite2D.new()
var sprite = Sprite2D.new()
add_child(sprite)
sprite.texture = get_state_texture(state)
sprite.scale = Vector2.ONE * PLANT_SPRITE_SCALE
sprite.offset
add_child(sprite)
sprite.texture = get_state_texture(state)
sprite.scale = Vector2.ONE * PLANT_SPRITE_SCALE
sprite.offset
return sprite
return sprite
func generate_area() -> Area2D:
var area = Area2D.new()
var collision = CollisionShape2D.new()
var collision_shape = CircleShape2D.new()
collision_shape.radius = PLANT_AREA_WIDTH
func generate_collision_shape() -> CollisionShape2D:
var collision = CollisionShape2D.new()
var shape = CircleShape2D.new()
shape.radius = PLANT_AREA_WIDTH
collision.shape = collision_shape
area.add_child(collision)
add_child(area)
collision.shape = shape
add_child(collision)
return area
return collision
func pass_day():
day += 1
if day > plant_type.growing_time:
change_state(State.MATURE)
else:
change_state(State.GROWING)
day += 1
if day > plant_type.growing_time:
change_state(State.MATURE)
else:
change_state(State.GROWING)
func change_state(_state : State):
state = _state
plant_sprite.texture = get_state_texture(state)
state = _state
plant_sprite.texture = get_state_texture(state)
if state == State.MATURE and plant_type.mature_effect:
plant_type.mature_effect.effect(self)
if state == State.MATURE and plant_type.mature_effect:
plant_type.mature_effect.effect(self)
func get_state_texture(s : State) -> Texture2D:
match s:
State.PLANTED:
return plant_type.planted_texture
State.GROWING:
return plant_type.growing_texture
State.MATURE:
return plant_type.mature_texture
return null
match s:
State.PLANTED:
return plant_type.planted_texture
State.GROWING:
return plant_type.growing_texture
State.MATURE:
return plant_type.mature_texture
return null
func harvest():
day += 1
if day > plant_type.growing_time:
change_state(State.MATURE)
else:
change_state(State.GROWING)

View File

@ -55,16 +55,16 @@ stream_0/stream = ExtResource("15_qpopc")
script = ExtResource("1_abrql")
[node name="Sprite" type="Sprite2D" parent="."]
position = Vector2(2, -55)
position = Vector2(2, -46)
scale = Vector2(0.084375, 0.084375)
texture = ExtResource("1_symyc")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(-2, -20)
position = Vector2(-2, -18)
shape = SubResource("CircleShape2D_sglur")
[node name="InteractArea2D" type="Area2D" parent="."]
position = Vector2(0, -14)
position = Vector2(0, -12)
[node name="CollisionShape2D" type="CollisionShape2D" parent="InteractArea2D"]
shape = SubResource("CircleShape2D_abrql")

View File

@ -0,0 +1,44 @@
extends Area2D
class_name ActionArea
const OPACITY = 0.3
const ACTIVATED_COLOR = Color.TURQUOISE
const DEACTIVATED_COLOR = Color.REBECCA_PURPLE
var collision_shape : CollisionShape2D = null
var area_width : float = 40
var area_distance : float = 80
var activated : bool = false :
set(v):
var old = activated
activated = v
if old != activated:
queue_redraw()
func _init(
_area_width : float = 40,
_area_distance : float = 80
):
area_width = _area_width
area_distance = _area_distance
func _ready():
collision_shape = CollisionShape2D.new()
collision_shape.position.x = area_distance
collision_shape.shape = CircleShape2D.new()
collision_shape.shape.radius = area_width
add_child(collision_shape)
func _process(_delta):
look_at(get_global_mouse_position())
func _draw():
draw_circle(
Vector2(
area_distance,
0
),
area_width,
Color((ACTIVATED_COLOR if activated else DEACTIVATED_COLOR), OPACITY)
)

View File

@ -0,0 +1 @@
uid://btmwjrscevcb1

View File

@ -31,6 +31,7 @@ var energy : int = max_energy :
set(v):
energy = v
emit_signal("player_updated", self)
var action_area : ActionArea = null
func _ready():
emit_signal("player_updated", self)
@ -44,14 +45,15 @@ func get_input():
var old_velocity=velocity
calculate_direction()
can_use_item = inventory.lenght() != 0 and inventory.get_item().can_use(self)
can_use_item = inventory.get_item() and inventory.get_item().can_use(self)
if action_area:
action_area.activated = can_use_item
if Input.is_action_just_pressed("action"):
use_item()
try_use_item()
if Input.is_action_just_pressed("interact") and closest_interactable:
closest_interactable.interact(self)
if Input.is_action_just_pressed("drop") and inventory.lenght() > 0:
var item_to_drop = inventory.pop_item()
planet.drop_item(item_to_drop, global_position)
if Input.is_action_just_pressed("drop") and inventory.get_item():
drop_item()
if old_velocity.length()==0 and velocity.length()!=0:
$Audio/AudioStreamPlayer_movement.play()
@ -65,13 +67,29 @@ func try_use_item():
if energy > 0 and can_use_item:
use_item()
func get_item(item : Item):
remove_action_area()
inventory.set_item(item)
if item is ToolItem:
add_action_area(item.generate_action_area())
func drop_item():
var item_to_drop = inventory.pop_item()
planet.drop_item(item_to_drop, global_position)
remove_action_area()
func delete_item():
inventory.set_item(null)
remove_action_area()
func use_item():
var item = inventory.get_item()
var is_item_used = item.use(self)
if is_item_used:
energy -= 1
if item.is_one_time_use():
inventory.pop_item()
delete_item()
func pass_day():
energy = max_energy
@ -92,6 +110,14 @@ func detect_closest_interactable():
else :
closest_interactable = null
func add_action_area(area : ActionArea):
action_area = area
add_child(action_area)
func remove_action_area():
if (action_area):
remove_child(action_area)
func _process(_delta):
get_input()
move_and_slide()

View File

@ -274,31 +274,26 @@ theme = ExtResource("2_nq5i2")
visible = false
layout_mode = 2
text = "space/click - Plant Seed"
label_settings = ExtResource("4_ujg5r")
[node name="GetItem" type="Label" parent="MarginContainer/AvailableActions"]
visible = false
layout_mode = 2
text = "E - Take Item"
label_settings = ExtResource("4_ujg5r")
[node name="SwapItem" type="Label" parent="MarginContainer/AvailableActions"]
visible = false
layout_mode = 2
text = "E - Swap Item"
label_settings = ExtResource("4_ujg5r")
[node name="DropItem" type="Label" parent="MarginContainer/AvailableActions"]
visible = false
layout_mode = 2
text = "w - Drop Item"
label_settings = ExtResource("4_ujg5r")
[node name="UseItem" type="Label" parent="MarginContainer/AvailableActions"]
visible = false
layout_mode = 2
text = "space/click - Use Item"
label_settings = ExtResource("4_ujg5r")
[node name="RechargeFade" type="ColorRect" parent="."]
physics_interpolation_mode = 0

View File

@ -9,15 +9,15 @@ signal day_pass_finished
func _on_player_updated(player:Player):
%EnergyCount.text = str(player.energy)
%AvailableActions/GetItem.visible = player.closest_interactable is ItemObject and player.inventory.lenght() == 0
%AvailableActions/SwapItem.visible = player.closest_interactable is ItemObject and player.inventory.lenght() > 0
%AvailableActions/DropItem.visible = player.inventory.lenght() > 0
%AvailableActions/UseItem.visible = player.inventory.lenght() > 0 and player.can_use_item and not player.inventory.get_item() is SeedItem
%AvailableActions/Plant.visible = player.inventory.lenght() > 0 and player.can_use_item and player.inventory.get_item() is SeedItem
%AvailableActions/GetItem.visible = player.closest_interactable is ItemObject and player.inventory.get_item() == null
%AvailableActions/SwapItem.visible = player.closest_interactable is ItemObject and player.inventory.get_item() != null
%AvailableActions/DropItem.visible = player.inventory.get_item() != null
%AvailableActions/UseItem.visible = player.inventory.get_item() and player.can_use_item and not player.inventory.get_item() is Seed
%AvailableActions/Plant.visible = player.inventory.get_item() and player.can_use_item and player.inventory.get_item() is Seed
%ItemInfo.visible = player.inventory.lenght() > 0
if player.inventory.lenght() > 0:
%ItemInfo.visible = player.inventory.get_item() != null
if player.inventory.get_item():
var item : Item = player.inventory.get_item()
%ItemIcon.texture = item.icon
%ItemName.text = item.name

View File

@ -1,22 +1,43 @@
[gd_scene load_steps=13 format=3 uid="uid://c5bruelvqbm1k"]
[gd_scene load_steps=18 format=3 uid="uid://c5bruelvqbm1k"]
[ext_resource type="PackedScene" uid="uid://12nak7amd1uq" path="res://gui/root_gui.tscn" id="1_jnlp7"]
[ext_resource type="PackedScene" uid="uid://tsi5j1uxppa4" path="res://stages/terrain/planet/planet.tscn" id="1_pyidc"]
[ext_resource type="PackedScene" uid="uid://bgvbgeq46wee2" path="res://entities/player/player.tscn" id="2_vvh5c"]
[ext_resource type="Script" uid="uid://dedg615xudpoq" path="res://entities/interactables/item_object/script/item_object.gd" id="3_bf3um"]
[ext_resource type="Resource" uid="uid://b04vho33bl52b" path="res://entities/plants/resources/plants/default.tres" id="3_jnlp7"]
[ext_resource type="PackedScene" uid="uid://dj7gp3crtg2yt" path="res://entities/camera/camera.tscn" id="3_vvh5c"]
[ext_resource type="PackedScene" uid="uid://cd3re3552pt7m" path="res://entities/interactables/item_object/item_object.tscn" id="4_vyht1"]
[ext_resource type="Resource" uid="uid://bb8etgye1qtfx" path="res://common/inventory/resources/items/shovel.tres" id="5_bf3um"]
[ext_resource type="Resource" uid="uid://dbja8xm7ehw1v" path="res://common/inventory/resources/items/water_can.tres" id="6_bf3um"]
[ext_resource type="Script" uid="uid://bypjcvlc15gsm" path="res://common/inventory/scripts/items/seed_item.gd" id="6_huihk"]
[ext_resource type="Texture2D" uid="uid://bf6nw4onkhavr" path="res://common/inventory/assets/icons/shovel.svg" id="4_huihk"]
[ext_resource type="Script" uid="uid://dya38x1h1uiyg" path="res://common/inventory/scripts/items/shovel.gd" id="5_qw60f"]
[ext_resource type="Script" uid="uid://jom8ulyopso" path="res://common/inventory/scripts/tool_item.gd" id="5_tw3kd"]
[ext_resource type="Texture2D" uid="uid://bo3o2qf3i20ke" path="res://common/inventory/assets/icons/scuba-diving-tank.svg" id="6_gd4vy"]
[ext_resource type="Texture2D" uid="uid://pltmnkqd5ut2" path="res://entities/plants/assets/sprites/seeds/grille_seeds.png" id="6_tw3kd"]
[ext_resource type="Script" uid="uid://bypjcvlc15gsm" path="res://common/inventory/scripts/items/seed.gd" id="9_gd4vy"]
[ext_resource type="Script" uid="uid://cega715smavh3" path="res://entities/plants/scripts/plant.gd" id="11_eji0w"]
[sub_resource type="Resource" id="Resource_qt76e"]
script = ExtResource("5_qw60f")
area_width = 50.0
area_distance = 50.0
name = "Shovel"
description = ""
icon = ExtResource("4_huihk")
metadata/_custom_type_script = "uid://dya38x1h1uiyg"
[sub_resource type="Resource" id="Resource_eji0w"]
script = ExtResource("5_tw3kd")
area_width = 100.0
area_distance = 90.0
name = "Arrosoir"
description = ""
icon = ExtResource("6_gd4vy")
metadata/_custom_type_script = "uid://jom8ulyopso"
[sub_resource type="AtlasTexture" id="AtlasTexture_ffarr"]
atlas = ExtResource("6_tw3kd")
region = Rect2(1140, 345, 141, 128)
[sub_resource type="Resource" id="Resource_gd4vy"]
script = ExtResource("6_huihk")
[sub_resource type="Resource" id="Resource_qwhpj"]
script = ExtResource("9_gd4vy")
plant_type = ExtResource("3_jnlp7")
name = "Chardi"
description = "This plant can grow without water, and reduce contamination around when it becomes mature."
@ -34,17 +55,30 @@ y_sort_enabled = true
[node name="Player" parent="Entities" instance=ExtResource("2_vvh5c")]
[node name="Shovel" parent="Entities" instance=ExtResource("4_vyht1")]
position = Vector2(172, -31)
item = ExtResource("5_bf3um")
[node name="ItemObject" type="Area2D" parent="Entities"]
position = Vector2(306, 0)
script = ExtResource("3_bf3um")
item = SubResource("Resource_qt76e")
metadata/_custom_type_script = "uid://dedg615xudpoq"
[node name="Seed" parent="Entities" instance=ExtResource("4_vyht1")]
position = Vector2(24, -189)
item = SubResource("Resource_gd4vy")
[node name="ItemObject2" type="Area2D" parent="Entities"]
position = Vector2(-23, -242)
script = ExtResource("3_bf3um")
item = SubResource("Resource_eji0w")
metadata/_custom_type_script = "uid://dedg615xudpoq"
[node name="WaterCan" parent="Entities" instance=ExtResource("4_vyht1")]
position = Vector2(-250, -116)
item = ExtResource("6_bf3um")
[node name="ItemObject3" type="Area2D" parent="Entities"]
position = Vector2(228, -103)
script = ExtResource("3_bf3um")
item = SubResource("Resource_qwhpj")
metadata/_custom_type_script = "uid://dedg615xudpoq"
[node name="Plant" type="Area2D" parent="Entities" node_paths=PackedStringArray("planet")]
position = Vector2(-120, -38)
script = ExtResource("11_eji0w")
plant_type = ExtResource("3_jnlp7")
planet = NodePath("../../Planet")
metadata/_custom_type_script = "uid://cega715smavh3"
[node name="Planet" parent="." node_paths=PackedStringArray("import_entities_from_node") instance=ExtResource("1_pyidc")]
import_entities_from_node = NodePath("../Entities")