271 lines
6.7 KiB
C#
271 lines
6.7 KiB
C#
using System;
|
|
using Obvious.Soap;
|
|
using SoapCustomVariable;
|
|
using UnityEngine;
|
|
|
|
namespace ECM2.Examples.FirstPerson
|
|
{
|
|
public class CharacterLook : MonoBehaviour
|
|
{
|
|
[Space(15f)]
|
|
public bool invertLook = true;
|
|
|
|
[Tooltip("Mouse look sensitivity")]
|
|
public FloatVariable MouseSensitivity;
|
|
|
|
[Space(15f)]
|
|
[Tooltip("How far in degrees can you move the camera down.")]
|
|
public float minPitch = -80f;
|
|
|
|
[Tooltip("How far in degrees can you move the camera up.")]
|
|
public float maxPitch = 80f;
|
|
|
|
public CameraViewTypeVariable CameraView;
|
|
|
|
public BoolVariable isBobberZoomInEnabled;
|
|
|
|
public BoolVariable IsUnderwaterCameraEnabled;
|
|
|
|
public BoolVariable IsRightButtonPressed;
|
|
|
|
public Vector2Variable MouseInput;
|
|
|
|
public FSMVariable PlayerState;
|
|
|
|
public FloatVariable LineTension;
|
|
|
|
public ScriptableEventBool onInVehicleStateChange;
|
|
|
|
private FirstPersonCharacter _character;
|
|
|
|
private bool _isLookingEnabled = true;
|
|
|
|
private bool _IsInVehicle;
|
|
|
|
[SerializeField]
|
|
private float _PrepareLookPitch;
|
|
|
|
[SerializeField]
|
|
private float _PrepareLookPitchTime;
|
|
|
|
[SerializeField]
|
|
private float _CastingLookPitch;
|
|
|
|
[SerializeField]
|
|
private float _CastingLookPitchTime;
|
|
|
|
public Transform TPPLookTarget;
|
|
|
|
public Transform VehicleLookTargetParent;
|
|
|
|
public float boatCameraDistance = 4f;
|
|
|
|
private float lookXRot;
|
|
|
|
private float lookYRot;
|
|
|
|
public Vector2 VehicleLookXMinMax;
|
|
|
|
public Vector2 VehicleLookYMinMax;
|
|
|
|
public bool isLookingEnabled
|
|
{
|
|
set
|
|
{
|
|
_isLookingEnabled = value;
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
_character = GetComponent<FirstPersonCharacter>();
|
|
CameraView.OnValueChanged += CameraViewOnOnValueChanged;
|
|
PlayerState.OnValueChanged += PlayerStateOnOnValueChanged;
|
|
IsRightButtonPressed.OnValueChanged += IsRightButtonPressedOnOnValueChanged;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
onInVehicleStateChange.OnRaised += OnInVehicleStateChangeOnOnRaised;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
onInVehicleStateChange.OnRaised -= OnInVehicleStateChangeOnOnRaised;
|
|
}
|
|
|
|
private void OnInVehicleStateChangeOnOnRaised(bool obj)
|
|
{
|
|
if (!obj)
|
|
{
|
|
AdjustTPPCameraLookRotation();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
|
|
private void CameraViewOnOnValueChanged(CameraViewType obj)
|
|
{
|
|
switch (obj)
|
|
{
|
|
case CameraViewType.TPP:
|
|
AdjustTPPCameraLookRotation();
|
|
break;
|
|
case CameraViewType.FPP:
|
|
case CameraViewType.Boat:
|
|
lookXRot = 25f;
|
|
lookYRot = 0f;
|
|
_ = CameraView.PreviousValue;
|
|
_ = 3;
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException("obj", obj, null);
|
|
case CameraViewType.Inventory:
|
|
case CameraViewType.Lure:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void AdjustTPPCameraLookRotation()
|
|
{
|
|
if (PlayerState.Value == State.vehicle)
|
|
{
|
|
TPPLookTarget.eulerAngles = base.transform.eulerAngles;
|
|
lookXRot = TPPLookTarget.localEulerAngles.x;
|
|
lookYRot = TPPLookTarget.localEulerAngles.y;
|
|
}
|
|
else
|
|
{
|
|
TPPLookTarget.eulerAngles = base.transform.eulerAngles;
|
|
_character.CameraPitch = 0f;
|
|
lookXRot = 0f;
|
|
lookYRot = TPPLookTarget.localEulerAngles.y;
|
|
}
|
|
}
|
|
|
|
private void IsRightButtonPressedOnOnValueChanged(bool value)
|
|
{
|
|
if (PlayerState.Value != State.fishing)
|
|
{
|
|
_ = PlayerState.Value;
|
|
_ = 6;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CameraView.OnValueChanged -= CameraViewOnOnValueChanged;
|
|
PlayerState.OnValueChanged -= PlayerStateOnOnValueChanged;
|
|
IsRightButtonPressed.OnValueChanged -= IsRightButtonPressedOnOnValueChanged;
|
|
}
|
|
|
|
private void PlayerStateOnOnValueChanged(State newState)
|
|
{
|
|
State previousValue = PlayerState.PreviousValue;
|
|
if (previousValue == State.vehicle || previousValue == State.vehicleFishing)
|
|
{
|
|
_IsInVehicle = false;
|
|
Transform child = TPPLookTarget.GetChild(0);
|
|
Vector3 localPosition = child.localPosition;
|
|
child.localPosition = new Vector3(localPosition.x, localPosition.y, (PlayerState.Value == State.vehicle) ? (0f - boatCameraDistance) : 0f);
|
|
if (newState == State.idle)
|
|
{
|
|
TPPLookTarget.eulerAngles = base.transform.eulerAngles;
|
|
_character.CameraPitch = 0f;
|
|
lookXRot = 0f;
|
|
lookYRot = TPPLookTarget.localEulerAngles.y;
|
|
}
|
|
}
|
|
switch (newState)
|
|
{
|
|
case State.idle:
|
|
case State.fishing:
|
|
case State.fight:
|
|
_isLookingEnabled = true;
|
|
break;
|
|
case State.prepare:
|
|
if (CameraView.Value == CameraViewType.TPP)
|
|
{
|
|
CameraView.Value = CameraViewType.FPP;
|
|
Vector3 eulerAngles = TPPLookTarget.eulerAngles;
|
|
base.transform.eulerAngles = new Vector3(0f, eulerAngles.y, 0f);
|
|
}
|
|
break;
|
|
case State.casting:
|
|
_isLookingEnabled = true;
|
|
break;
|
|
case State.collectFish:
|
|
_isLookingEnabled = false;
|
|
_character.SetPitchTween(_CastingLookPitch, _CastingLookPitchTime);
|
|
break;
|
|
case State.vehicle:
|
|
case State.vehicleFishing:
|
|
{
|
|
Transform child2 = TPPLookTarget.GetChild(0);
|
|
Vector3 localPosition2 = child2.localPosition;
|
|
child2.localPosition = new Vector3(localPosition2.x, localPosition2.y, (PlayerState.Value == State.vehicle) ? (-3f) : 0f);
|
|
_IsInVehicle = true;
|
|
if (CameraView.Value == CameraViewType.TPP)
|
|
{
|
|
AdjustTPPCameraLookRotation();
|
|
}
|
|
else
|
|
{
|
|
lookXRot = 25f;
|
|
lookYRot = 0f;
|
|
}
|
|
_isLookingEnabled = true;
|
|
break;
|
|
}
|
|
case State.move:
|
|
case State.baitFlies:
|
|
case State.fishView:
|
|
case State.throwFish:
|
|
case State.swiming:
|
|
case State.flyModeDebug:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (isBobberZoomInEnabled.Value || !_isLookingEnabled || IsUnderwaterCameraEnabled.Value)
|
|
{
|
|
return;
|
|
}
|
|
TPPLookTarget.position = base.transform.position;
|
|
if (CameraView.Value == CameraViewType.TPP)
|
|
{
|
|
lookXRot -= MouseInput.Value.y;
|
|
lookXRot = Mathf.Clamp(lookXRot, -25f, 55f);
|
|
lookYRot += MouseInput.Value.x;
|
|
lookYRot = Mathf.Repeat(lookYRot, 360f);
|
|
TPPLookTarget.localEulerAngles = new Vector3(lookXRot, lookYRot, 0f);
|
|
}
|
|
else if (CameraView.Value == CameraViewType.FPP)
|
|
{
|
|
if (_IsInVehicle && PlayerState.Value == State.vehicle)
|
|
{
|
|
lookXRot -= MouseInput.Value.y;
|
|
lookXRot = Mathf.Clamp(lookXRot, VehicleLookXMinMax.x, VehicleLookXMinMax.y);
|
|
lookYRot += MouseInput.Value.x;
|
|
lookYRot = Mathf.Clamp(lookYRot, VehicleLookYMinMax.x, VehicleLookYMinMax.y);
|
|
VehicleLookTargetParent.localEulerAngles = new Vector3(lookXRot, lookYRot, 0f);
|
|
_character.CameraPitch = 0f;
|
|
}
|
|
else
|
|
{
|
|
Vector2 value = MouseInput.Value;
|
|
_character.AddControlYawInput(value.x * (float)MouseSensitivity);
|
|
_character.AddControlPitchInput((invertLook ? (0f - value.y) : value.y) * (float)MouseSensitivity, minPitch, maxPitch);
|
|
lookXRot = base.transform.eulerAngles.x;
|
|
lookYRot = base.transform.eulerAngles.y;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|