2026-03-26 20:27:19 +07:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace OnlyScove.Scripts
|
|
|
|
|
{
|
|
|
|
|
public class CameraController : MonoBehaviour
|
|
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
[SerializeField] InputReader inputReader;
|
2026-03-26 20:27:19 +07:00
|
|
|
[SerializeField] Transform followTarget;
|
2026-03-27 12:08:16 +07:00
|
|
|
[SerializeField] float positionSmoothTime = 0.12f;
|
|
|
|
|
[SerializeField] float rotationSmoothTime = 5f;
|
2026-03-26 20:27:19 +07:00
|
|
|
[SerializeField] Vector2 framingOffset;
|
|
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
[Header("Components")]
|
|
|
|
|
[SerializeField] private CameraRotationHandler rotationHandler = new CameraRotationHandler();
|
|
|
|
|
[SerializeField] private CameraZoomHandler zoomHandler = new CameraZoomHandler();
|
|
|
|
|
[SerializeField] private CameraCollisionHandler collisionHandler = new CameraCollisionHandler();
|
|
|
|
|
[SerializeField] private CameraOcclusionTransparency occlusionTransparency = new CameraOcclusionTransparency();
|
|
|
|
|
[SerializeField] private CameraDynamicFOV dynamicFOV = new CameraDynamicFOV();
|
|
|
|
|
[SerializeField] private CameraCharacterFading characterFading = new CameraCharacterFading();
|
|
|
|
|
[SerializeField] private CameraSideBias sideBias = new CameraSideBias();
|
|
|
|
|
[SerializeField] private CameraShakeManager shakeManager = new CameraShakeManager();
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
private Vector3 _currentVelocity;
|
|
|
|
|
private Camera _cam;
|
2026-03-26 20:27:19 +07:00
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
_cam = GetComponent<Camera>();
|
2026-03-26 20:27:19 +07:00
|
|
|
Cursor.visible = false;
|
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
rotationHandler.Initialize(transform);
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
// Handle Input-related updates first
|
2026-03-26 20:27:19 +07:00
|
|
|
if (inputReader != null)
|
|
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
rotationHandler.HandleRotation(inputReader, followTarget, rotationSmoothTime);
|
|
|
|
|
zoomHandler.HandleZoom(inputReader);
|
|
|
|
|
sideBias.HandleSideBias(inputReader);
|
|
|
|
|
dynamicFOV.HandleDynamicFOV(_cam, inputReader);
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Update camera rotation
|
|
|
|
|
transform.rotation = rotationHandler.CurrentRotation;
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Calculate focus position with framing offset and side bias
|
|
|
|
|
Vector3 focusPosition = followTarget.position + rotationHandler.CurrentRotation * new Vector3(framingOffset.x + sideBias.CurrentSideBias, framingOffset.y, 0);
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Handle collision
|
|
|
|
|
float targetDistance = collisionHandler.CheckCollision(focusPosition, rotationHandler.CurrentRotation, zoomHandler.CurrentDistance, zoomHandler.MinDistance);
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Handle character fading
|
|
|
|
|
characterFading.HandleCharacterFading(targetDistance);
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Calculate target position
|
|
|
|
|
Vector3 targetPosition = focusPosition - rotationHandler.CurrentRotation * new Vector3(0, 0, targetDistance);
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Handle camera shake
|
|
|
|
|
shakeManager.HandleShake();
|
2026-03-26 20:27:19 +07:00
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
// Apply final position and rotation
|
|
|
|
|
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref _currentVelocity, positionSmoothTime) + shakeManager.ShakeOffset;
|
|
|
|
|
|
|
|
|
|
// Handle occlusion transparency (needs camera's final position and rotation)
|
|
|
|
|
occlusionTransparency.HandleTransparency(transform, focusPosition);
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Shake(float intensity, float duration)
|
|
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
shakeManager.Shake(intensity, duration);
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
public void TriggerFallImpactShake(float fallHeight)
|
2026-03-26 20:27:19 +07:00
|
|
|
{
|
2026-03-27 12:08:16 +07:00
|
|
|
shakeManager.TriggerFallImpactShake(fallHeight);
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 12:08:16 +07:00
|
|
|
public Quaternion PlanarRotation => rotationHandler.PlanarRotation;
|
2026-03-26 20:27:19 +07:00
|
|
|
}
|
|
|
|
|
}
|