You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
6.4 KiB
C#
206 lines
6.4 KiB
C#
|
4 years ago
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using SiegeSong;
|
||
|
|
|
||
|
|
namespace SiegeSong
|
||
|
|
{
|
||
|
4 years ago
|
public class CameraManager : MonoBehaviour
|
||
|
4 years ago
|
{
|
||
|
4 years ago
|
public RuntimeManager RuntimeManager;
|
||
|
4 years ago
|
|
||
|
4 years ago
|
public Motor Motor;
|
||
|
4 years ago
|
|
||
|
|
public GameObject ActiveCamera;
|
||
|
|
private int activeCameraIndex;
|
||
|
|
|
||
|
|
public bool FirstPersonActive;
|
||
|
|
|
||
|
|
public Transform CameraTarget;
|
||
|
|
|
||
|
|
public GameObject LeftCamera;
|
||
|
|
public GameObject CenterCamera;
|
||
|
|
public GameObject RightCamera;
|
||
|
|
|
||
|
|
public GameObject FirstPersonCamera;
|
||
|
|
|
||
|
4 years ago
|
public float CameraX;
|
||
|
|
public float CameraY;
|
||
|
|
|
||
|
|
private float cameraSpeedX = 200.0f;
|
||
|
|
private float cameraSpeedY = 100.0f;
|
||
|
|
|
||
|
|
private float cameraMinX = -90.0f;
|
||
|
|
private float cameraMaxX = 90.0f;
|
||
|
|
|
||
|
|
private float cameraMinY = -70.0f;
|
||
|
|
private float cameraMaxY = 90.0f;
|
||
|
|
private float firstPersonMinY = -170.0f;
|
||
|
|
private float firstPersonMaxY = 130.0f;
|
||
|
4 years ago
|
|
||
|
4 years ago
|
private float cameraZoomSpeed = 0.1f;
|
||
|
|
private float cameraZoom = 2.0f;
|
||
|
|
private float cameraRequestedZoom = 2.0f;
|
||
|
|
private float cameraMinZoom = -0.2f;
|
||
|
|
private float cameraMaxZoom = 0.5f;
|
||
|
|
private bool canZoomToRequested = true;
|
||
|
|
private bool cameraZooming;
|
||
|
|
private bool cameraZoomCollision;
|
||
|
|
|
||
|
|
private float rotationSpeedBrakeSpeed = 2.0f;
|
||
|
|
private float rotationSpeedStopThreshold = 0.1f;
|
||
|
|
|
||
|
|
private float cameraZoomStep = 0.1f;
|
||
|
4 years ago
|
|
||
|
|
//public CameraMode ActiveMode;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
4 years ago
|
if (Motor.UsingStation)
|
||
|
4 years ago
|
{
|
||
|
4 years ago
|
CameraX = Motor.TargetStation.CameraX;
|
||
|
|
CameraY = Motor.TargetStation.CameraY;
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
4 years ago
|
if (!cameraZoomCollision)
|
||
|
4 years ago
|
{
|
||
|
|
for (var i = 0; i < ActiveCamera.transform.childCount; i++)
|
||
|
|
{
|
||
|
|
var child = ActiveCamera.transform.GetChild(i);
|
||
|
|
if (child.gameObject.CompareTag("MainCamera") && !FirstPersonActive)
|
||
|
|
{
|
||
|
4 years ago
|
child.position = new Vector3(child.position.x, child.position.y, child.position.z + cameraZoom);
|
||
|
|
cameraZoom = 0;
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SwitchActiveCamera(Camera targetCamera)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ZoomIn()
|
||
|
|
{
|
||
|
|
if (!FirstPersonActive)
|
||
|
|
{
|
||
|
4 years ago
|
if (cameraZoom - cameraZoomStep > cameraMinZoom)
|
||
|
4 years ago
|
{
|
||
|
4 years ago
|
cameraZoom -= cameraZoomStep;
|
||
|
4 years ago
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
4 years ago
|
cameraZoom = cameraMinZoom;
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ZoomOut(bool request = true)
|
||
|
|
{
|
||
|
|
if (!FirstPersonActive)
|
||
|
|
{
|
||
|
4 years ago
|
cameraZoom += cameraZoomStep;
|
||
|
4 years ago
|
}
|
||
|
|
if (!FirstPersonActive)
|
||
|
|
{
|
||
|
4 years ago
|
if (cameraZoom + cameraZoomStep < cameraMaxZoom)
|
||
|
4 years ago
|
{
|
||
|
4 years ago
|
cameraZoom += cameraZoomStep;
|
||
|
4 years ago
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
4 years ago
|
cameraZoom = cameraMaxZoom;
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void RealignCamera(bool rotateBody = false)
|
||
|
|
{
|
||
|
|
if (!rotateBody)
|
||
|
|
{
|
||
|
4 years ago
|
if (CameraX < rotationSpeedStopThreshold && CameraX > -rotationSpeedStopThreshold)
|
||
|
|
CameraX = 0;
|
||
|
4 years ago
|
else
|
||
|
4 years ago
|
CameraX = CameraX / rotationSpeedBrakeSpeed;
|
||
|
4 years ago
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
4 years ago
|
Motor.RotateToDirection(ActiveCamera.transform.eulerAngles);
|
||
|
4 years ago
|
|
||
|
4 years ago
|
ActiveCamera.transform.RotateAround(CameraTarget.position, Vector3.up, -CameraX);
|
||
|
4 years ago
|
|
||
|
4 years ago
|
CameraX = 0;
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
public void RotateMotor(Vector3 eulerAngles)
|
||
|
|
{
|
||
|
|
Motor.RotateToDirection(eulerAngles);
|
||
|
|
|
||
|
|
ActiveCamera.transform.RotateAround(CameraTarget.position, Vector3.up, -eulerAngles.x);
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
public void RotateHorizontal(float amount)
|
||
|
|
{
|
||
|
4 years ago
|
var degreesToRotate = amount * cameraSpeedX * Time.deltaTime;
|
||
|
4 years ago
|
|
||
|
4 years ago
|
CameraX += degreesToRotate;
|
||
|
|
ActiveCamera.transform.RotateAround(CameraTarget.position, Vector3.up, amount * cameraSpeedX * Time.deltaTime);
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
|
public void RotateVertical(float amount)
|
||
|
|
{
|
||
|
4 years ago
|
var degreesToRotate = amount * cameraSpeedY * Time.deltaTime;
|
||
|
|
if (degreesToRotate > 0 && !(CameraY + degreesToRotate > (FirstPersonActive ? firstPersonMaxY : cameraMaxY))
|
||
|
|
|| (degreesToRotate < 0 && !(CameraY + degreesToRotate < (FirstPersonActive ? firstPersonMinY : cameraMinY))))
|
||
|
4 years ago
|
{
|
||
|
4 years ago
|
CameraY += degreesToRotate;
|
||
|
|
ActiveCamera.transform.RotateAround(CameraTarget.position, ActiveCamera.transform.TransformDirection(-Vector3.right), amount * cameraSpeedY * Time.deltaTime);
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SwitchCamera()
|
||
|
|
{
|
||
|
4 years ago
|
ActiveCamera.transform.RotateAround(CameraTarget.position, ActiveCamera.transform.TransformDirection(-Vector3.right), -CameraY);
|
||
|
|
CameraY = 0;
|
||
|
4 years ago
|
switch (activeCameraIndex)
|
||
|
|
{
|
||
|
|
case 0:
|
||
|
|
FirstPersonActive = false;
|
||
|
|
FirstPersonCamera.active = false;
|
||
|
|
CenterCamera.active = true;
|
||
|
|
ActiveCamera = CenterCamera;
|
||
|
|
activeCameraIndex++;
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
LeftCamera.active = false;
|
||
|
|
RightCamera.active = true;
|
||
|
|
ActiveCamera = RightCamera;
|
||
|
|
activeCameraIndex++;
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
RightCamera.active = false;
|
||
|
|
LeftCamera.active = true;
|
||
|
|
ActiveCamera = LeftCamera;
|
||
|
|
activeCameraIndex++;
|
||
|
|
break;
|
||
|
|
case 3:
|
||
|
|
default:
|
||
|
|
LeftCamera.active = false;
|
||
|
|
FirstPersonActive = true;
|
||
|
|
FirstPersonCamera.active = true;
|
||
|
|
ActiveCamera = FirstPersonCamera;
|
||
|
|
activeCameraIndex = 0;
|
||
|
|
break;
|
||
|
|
}
|
||
|
4 years ago
|
RuntimeManager.PopulateReferences();
|
||
|
4 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|