121 lines
2.8 KiB
C#
121 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITFALL.Player.Equip;
|
|
using BITFALL.Player.Movement;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.PlayerCamera;
|
|
using Cinemachine;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.Interactions;
|
|
|
|
namespace BITFALL.Player
|
|
{
|
|
public class PlayerTPSController : EntityBehavior
|
|
{
|
|
[SerializeField] private CinemachineVirtualCamera thirdPersonCamera;
|
|
|
|
[Inject]
|
|
private IPlayerCameraService playerCameraService;
|
|
|
|
[Header(Constant.Header.Input)]
|
|
[SerializeField] private InputActionReference toggleCameraAction;
|
|
[SerializeField] private InputActionReference scrollWheelAction;
|
|
|
|
[Inject(true)]
|
|
private InputActionGroup _inputActionGroup;
|
|
|
|
[Inject] private IEquipService _equipService;
|
|
|
|
[Inject] private IEntityMovement _movement;
|
|
|
|
private readonly ValidHandle _allowTPS = new();
|
|
|
|
private bool enableTPS;
|
|
|
|
private Cinemachine3rdPersonFollow _aim;
|
|
|
|
private Vector3 _initialShoulderOffset;
|
|
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
playerCameraService.OnCameraActivated += OnCameraActivated;
|
|
|
|
_aim = thirdPersonCamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
|
|
|
_inputActionGroup.RegisterCallback(toggleCameraAction, OnToggleCamera);
|
|
_inputActionGroup.RegisterCallback(scrollWheelAction, OnScrollWheel);
|
|
|
|
_allowTPS.AddListener(x => thirdPersonCamera.enabled = x);
|
|
|
|
_movement.OnStateChanged += OnStateChanged;
|
|
|
|
_initialShoulderOffset = _aim.ShoulderOffset;
|
|
}
|
|
|
|
|
|
private void OnScrollWheel(InputAction.CallbackContext obj)
|
|
{
|
|
switch (_movement.CurrentState)
|
|
{
|
|
case IPlayerFixedState:
|
|
case IPlayerParachuteState:
|
|
break;
|
|
default:
|
|
_aim.CameraDistance =
|
|
Mathf.Clamp(_aim.CameraDistance - obj.ReadValue<Vector2>().y * Time.deltaTime * 0.2f, 0, 10);
|
|
break;
|
|
}
|
|
}
|
|
private float _initialCameraDistance;
|
|
private void OnStateChanged(IEntityMovementState arg1, IEntityMovementState arg2)
|
|
{
|
|
_aim.ShoulderOffset = arg2
|
|
is IPlayerFixedState
|
|
or IPlayerParachuteState
|
|
? Vector3.zero : _initialShoulderOffset;
|
|
|
|
switch (_movement.CurrentState)
|
|
{
|
|
case IPlayerFixedState:
|
|
case IPlayerParachuteState:
|
|
_initialCameraDistance = _aim.CameraDistance;
|
|
_aim.CameraDistance = 16f;
|
|
break;
|
|
default:
|
|
if (_initialCameraDistance is not 0)
|
|
_aim.CameraDistance = _initialCameraDistance;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void OnUpdate(float deltaTime)
|
|
{
|
|
base.OnUpdate(deltaTime);
|
|
_allowTPS.SetDisableElements("Aim",_equipService.Zoom.Allow);
|
|
}
|
|
|
|
private void OnToggleCamera(InputAction.CallbackContext obj)
|
|
{
|
|
enableTPS = !enableTPS;
|
|
if (enableTPS)
|
|
{
|
|
_allowTPS.AddElement(this);
|
|
}
|
|
else
|
|
{
|
|
_allowTPS.RemoveElement(this);
|
|
}
|
|
}
|
|
|
|
private void OnCameraActivated(bool obj)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
}
|