#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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user