102 lines
3.1 KiB
GDScript
102 lines
3.1 KiB
GDScript
class_name PlayerMovement
|
|
extends RefCounted
|
|
|
|
@export var _move_speed: float = 8
|
|
@export var _move_acceleration: float = 100
|
|
@export var _move_deceleration: float = 50
|
|
|
|
@export var _fall_speed: float = 20
|
|
@export var _fall_acceleration: float = 25
|
|
|
|
var move_input: Vector2
|
|
|
|
var _move_direction: Vector3
|
|
|
|
var _move_left: float
|
|
var _move_right: float
|
|
var _move_up: float
|
|
var _move_down: float
|
|
|
|
|
|
func handle_input(event: InputEvent, mode: Inputer.Mode) -> void:
|
|
if Referencer.players_count == 1:
|
|
return
|
|
|
|
if mode == Inputer.Mode.KB_MOUSE and event is InputEventKey:
|
|
var key_event := event as InputEventKey
|
|
if key_event.is_action_pressed("move_left"):
|
|
_move_left = 1
|
|
elif key_event.is_action_released("move_left"):
|
|
_move_left = 0
|
|
if key_event.is_action_pressed("move_right"):
|
|
_move_right = 1
|
|
elif key_event.is_action_released("move_right"):
|
|
_move_right = 0
|
|
if key_event.is_action_pressed("move_up"):
|
|
_move_up = 1
|
|
elif key_event.is_action_released("move_up"):
|
|
_move_up = 0
|
|
if key_event.is_action_pressed("move_down"):
|
|
_move_down = 1
|
|
elif key_event.is_action_released("move_down"):
|
|
_move_down = 0
|
|
|
|
if mode == Inputer.Mode.CONTROLLER and event is InputEventJoypadMotion:
|
|
var motion_event := event as InputEventJoypadMotion
|
|
if motion_event.is_action("move_left"):
|
|
_move_left = motion_event.get_action_strength("move_left")
|
|
if motion_event.is_action("move_right"):
|
|
_move_right = motion_event.get_action_strength("move_right")
|
|
if motion_event.is_action("move_up"):
|
|
_move_up = motion_event.get_action_strength("move_up")
|
|
if motion_event.is_action("move_down"):
|
|
_move_down = motion_event.get_action_strength("move_down")
|
|
|
|
|
|
func movement(
|
|
velocity: Vector3, delta: float, is_on_floor: bool, can_move: bool
|
|
) -> Vector3:
|
|
velocity = _lateral_movement(velocity, delta, can_move)
|
|
velocity = _vertical_movement(velocity, delta, is_on_floor)
|
|
|
|
return velocity
|
|
|
|
|
|
func _lateral_movement(velocity: Vector3, delta: float, can_move: bool) -> Vector3:
|
|
if Referencer.players_count == 1:
|
|
move_input = Input.get_vector("move_left", "move_right", "move_up", "move_down")
|
|
else:
|
|
move_input = Inputer.get_vector_from_raw_strengths(
|
|
_move_left,
|
|
_move_right,
|
|
_move_up,
|
|
_move_down,
|
|
Settings.movement_stick_deadzone
|
|
)
|
|
Debugger.text("move_input" + str(get_instance_id()), move_input)
|
|
|
|
if move_input.length() > 0 and can_move:
|
|
_move_direction = Vector3(move_input.x, 0, move_input.y).normalized().rotated(
|
|
Vector3.UP, Referencer.main_camera.rotation.y
|
|
)
|
|
var new_velocity := _move_direction * _move_speed
|
|
new_velocity.y = velocity.y
|
|
velocity = velocity.move_toward(new_velocity, _move_acceleration * delta)
|
|
else:
|
|
var new_velocity := Vector3.ZERO
|
|
new_velocity.y = velocity.y
|
|
velocity = velocity.move_toward(new_velocity, _move_deceleration * delta)
|
|
|
|
return velocity
|
|
|
|
|
|
func _vertical_movement(velocity: Vector3, delta: float, is_on_floor: bool) -> Vector3:
|
|
if not is_on_floor:
|
|
var new_velocity := velocity
|
|
new_velocity.y = -_fall_speed
|
|
velocity = velocity.move_toward(new_velocity, _fall_acceleration * delta)
|
|
else:
|
|
velocity.y = 0
|
|
|
|
return velocity
|