163 lines
4.1 KiB
GDScript
163 lines
4.1 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
signal player_updated(player: Player)
|
|
signal action_tried_without_energy
|
|
signal upgraded
|
|
|
|
var planet : Planet # mis à jour par la classe Planet
|
|
@export var speed = 350
|
|
|
|
@onready var inventory : Inventory = Inventory.new()
|
|
|
|
var max_energy : int = 3
|
|
|
|
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 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:
|
|
player_updated.emit(self)
|
|
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)
|
|
inventory.inventory_changed.connect(_on_inventory_updated)
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _pass_day():
|
|
energy = max_energy
|
|
|
|
func _process(_delta):
|
|
get_input()
|
|
move_and_slide()
|
|
detect_closest_interactable()
|
|
|
|
func _on_root_gui_day_pass_pressed():
|
|
controlling_player = false
|
|
|
|
func _on_root_gui_day_pass_finished():
|
|
controlling_player = true
|
|
|
|
func _on_root_gui_game_click():
|
|
try_use_item()
|
|
|
|
func _on_inventory_updated(_inventory: Inventory):
|
|
emit_signal("player_updated", self)
|
|
|
|
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 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:
|
|
play_sfx("move")
|
|
|
|
func calculate_direction():
|
|
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_use_item():
|
|
if energy == 0 and inventory.get_item():
|
|
action_tried_without_energy.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())
|
|
play_sfx("pick")
|
|
|
|
func drop_item():
|
|
var item_to_drop = inventory.pop_item()
|
|
planet.drop_item(item_to_drop, global_position)
|
|
remove_action_area()
|
|
play_sfx("pick")
|
|
|
|
func delete_item():
|
|
inventory.set_item(null)
|
|
remove_action_area()
|
|
|
|
func use_item():
|
|
var item : Item = inventory.get_item()
|
|
var is_item_used = item.use(self)
|
|
if is_item_used:
|
|
energy -= 1
|
|
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)
|
|
|
|
func remove_action_area():
|
|
if (action_area):
|
|
remove_child(action_area)
|
|
|
|
func upgrade():
|
|
max_energy += 1
|
|
energy += 1
|
|
upgraded.emit()
|
|
|
|
func play_sfx(sound : String):
|
|
match sound:
|
|
"dig":
|
|
$Audio/AudioStreamPlayer_dig.play()
|
|
"harvest":
|
|
$Audio/AudioStreamPlayer_harvest.play()
|
|
"pick":
|
|
$Audio/AudioStreamPlayer_pick_up.play()
|
|
"move":
|
|
$Audio/AudioStreamPlayer_movement.play()
|