* changements des objectifs, donnent juste des graines, sprite moins gros et objectifs plus nombreux * changement de la probabilité de mutation * refactor du code terrain et planet
302 lines
7.7 KiB
GDScript
302 lines
7.7 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
const MAX_REACH = 100
|
|
const HOLDING_ITEM_SPRITE_SIZE = 20.
|
|
const DEFAULT_INVENTORY_SIZE = 2
|
|
|
|
signal player_updated(player: Player)
|
|
signal upgraded
|
|
|
|
var terrain : Terrain
|
|
var planet : Planet :
|
|
get(): return terrain if terrain is Planet else null
|
|
@export var speed = 350
|
|
|
|
var max_energy : int = 3
|
|
var has_just_received_instruction : bool = false # pour récupérer les zones dans les action_area, une frame doit être passée depuis la création de la zone
|
|
|
|
var controlling_player : bool = true :
|
|
set(v):
|
|
controlling_player = v
|
|
velocity = Vector2.ZERO
|
|
|
|
var instruction : Instruction = null
|
|
|
|
var energy : int = max_energy :
|
|
set(v):
|
|
energy = v
|
|
player_updated.emit(self)
|
|
|
|
@onready var inventory : Inventory = Inventory.new(DEFAULT_INVENTORY_SIZE)
|
|
@onready var preview_zone : ActionZone = null
|
|
@onready var action_zone : ActionZone = null
|
|
|
|
func _ready():
|
|
player_updated.emit(self)
|
|
inventory.inventory_changed.connect(_on_inventory_updated)
|
|
Pointer.player = self
|
|
|
|
func _input(_event) -> void:
|
|
if Input.is_action_pressed("change_item_left"):
|
|
inventory.change_current_item(1)
|
|
if Input.is_action_pressed("change_item_right"):
|
|
inventory.change_current_item(-1)
|
|
for i in range(1, 10):
|
|
if Input.is_action_pressed("item_" + str(i)):
|
|
inventory.set_current_item(i - 1)
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _start_pass_day():
|
|
controlling_player = false
|
|
instruction = null
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _pass_day():
|
|
full_recharge()
|
|
|
|
# Méthode déclenchée par la classe planet
|
|
func _end_pass_day():
|
|
controlling_player = true
|
|
|
|
func _process(_delta):
|
|
if controlling_player:
|
|
var old_velocity=velocity
|
|
calculate_direction()
|
|
|
|
if instruction and instruction.can_be_done(self) and not has_just_received_instruction:
|
|
instruction.do(self)
|
|
instruction = null
|
|
move_preview_zone(get_global_mouse_position())
|
|
|
|
has_just_received_instruction = false
|
|
|
|
# Sound
|
|
if old_velocity.length()==0 and velocity.length()!=0:
|
|
play_sfx("move")
|
|
else:
|
|
velocity = Vector2.ZERO
|
|
move_and_slide()
|
|
|
|
func _on_inventory_updated(_inventory: Inventory):
|
|
var item : Item = inventory.get_item()
|
|
if item:
|
|
setup_preview_zone(item.usage_zone_radius)
|
|
var item_texture = item.icon
|
|
%ItemSprite.texture = item_texture
|
|
%ItemSprite.scale = Vector2(
|
|
1./(item_texture.get_width()/HOLDING_ITEM_SPRITE_SIZE),
|
|
1./(item_texture.get_height()/HOLDING_ITEM_SPRITE_SIZE)
|
|
)
|
|
%HideEyes.visible = item != null
|
|
%ItemSprite.visible = item != null
|
|
emit_signal("player_updated", self)
|
|
|
|
|
|
func calculate_direction():
|
|
var input_direction: Vector2 = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
|
|
if input_direction.length() != 0:
|
|
instruction = null
|
|
|
|
if instruction:
|
|
input_direction = self.global_position.direction_to(instruction.position)
|
|
|
|
velocity = input_direction * speed
|
|
if input_direction.x:
|
|
flip_character(input_direction.x > 0)
|
|
|
|
|
|
func flip_character(face_right = true):
|
|
$Sprite.flip_h = not face_right
|
|
%ItemSprite.position.x = abs(%ItemSprite.position.x) * (1 if face_right else -1)
|
|
%HideEyes.position.x = abs(%ItemSprite.position.x) * (1 if face_right else -1)
|
|
|
|
func can_interact(interactable : Interactable):
|
|
return interactable.can_interact(self)
|
|
|
|
func try_interact(interactable : Interactable):
|
|
if interactable:
|
|
has_just_received_instruction = true
|
|
instruction = InteractableInstruction.new(
|
|
interactable
|
|
)
|
|
|
|
func try_move(move_to : Vector2):
|
|
instruction = MoveInstruction.new(move_to)
|
|
|
|
func pick_item(item : Item) -> Item:
|
|
play_sfx("pick")
|
|
if inventory.length() >= inventory.size:
|
|
var current_item : Item = inventory.get_item()
|
|
inventory.set_item(item, inventory.current_item_ind)
|
|
return current_item
|
|
else :
|
|
inventory.add_item(item)
|
|
return null
|
|
|
|
func drop_item():
|
|
var item_to_drop = inventory.pop_item()
|
|
if item_to_drop:
|
|
terrain.drop_item(item_to_drop, global_position)
|
|
play_sfx("drop")
|
|
|
|
func delete_item(item: Item):
|
|
inventory.remove_item(item)
|
|
|
|
func try_use_item(item : Item, use_position : Vector2):
|
|
has_just_received_instruction = true
|
|
setup_action_zone(use_position, item.usage_zone_radius)
|
|
instruction = ItemActionInstruction.new(
|
|
use_position,
|
|
item
|
|
)
|
|
|
|
func preview_can_use_item(item : Item) -> bool:
|
|
return can_use_item_on_zone(item, preview_zone)
|
|
|
|
func can_use_item_on_zone(item : Item, zone: ActionZone) -> bool:
|
|
return (
|
|
inventory.has_item(item)
|
|
and (energy - item.energy_usage) >= 0
|
|
and item.can_use(self, zone)
|
|
)
|
|
|
|
func use_item(item : Item):
|
|
if can_use_item_on_zone(item, action_zone):
|
|
var is_item_used = item.use(self, action_zone)
|
|
if is_item_used:
|
|
energy -= item.energy_usage
|
|
if item.is_one_time_use():
|
|
inventory.remove_current_item()
|
|
|
|
func upgrade_max_energy(amount = 1):
|
|
max_energy += amount
|
|
upgraded.emit()
|
|
player_updated.emit(self)
|
|
|
|
func upgrade_inventory_size(amount = 1):
|
|
inventory.size += amount
|
|
upgraded.emit()
|
|
player_updated.emit(self)
|
|
|
|
func recharge(amount : int = max_energy):
|
|
energy = energy + amount
|
|
upgraded.emit()
|
|
|
|
func full_recharge():
|
|
energy = max(energy, max_energy)
|
|
|
|
func generate_action_zone(radius : int = 0) -> ActionZone:
|
|
var zone = ActionZone.new(radius)
|
|
|
|
get_parent().add_child(zone.area)
|
|
|
|
return zone
|
|
|
|
func setup_preview_zone(zone_radius : int) -> ActionZone:
|
|
if preview_zone:
|
|
preview_zone.destroy()
|
|
preview_zone = generate_action_zone(zone_radius)
|
|
return preview_zone
|
|
|
|
func setup_action_zone(zone_position : Vector2, zone_radius : int) -> ActionZone:
|
|
if action_zone:
|
|
action_zone.destroy()
|
|
action_zone = generate_action_zone(zone_radius)
|
|
action_zone.area.global_position = zone_position
|
|
return action_zone
|
|
|
|
func move_preview_zone(zone_position : Vector2):
|
|
if preview_zone:
|
|
preview_zone.move_to_position(zone_position)
|
|
|
|
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()
|
|
|
|
class Instruction:
|
|
|
|
var position : Vector2
|
|
|
|
func _init(_pos : Vector2):
|
|
position = _pos
|
|
|
|
func can_be_done(player : Player):
|
|
return player.global_position.distance_to(position) < 10
|
|
|
|
func do(_player : Player):
|
|
pass
|
|
|
|
class MoveInstruction extends Instruction:
|
|
pass
|
|
|
|
class ItemActionInstruction extends Instruction:
|
|
var item = Item
|
|
|
|
func _init(_pos : Vector2, _item : Item):
|
|
position = _pos
|
|
item = _item
|
|
|
|
func can_be_done(player : Player):
|
|
return player.global_position.distance_to(position) < player.MAX_REACH
|
|
|
|
func do(player : Player):
|
|
player.use_item(item)
|
|
|
|
class InteractableInstruction extends Instruction:
|
|
var interactable = Interactable
|
|
|
|
func _init(_interactable : Interactable):
|
|
interactable = _interactable
|
|
position = interactable.global_position
|
|
|
|
func can_be_done(player : Player):
|
|
return player.global_position.distance_to(position) < player.MAX_REACH
|
|
|
|
func do(player : Player):
|
|
interactable.interact(player)
|
|
|
|
class ActionZone:
|
|
var radius : int = 10
|
|
var area : Area2D
|
|
|
|
func _init(_r : int):
|
|
radius = _r
|
|
area = Area2D.new()
|
|
var collision_shape = CollisionShape2D.new()
|
|
var circle_shape = CircleShape2D.new()
|
|
|
|
circle_shape.radius = radius
|
|
collision_shape.shape = circle_shape
|
|
area.add_child(collision_shape)
|
|
|
|
func destroy():
|
|
area.queue_free()
|
|
|
|
func get_global_position() -> Vector2:
|
|
return Vector2.ZERO if area == null else area.global_position
|
|
|
|
func move_to_position(pos : Vector2):
|
|
if area:
|
|
area.global_position = pos
|
|
|
|
func get_points_in_zone(point_factor = 10) -> Array[Vector2]:
|
|
var points : Array[Vector2] = []
|
|
for x in range(-radius, radius, point_factor):
|
|
for y in range(-radius, radius, point_factor):
|
|
if Vector2(x, y).length() <= radius:
|
|
points.append(area.global_position + Vector2(x, y))
|
|
|
|
return points
|
|
|