61 lines
1.3 KiB
GDScript
61 lines
1.3 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
signal player_updated(player: Player)
|
|
|
|
var controlling_player : bool = true
|
|
var planet : Planet # mis à jour par la classe Planet
|
|
@export var speed = 400
|
|
|
|
@export var testPlantType : PlantType
|
|
|
|
var max_energy : int = 10
|
|
|
|
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:
|
|
calculate_direction()
|
|
|
|
if Input.is_action_just_pressed("action") and energy > 0:
|
|
action()
|
|
|
|
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 _physics_process(_delta):
|
|
get_input()
|
|
move_and_slide()
|
|
|
|
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()
|