#44 changement de la méthode d'interaction et d'utilisation du joueur pour une plus grande liberté de la souris

This commit is contained in:
2025-08-21 18:02:50 +02:00
parent 3bbb1cfcdc
commit bd852b007c
24 changed files with 433 additions and 146 deletions

View File

@@ -12,6 +12,9 @@ func _process(_delta):
func inspected_text():
return "Compost"
func interact_text():
return "Put a seed"
func can_interact(p : Player) -> bool:
return p.inventory.get_item() and p.inventory.get_item() is Seed

View File

@@ -24,6 +24,9 @@ func _ready():
func inspected_text():
return item.name + (" Seed" if item is Seed else "")
func interact_text():
return "Take"
func interact(player : Player) -> bool:
var swapped_item = player.inventory.get_item()

View File

@@ -4,19 +4,22 @@ class_name Interactable
var available : bool = true
func can_interact(_p : Player) -> bool:
return true
return true
func interact_requirement_text() -> String:
return ""
return ""
func interact(_p : Player) -> bool:
printerr("Interact function called on abstract Interactable class")
return false
func interact(_p : Player) -> bool:
printerr("Interact function called on abstract Interactable class")
return false
func generate_collision(area_width : float):
var collision = CollisionShape2D.new()
var collision_shape = CircleShape2D.new()
collision_shape.radius = area_width
var collision = CollisionShape2D.new()
var collision_shape = CircleShape2D.new()
collision_shape.radius = area_width
collision.shape = collision_shape
add_child(collision)
collision.shape = collision_shape
add_child(collision)
func interact_text():
return ""

View File

@@ -2,7 +2,7 @@
[ext_resource type="Texture2D" uid="uid://c7mp7tkkkk6o5" path="res://entities/plants/assets/sprites/default/growing.png" id="1_fp5j6"]
[ext_resource type="Script" uid="uid://jnye5pe1bgqw" path="res://entities/plants/scripts/plant_type.gd" id="1_moyj3"]
[ext_resource type="Script" uid="uid://cgscbuxe4dawb" path="res://entities/plants/scripts/plant_effects/decontaminate_terrain_effect.gd" id="2_cky1j"]
[ext_resource type="Script" path="res://entities/plants/scripts/plant_effects/decontaminate_terrain_effect.gd" id="2_cky1j"]
[ext_resource type="Texture2D" uid="uid://bupl1y0cfj21q" path="res://entities/plants/assets/sprites/default/mature.png" id="3_ffarr"]
[ext_resource type="Texture2D" uid="uid://ba413oun7ry78" path="res://entities/plants/assets/sprites/default/planted.png" id="4_2s6re"]
[ext_resource type="Texture2D" uid="uid://pltmnkqd5ut2" path="res://entities/plants/assets/sprites/seeds/grille_seeds.png" id="6_cky1j"]

View File

@@ -1,44 +1,41 @@
extends Area2D
class_name ActionArea
const OPACITY = 0.3
const OPACITY = 0.4
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 area_width : float
var area_max_distance : float
var player : Player
var activated : bool = false :
set(v):
var old = activated
activated = v
if old != activated:
queue_redraw()
var activated : bool = false
func _init(
_player: Player,
_area_width : float = 40,
_area_distance : float = 80
):
player = _player
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())
var target_position = get_global_mouse_position() - player.global_position
if Vector2.ONE.distance_to(target_position) > player.max_reach:
target_position = Vector2.ZERO.direction_to(target_position)*player.max_reach
position = target_position
queue_redraw()
func _draw():
draw_circle(
Vector2(
area_distance,
0
),
Vector2.ZERO,
area_width,
Color((ACTIVATED_COLOR if activated else DEACTIVATED_COLOR), OPACITY)
)

View File

@@ -1,8 +1,11 @@
extends CharacterBody2D
class_name Player
const MAX_REACH_CIRCLE_OPACITY = 0.1
signal player_updated(player: Player)
signal action_tried_without_energy
signal action_not_permitted
signal upgraded
var planet : Planet # mis à jour par la classe Planet
@@ -11,48 +14,47 @@ var planet : Planet # mis à jour par la classe Planet
@onready var inventory : Inventory = Inventory.new()
var max_energy : int = 3
var max_reach : int = 100 :
set(v):
max_reach = v
queue_redraw()
var controlling_player : bool = true :
set(v):
controlling_player = v
velocity = Vector2.ZERO
var closest_interactable : Interactable = null :
set(v):
var old = closest_interactable
closest_interactable = v
if old != closest_interactable:
player_updated.emit(self)
var target_interactable : Interactable = null # mis à jour par la classe Interactable
var can_use_item : bool = false :
set(v):
var old = can_use_item
can_use_item = v
if old != can_use_item:
player_updated.emit(self)
var can_interact : bool = false :
set(v):
var old = can_interact
can_interact = v
if old != can_interact:
print("emit")
player_updated.emit(self)
var energy : int = max_energy :
set(v):
energy = v
emit_signal("player_updated", self)
var action_area : ActionArea = null
player_updated.emit(self)
var action_area : ActionArea = null :
set(v):
action_area = v
queue_redraw()
func _ready():
emit_signal("player_updated", self)
player_updated.emit(self)
inventory.inventory_changed.connect(_on_inventory_updated)
Pointer.player = self
# Méthode déclenchée par la classe planet
func _pass_day():
energy = max_energy
target_interactable = null
func _process(_delta):
get_input()
move_and_slide()
detect_closest_interactable()
func _on_root_gui_day_pass_pressed():
controlling_player = false
@@ -66,19 +68,33 @@ func _on_root_gui_game_click():
func _on_inventory_updated(_inventory: Inventory):
emit_signal("player_updated", self)
func _draw():
if action_area:
draw_arc(
Vector2.ZERO,
max_reach,
0.,
2*PI,
30,
Color(Color.WHITE,MAX_REACH_CIRCLE_OPACITY),
1
)
func get_input():
if controlling_player:
var old_velocity=velocity
calculate_direction()
can_use_item = inventory.get_item() and inventory.get_item().can_use(self)
can_interact = closest_interactable and closest_interactable.can_interact(self)
if action_area:
action_area.activated = can_use_item
if target_interactable and target_interactable in $InteractArea2D.get_overlapping_areas():
if target_interactable.can_interact(self):
Pointer.stop_inspect_entity(target_interactable)
target_interactable.interact(self)
target_interactable = null
if Input.is_action_just_pressed("action"):
try_use_item()
if Input.is_action_just_pressed("interact") and closest_interactable and can_interact:
closest_interactable.interact(self)
if Input.is_action_just_pressed("drop") and inventory.get_item():
drop_item()
if old_velocity.length()==0 and velocity.length()!=0:
@@ -86,21 +102,31 @@ func get_input():
func calculate_direction():
var input_direction: Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if input_direction.length() != 0:
target_interactable = null
if target_interactable:
input_direction = self.global_position.direction_to(target_interactable.global_position)
velocity = input_direction * speed
if input_direction.x:
$Sprite.flip_h = (input_direction.x < 0)
func try_use_item():
if energy == 0 and inventory.get_item():
if energy == 0:
action_tried_without_energy.emit()
elif not can_use_item:
action_not_permitted.emit()
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())
var new_action_area = item.generate_action_area(self)
if new_action_area != null :
add_action_area(new_action_area)
play_sfx("pick")
func drop_item():
@@ -121,22 +147,6 @@ func use_item():
if item.is_one_time_use():
delete_item()
func detect_closest_interactable():
var in_range_interactables : Array[Interactable] = []
for area in $InteractArea2D.get_overlapping_areas():
if area is Interactable and area.available:
in_range_interactables.append(area)
in_range_interactables.sort_custom(
func(a : Node2D, b : Node2D) :
return a.global_position.distance_to(global_position) < b.global_position.distance_to(global_position)
)
if len(in_range_interactables) > 0:
closest_interactable = in_range_interactables[0]
else :
closest_interactable = null
func add_action_area(area : ActionArea):
action_area = area
add_child(action_area)
@@ -144,6 +154,8 @@ func add_action_area(area : ActionArea):
func remove_action_area():
if (action_area):
remove_child(action_area)
action_area.queue_free()
action_area = null
func upgrade():
max_energy += 1

View File

@@ -4,15 +4,14 @@ class_name InspectableEntity
const MODULATE_INSPECTED_COLOR = Color.GRAY
@onready var default_modulate : Color = modulate
@onready var mouse_signals_setuped : bool = setup_mouse_signals()
@onready var inspectable_signals_setuped : bool = setup_inspectable_signals()
var inspected : bool = false :
set(v):
print(v)
inspected = v
modulate = MODULATE_INSPECTED_COLOR if inspected else default_modulate
func setup_mouse_signals() -> bool:
func setup_inspectable_signals() -> bool:
mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(_on_mouse_excited)
return true