74 lines
2.8 KiB
C#
74 lines
2.8 KiB
C#
using System;
|
|
using Code.Example.Liminal.Character;
|
|
using Code.Infrastructure.Camera;
|
|
using Code.Infrastructure.Camera.CameraStates;
|
|
using Code.Infrastructure.Input;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Example.Liminal.Player
|
|
{
|
|
public class PlayerMovementController : MonoBehaviour
|
|
{
|
|
[SerializeField] private LiminalCharacterController characterController;
|
|
private PlayerCharacterInputs _currentInputs = new();
|
|
private IInputService _inputService;
|
|
|
|
private CameraStateMachine _cameraStateMachine;
|
|
|
|
public Transform PlayerHeadPoint;
|
|
|
|
[Inject]
|
|
private void Construct(IInputService inputService, CameraStateMachine stateMachine)
|
|
{
|
|
_inputService = inputService;
|
|
_inputService.BindActionPerformed((int) ActionMappings.Gameplay, (int) GameplayActions.Move, OnMovePerformed);
|
|
_inputService.BindActionCancel((int) ActionMappings.Gameplay, (int) GameplayActions.Move, OnMoveCanceled);
|
|
_inputService.BindActionPerformed((int) ActionMappings.Gameplay, (int) GameplayActions.Crouch, OnCrouchPerformed);
|
|
_inputService.BindActionCancel((int) ActionMappings.Gameplay, (int) GameplayActions.Crouch, OnCrouchCanceled);
|
|
|
|
_cameraStateMachine = stateMachine;
|
|
((FirstPersonCameraState)stateMachine.CurrentBaseState).SetPlayer(this);
|
|
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
_currentInputs.MoveAxisForward = 0;
|
|
_currentInputs.MoveAxisRight = 0;
|
|
_currentInputs.CrouchUp = true;
|
|
_currentInputs.CrouchDown = false;
|
|
_currentInputs.CameraRotation = Quaternion.identity;
|
|
}
|
|
|
|
public void SetCameraRotation(Quaternion rotation) => _currentInputs.CameraRotation = rotation;
|
|
private void OnMovePerformed(InputAction.CallbackContext context)
|
|
{
|
|
_currentInputs.MoveAxisForward = context.ReadValue<Vector2>().y;
|
|
_currentInputs.MoveAxisRight = context.ReadValue<Vector2>().x;
|
|
}
|
|
|
|
private void OnMoveCanceled(InputAction.CallbackContext context)
|
|
{
|
|
_currentInputs.MoveAxisForward = 0;
|
|
_currentInputs.MoveAxisRight = 0;
|
|
}
|
|
|
|
private void OnCrouchPerformed(InputAction.CallbackContext context)
|
|
{
|
|
_currentInputs.CrouchDown = true;
|
|
_currentInputs.CrouchUp = false;
|
|
}
|
|
|
|
private void OnCrouchCanceled(InputAction.CallbackContext context)
|
|
{
|
|
_currentInputs.CrouchUp = true;
|
|
_currentInputs.CrouchDown = false;
|
|
}
|
|
private void Update()
|
|
{
|
|
characterController.SetInputs(_currentInputs);
|
|
}
|
|
}
|
|
} |