98 lines
2.5 KiB
GDScript
98 lines
2.5 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
signal player_updated(player: Player)
|
|
|
|
var planet : Planet # mis à jour par la classe Planet
|
|
@export var speed = 400
|
|
|
|
@export var testPlantType : PlantType
|
|
|
|
@onready var inventory : Inventory = Inventory.new()
|
|
|
|
var max_energy : int = 10
|
|
|
|
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 energy : int = max_energy :
|
|
set(v):
|
|
energy = v
|
|
emit_signal("player_updated", self)
|
|
|
|
func _ready():
|
|
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():
|
|
if controlling_player:
|
|
var old_velocity=velocity
|
|
calculate_direction()
|
|
|
|
if Input.is_action_just_pressed("action") and energy > 0:
|
|
action()
|
|
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 old_velocity.length()==0 and velocity.length()!=0:
|
|
$Audio/AudioStreamPlayer_movement.play()
|
|
|
|
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 action():
|
|
if planet:
|
|
planet.plant(
|
|
testPlantType,
|
|
global_position
|
|
)
|
|
|
|
func pass_day():
|
|
energy = max_energy
|
|
|
|
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 _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():
|
|
action()
|