103 lines
3.7 KiB
C#
103 lines
3.7 KiB
C#
using Unity.Cinemachine;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public class CameraController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
|
{
|
|
public static CameraController Instance;
|
|
[SerializeField] private CinemachineCamera _cinemachineCamera;
|
|
[SerializeField] private CinemachineOrbitalFollow orbital;
|
|
private bool fingerDown = false;
|
|
[SerializeField, Min(0f)] private float dragDeadZone = 10f;
|
|
[SerializeField] private float horizontalRotationPerScreen = 60f;
|
|
[SerializeField] private float verticalRotationPerScreen = 500f;
|
|
[SerializeField, HideInInspector] private bool _migratedDeadZoneToNormalized;
|
|
|
|
public CinemachineOrbitalFollow Orbital { get => orbital;}
|
|
|
|
void OnEnable()
|
|
{
|
|
Instance = this;
|
|
MigrateLegacyDeadZone();
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!fingerDown || orbital == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float screenReference = Mathf.Max(1f, Mathf.Min(Screen.width, Screen.height));
|
|
Vector2 normalizedDelta = eventData.delta / screenReference;
|
|
float normalizedDeadZone = Mathf.Max(0.0001f, dragDeadZone);
|
|
|
|
if (normalizedDelta.sqrMagnitude >= normalizedDeadZone * normalizedDeadZone)
|
|
{
|
|
orbital.HorizontalAxis.Value += normalizedDelta.x * horizontalRotationPerScreen;
|
|
//orbital.HorizontalAxis.Value = Mathf.Clamp(orbital.HorizontalAxis.Value, -360f, 360f);
|
|
orbital.VerticalAxis.Value -= normalizedDelta.y * verticalRotationPerScreen;
|
|
orbital.VerticalAxis.Value = Mathf.Clamp(orbital.VerticalAxis.Value, -360f, 360f);
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
fingerDown = true;
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
fingerDown = false;
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
MigrateLegacyDeadZone();
|
|
}
|
|
|
|
private void MigrateLegacyDeadZone()
|
|
{
|
|
if (_migratedDeadZoneToNormalized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Older values were serialized in pixels; convert once to normalized short-side ratio.
|
|
if (dragDeadZone > 0.05f)
|
|
{
|
|
dragDeadZone /= 1080f;
|
|
}
|
|
|
|
dragDeadZone = Mathf.Max(0.0001f, dragDeadZone);
|
|
|
|
_migratedDeadZoneToNormalized = true;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//todo: should not always update
|
|
if (_cinemachineCamera.Follow == null && CECGameRun.Instance.GetHostPlayer() != null)
|
|
{
|
|
if(CECGameRun.Instance.GetHostPlayer().PointCam != null)
|
|
{
|
|
_cinemachineCamera.Follow = CECGameRun.Instance.GetHostPlayer().PointCam;
|
|
_cinemachineCamera.ForceCameraPosition(CECGameRun.Instance.GetHostPlayer().PointCam.position, Quaternion.identity);
|
|
orbital.HorizontalAxis.Value = 208;
|
|
orbital.VerticalAxis.Value = -268;
|
|
}
|
|
else
|
|
{
|
|
_cinemachineCamera.Follow = CECGameRun.Instance.GetHostPlayer().transform;
|
|
_cinemachineCamera.ForceCameraPosition(CECGameRun.Instance.GetHostPlayer().ObjectPosition, Quaternion.identity);
|
|
orbital.HorizontalAxis.Value = 208;
|
|
orbital.VerticalAxis.Value = -268;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|