game/player_controller.gd

93 lines
2.6 KiB
GDScript3
Raw Normal View History

2023-01-10 02:36:09 +01:00
class_name PlayerController extends Node3D
const _BACKWARD := "player_controller_backward"
const _FORWARD := "player_controller_forward"
const _LEFT := "player_controller_left"
const _RIGHT := "player_controller_right"
const _ROTATE_CCW := "player_controller_rotate_ccw"
const _ROTATE_CW := "player_controller_rotate_cw"
const _DRAG_SPEED_BASE_MODIFIER := 0.15
const _MOVE_SPEED_BASE_MODIFIER := 50.0
const _ROTATE_SPEED_BASE := 5.0
const _TRANSFORM_DELTA := 10.0
var _is_drag_panning := false
@onready
var _target_position := self.position
@onready
var _target_orientation := self.global_rotation.y
@export
var movement_smoothing := 0.5
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
match event.button_index:
MOUSE_BUTTON_MIDDLE:
self._is_drag_panning = event.is_pressed()
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if self._is_drag_panning else Input.MOUSE_MODE_VISIBLE
MOUSE_BUTTON_WHEEL_DOWN:
pass
MOUSE_BUTTON_WHEEL_UP:
pass
return
if (event is InputEventMouseMotion) and self._is_drag_panning:
var global_basis := self.global_transform.basis
var camera_settings := GameSettings.camera_settings
var dampened_speed :=\
camera_settings.movement_speed_modifier * _DRAG_SPEED_BASE_MODIFIER
self._target_position += dampened_speed.y * (-global_basis.z) *\
event.relative.y * (-1.0 if camera_settings.is_y_inverted else 1.0)
self._target_position += dampened_speed.x * (-global_basis.x) *\
event.relative.x * (-1.0 if camera_settings.is_x_inverted else 1.0)
return
if event is InputEventScreenDrag:
return
func _process(delta: float) -> void:
var global_basis := self.global_transform.basis
var camera_settings := GameSettings.camera_settings
var delta_speed := camera_settings.movement_speed_modifier * _MOVE_SPEED_BASE_MODIFIER * delta
self._target_position += delta_speed.y * (-global_basis.z) *\
(Input.get_action_strength(_FORWARD) - Input.get_action_strength(_BACKWARD)) *\
(-1.0 if camera_settings.is_y_inverted else 1.0)
self._target_position += delta_speed.x * (-global_basis.x) *\
(Input.get_action_strength(_LEFT) - Input.get_action_strength(_RIGHT)) *\
(-1.0 if camera_settings.is_y_inverted else 1.0)
self._target_orientation += (Input.get_action_strength(_ROTATE_CCW) -\
Input.get_action_strength(_ROTATE_CW)) * _ROTATE_SPEED_BASE *\
camera_settings.rotation_speed_modifier * delta
self.global_transform = self.global_transform.interpolate_with(
Transform3D(Basis(Vector3.UP, self._target_orientation), self._target_position),
delta * _TRANSFORM_DELTA * self.movement_smoothing)
##
##
##
func is_drag_panning() -> bool:
return self._is_drag_panning