extends PlayerComponent class_name InputManagementComponent #instead of setting up keybinds in 3 differents scripts, it's centralized in this one only # #point d'entree unique de l'input du joueur : # - declare les noms des actions (exports ci-dessous) : c'est la seule definition, ni PlayerCharacter ni CameraHolder n'en gardent de copie # (le CameraHolder lit la camera par read_look_vector / read_zoom_axis) # - fournit les lectures utilisees par les etats (read_move_dir, wants_jump...) : plus aucun etat n'appelle Input directement # sauf pour les actions portees par un Hint (cancel, unlock, switch target, mount, dismount), lues par leur proprietaire @export_group("Keybind variables") @export_subgroup("Player character variables") @export var move_forward_action : StringName = "player_move_forward" @export var move_backward_action : StringName = "player_move_backward" @export var move_left_action : StringName = "player_move_left" @export var move_right_action : StringName = "player_move_right" @export var run_action : StringName = "player_run" @export var jump_action : StringName = "player_jump" @export var dodge_action : StringName = "player_dodge" @export var crouch_action : StringName = "player_crouch" @export var throw_action : StringName = &"player_throw" #RT / clic gauche : maintenir pour charger, relacher pour lancer @export_subgroup("Camera variables") @export var aim_cam_action : StringName = "player_aim_camera" @export var cam_zoom_in_action : StringName = "player_zoom_camera_in" @export var cam_zoom_out_action : StringName = "player_zoom_camera_out" @export var cam_look_up_action : StringName = "player_look_up" @export var cam_look_down_action : StringName = "player_look_down" @export var cam_look_left_action : StringName = "player_look_left" @export var cam_look_right_action : StringName = "player_look_right" func register_with_player() -> void: player.player_input = self # Called when the node enters the scene tree for the first time. func _ready() -> void: player.cam_holder.player_input = self #le CameraHolder lit la camera ici, il ne connait aucun nom d'action InputRegistry.ensure(throw_action, [InputRegistry.pad_axis(JOY_AXIS_TRIGGER_RIGHT, 1.0), InputRegistry.mouse(MOUSE_BUTTON_LEFT)]) #---------- camera ---------- func read_look_vector() -> Vector2: #rotation de la camera a la manette / aux touches (la souris est geree par le CameraHolder lui-meme) return Input.get_vector(cam_look_left_action, cam_look_right_action, cam_look_up_action, cam_look_down_action) func read_zoom_axis() -> float: return Input.get_axis(cam_zoom_in_action, cam_zoom_out_action) #---------- deplacement ---------- func read_move_dir() -> Vector2: #direction voulue, relative a la camera return read_move_raw().rotated(-player.cam_holder.global_rotation.y) func read_move_raw() -> Vector2: #direction brute du stick / des touches, sans tenir compte de la camera return Input.get_vector(move_left_action, move_right_action, move_forward_action, move_backward_action) #---------- input consomme ---------- #un appui consomme est ignore par tous les getters ci-dessous jusqu'a ce que le bouton soit relache. #sert quand un meme bouton a deux roles (ex : A monte sur un pokemon ET fait sauter) : celui qui agit en premier consomme l'appui #(avant : MountRider bloquait des etats pendant une frame avec un await). Les composants qui consomment doivent s'executer #avant la StateMachine (process_physics_priority plus petit, comme MountRider). var _consumed : Dictionary = {} #action -> true func consume(action : StringName) -> void: if Input.is_action_pressed(action): _consumed[action] = true #rien a consommer si le bouton n'est pas enfonce func is_consumed(action : StringName) -> bool: if !_consumed.has(action): return false if Input.is_action_pressed(action): return true _consumed.erase(action) #bouton relache : l'action redevient disponible return false func _just_pressed(action : StringName) -> bool: return !is_consumed(action) and Input.is_action_just_pressed(action) func _held(action : StringName) -> bool: return !is_consumed(action) and Input.is_action_pressed(action) #---------- actions au sol ---------- func wants_dodge() -> bool: return _just_pressed(dodge_action) and player.dodge_cooldown <= 0.0 func wants_jump() -> bool: #depart d'un saut depuis le sol : tient compte de auto_jump (rester appuye) if player.auto_jump: return _held(jump_action) return _just_pressed(jump_action) func jump_pressed() -> bool: return _just_pressed(jump_action) func jump_released() -> bool: return Input.is_action_just_released(jump_action) func crouch_pressed() -> bool: return _just_pressed(crouch_action) func run_pressed() -> bool: return _held(run_action) func run_just_pressed() -> bool: return _just_pressed(run_action) #---------- visee et lancer ---------- func aim_pressed() -> bool: return _just_pressed(aim_cam_action) func aim_held() -> bool: return _held(aim_cam_action) func throw_pressed() -> bool: return _just_pressed(throw_action) func throw_held() -> bool: return _held(throw_action)