179 lines
4.1 KiB
GDScript
179 lines
4.1 KiB
GDScript
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
|
|
@export var speed = 350
|
|
|
|
@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 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:
|
|
print("emit")
|
|
player_updated.emit(self)
|
|
var energy : int = max_energy :
|
|
set(v):
|
|
energy = v
|
|
player_updated.emit(self)
|
|
var action_area : ActionArea = null :
|
|
set(v):
|
|
action_area = v
|
|
queue_redraw()
|
|
|
|
func _ready():
|
|
player_updated.emit(self)
|
|
inventory.inventory_changed.connect(_on_inventory_updated)
|
|
Pointer.player = self
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _start_pass_day():
|
|
controlling_player = false
|
|
target_interactable = null
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _pass_day():
|
|
recharge()
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _end_pass_day():
|
|
controlling_player = true
|
|
|
|
func _process(_delta):
|
|
get_input()
|
|
move_and_slide()
|
|
|
|
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)
|
|
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("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")
|
|
|
|
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:
|
|
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)
|
|
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():
|
|
var item_to_drop = inventory.pop_item()
|
|
planet.drop_item(item_to_drop, global_position)
|
|
remove_action_area()
|
|
play_sfx("drop")
|
|
|
|
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 add_action_area(area : ActionArea):
|
|
action_area = area
|
|
add_child(action_area)
|
|
|
|
func remove_action_area():
|
|
if (action_area):
|
|
remove_child(action_area)
|
|
action_area.queue_free()
|
|
action_area = null
|
|
|
|
func upgrade():
|
|
max_energy += 1
|
|
energy += 1
|
|
upgraded.emit()
|
|
|
|
func recharge(amount : int = max_energy):
|
|
energy = min(energy + amount, max_energy)
|
|
|
|
func play_sfx(sound : String):
|
|
match sound:
|
|
"dig":
|
|
$Audio/AudioStreamPlayer_dig.play()
|
|
"harvest":
|
|
$Audio/AudioStreamPlayer_harvest.play()
|
|
"pick":
|
|
$Audio/AudioStreamPlayer_pick_up.play()
|
|
"drop":
|
|
$Audio/AudioStreamPlayer_drop.play()
|
|
"move":
|
|
$Audio/AudioStreamPlayer_movement.play()
|