diff --git a/scripts/main_camera.gd b/scripts/main_camera.gd index 0492636..fac20ce 100644 --- a/scripts/main_camera.gd +++ b/scripts/main_camera.gd @@ -26,12 +26,12 @@ func _follow(delta: float) -> void: player_position.y = _floor_height if Inputer.mode == Inputer.Mode.KB_MOUSE: - _aim_offset = Referencer.player.aim_offset + _aim_offset = Referencer.player.aiming.aim_offset else: var new_aim_offset := ( Vector3.ZERO - if Referencer.player.aim_input.length() == 0 - else Referencer.player.aim_offset + if Referencer.player.aiming.aim_input.length() == 0 + else Referencer.player.aiming.aim_offset ) _aim_offset = _aim_offset.lerp(new_aim_offset, _aim_damping * delta) diff --git a/scripts/player/player.gd b/scripts/player/player.gd index 9467127..2a0c024 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -1,24 +1,12 @@ class_name Player extends CharacterBody3D -const MOVE_SPEED: float = 10 -const MOVE_ACCELERATION: float = 100 -const MOVE_DECELERATION: float = 50 - -const FALL_SPEED: float = 20 -const FALL_ACCELERATION: float = 25 - @export var _respawn_height: float = -5 -@export var _controller_aim_offset: float = 20 -@export var _vertical_aim_aspect: float = 1.5 -var aim_offset: Vector3 -var aim_input: Vector2 +var movement: PlayerMovement = PlayerMovement.new() +var aiming: PlayerAiming = PlayerAiming.new() var _respawn_point: Vector3 -var _floor_height: float -var _move_input: Vector2 -var _move_direction: Vector3 func _ready() -> void: @@ -27,103 +15,31 @@ func _ready() -> void: func _process(_delta: float) -> void: - _controller_aiming() + aiming.controller_aiming(movement.move_input) call_deferred("_mouse_aiming") _process_respawning() - Debugger.marker("aim", global_position + aim_offset) + Debugger.marker("aim", global_position + aiming.aim_offset) func _physics_process(delta: float) -> void: if not is_multiplayer_authority(): return - _lateral_movement(delta) - _vertical_movement(delta) + velocity = movement.lateral_movement(velocity, delta) + velocity = movement.vertical_movement(velocity, delta, is_on_floor()) move_and_slide() -func _controller_aiming() -> void: - if Inputer.mode != Inputer.Mode.CONTROLLER: - return - - aim_input = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down") - - if aim_input.length() == 0 and _move_input.length() == 0: - return - - var input := (aim_input if aim_input.length() > 0 else _move_input).normalized() - - var aim_direction := Vector3(input.x, 0, input.y * _vertical_aim_aspect) - - aim_offset = ( - aim_direction.rotated(Vector3.UP, Referencer.main_camera.rotation.y) - * _controller_aim_offset - ) - - look_at(global_position + aim_offset, Vector3.UP, true) - - func _mouse_aiming() -> void: - if Inputer.mode != Inputer.Mode.KB_MOUSE: - return - - var player_position := global_position - if is_on_floor(): - _floor_height = player_position.y - player_position.y = _floor_height - - var aim_position := _mouse_project(_floor_height) - aim_offset = aim_position - player_position - aim_position.y = global_position.y - - if aim_position == global_position: - return - - look_at(aim_position, Vector3.UP, true) - - -func _mouse_project(height: float) -> Vector3: var mouse_pos := get_viewport().get_mouse_position() - var camera := Referencer.main_camera - - var from := camera.project_ray_origin(mouse_pos) - var direction := camera.project_ray_normal(mouse_pos) - var plane := Plane(Vector3.UP, height) - - var intersection: Variant = plane.intersects_ray(from, direction) - if not intersection: - return Vector3.ZERO - return intersection as Vector3 + aiming.mouse_aiming(mouse_pos, global_position, is_on_floor()) + if aiming.aim_offset.length() > 0: + look_at(global_position + aiming.aim_offset, Vector3.UP, true) func _process_respawning() -> void: if global_position.y < _respawn_height: global_position = _respawn_point velocity = Vector3.ZERO - - -func _lateral_movement(delta: float) -> void: - _move_input = Input.get_vector("move_left", "move_right", "move_up", "move_down") - - if _move_input.length() > 0: - _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) - - -func _vertical_movement(delta: float) -> void: - 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 diff --git a/scripts/player/player_aiming.gd b/scripts/player/player_aiming.gd new file mode 100644 index 0000000..444b2f4 --- /dev/null +++ b/scripts/player/player_aiming.gd @@ -0,0 +1,56 @@ +class_name PlayerAiming +extends RefCounted + +@export var _controller_aim_offset: float = 20 +@export var _vertical_aim_aspect: float = 1.5 + +var aim_offset: Vector3 +var aim_input: Vector2 + +var _floor_height: float + + +func controller_aiming(move_input: Vector2) -> void: + if Inputer.mode != Inputer.Mode.CONTROLLER: + return + + aim_input = Input.get_vector("aim_left", "aim_right", "aim_up", "aim_down") + + if aim_input.length() == 0 and move_input.length() == 0: + return + + var input := (aim_input if aim_input.length() > 0 else move_input).normalized() + + var aim_direction := Vector3(input.x, 0, input.y * _vertical_aim_aspect) + + aim_offset = ( + aim_direction.rotated(Vector3.UP, Referencer.main_camera.rotation.y) + * _controller_aim_offset + ) + + +func mouse_aiming(mouse_pos: Vector2, player_position: Vector3, is_on_floor: bool) -> void: + if Inputer.mode != Inputer.Mode.KB_MOUSE: + return + + var position_y := player_position.y + if is_on_floor: + _floor_height = player_position.y + player_position.y = _floor_height + + var aim_position := _mouse_project(mouse_pos, _floor_height) + aim_offset = aim_position - player_position + aim_position.y = position_y + + +func _mouse_project(mouse_pos:Vector2, height: float) -> Vector3: + var camera := Referencer.main_camera + + var from := camera.project_ray_origin(mouse_pos) + var direction := camera.project_ray_normal(mouse_pos) + var plane := Plane(Vector3.UP, height) + + var intersection: Variant = plane.intersects_ray(from, direction) + if not intersection: + return Vector3.ZERO + return intersection as Vector3 diff --git a/scripts/player/player_movement.gd b/scripts/player/player_movement.gd new file mode 100644 index 0000000..7a438d7 --- /dev/null +++ b/scripts/player/player_movement.gd @@ -0,0 +1,42 @@ +class_name PlayerMovement +extends RefCounted + +const MOVE_SPEED: float = 10 +const MOVE_ACCELERATION: float = 100 +const MOVE_DECELERATION: float = 50 + +const FALL_SPEED: float = 20 +const FALL_ACCELERATION: float = 25 + +var move_input: Vector2 + +var _move_direction: Vector3 + + +func lateral_movement(velocity: Vector3, delta: float) -> Vector3: + move_input = Input.get_vector("move_left", "move_right", "move_up", "move_down") + + if move_input.length() > 0: + _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