Files
chaussette.sale/caracters/player/player_controler.gd
Thomas 5e32ed4196 Un shader de transition entre les scènes
ça fait plus propre braw
2025-04-14 17:24:10 +02:00

114 lines
4.1 KiB
GDScript

extends Node2D
@export var human: Human
@export var ray : ShapeCast2D
@export var pathFinder : HumanPathFinder
@onready var world: World = human.get_parent();
# Stores the nearest Node2D we can interact with, then we can start interacting with it if the player
# press grab
var can_interact_with : Node2D
@onready var cam := $"../Camera2D" as Camera2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var limits:Rect2 = world.get_used_rect()
var top_left_global = world.to_global(world.map_to_local(limits.position))
var bottom_right_global = world.to_global(world.map_to_local(limits.end))
cam.limit_left = top_left_global.x + 48
cam.limit_top = top_left_global.y - 24
cam.limit_right = bottom_right_global.x + 48
cam.limit_bottom = bottom_right_global.y - 24
func _unhandled_input(event: InputEvent) -> void:
human.stop_interaction()
human.velocityVector = Vector2(0, 0)
human.wants_to_grab = false
human.wants_to_interact_with = null
human.velocityVector = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if human.velocityVector != Vector2(0, 0):
GameState.isUsingTouch = false
update_game_state()
if event.is_action_pressed("grab"):
if can_interact_with:
human.wants_to_interact_with = can_interact_with
else:
human.wants_to_grab = true
# moving using either touch or mouse button
if event.is_action_pressed("touch") or event is InputEventScreenTouch:
GameState.isUsingTouch = true
var tile_pos = world.local_to_map(world.to_local(get_global_mouse_position()))
if event.is_pressed():
pathFinder.destination = get_global_mouse_position()
func update_game_state():
GameState.update_position(human.get_feet_global_position())
func _process(delta) -> void:
ray.target_position = human.last_facing_direction * 48
if human.last_facing_direction.y > 0 :
ray.target_position = Vector2(0, 48)
(ray.shape as RectangleShape2D).size = Vector2(50, 20)
elif human.last_facing_direction.y < 0:
ray.target_position = Vector2(0, -48)
(ray.shape as RectangleShape2D).size = Vector2(50, 20)
elif human.last_facing_direction.x > 0 :
ray.target_position = Vector2(48, 0)
(ray.shape as RectangleShape2D).size = Vector2(20, 50)
else:
ray.target_position = Vector2(-48, 0)
(ray.shape as RectangleShape2D).size = Vector2(20, 50)
# find all the possible interactables
var possible_interactables = []
if ray.is_colliding():
var nbCollisions = ray.get_collision_count()
for n in range(nbCollisions):
var colider = ray.get_collider(n) as Node2D
if colider != null and colider != get_parent():
if (
colider and colider.get_parent().has_method("start_interaction") and
colider.get_parent().has_method("enable_interaction_clue") and
colider.get_parent().has_method("disable_interaction_clue")
):
possible_interactables.append(colider.get_parent())
# find the closest interactable
var distance_to_closest = Vector2(0, 0)
var closest: Node2D = null
for potential_closest in possible_interactables:
var distance_to_human = abs(potential_closest.global_position - human.global_position)
if distance_to_closest == Vector2(0, 0):
distance_to_closest = distance_to_human
closest = potential_closest
if distance_to_human < distance_to_closest:
closest = potential_closest
distance_to_closest = distance_to_human
# if the new closest is different from the one we had before, notify the one we had before that it shouldn't keep its bubble visible
if can_interact_with != null:
if can_interact_with != closest:
can_interact_with.get_children()
can_interact_with.disable_interaction_clue()
# don't retrigger enabling the clue if it was already enabled
if can_interact_with != closest:
can_interact_with = closest
if can_interact_with:
can_interact_with.enable_interaction_clue(human)
func _on_area_2d_body_entered(body: Node2D) -> void:
if body.has_method("do_trap") :
human.kill_path_finder = true
body.do_trap()
func _on_area_2d_body_exited(body: Node2D) -> void:
pass
func _on_character_body_2d_load_from_game_state() -> void:
human.set_feet_global_position(GameState.position_outside)