85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
using System;
|
|
using Code.Infrastructure.Camera;
|
|
using Code.Infrastructure.Camera.CameraStates;
|
|
using Code.Infrastructure.Generators;
|
|
using Code.Infrastructure.Input;
|
|
using Cysharp.Threading.Tasks;
|
|
using R3;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Game.Gameplay.Player
|
|
{
|
|
[State(typeof(CameraMode),typeof(CameraStateMachineContext), ManualInherit = true)]
|
|
public partial class CleanerCameraState : BaseCameraState
|
|
{
|
|
public void SetTarget(Transform target)
|
|
{
|
|
_target = target;
|
|
Context.cameraLoadedData.Camera.transform.position = _target.position;
|
|
Context.cameraLoadedData.Camera.transform.rotation = _target.rotation;
|
|
}
|
|
|
|
|
|
private Transform _target;
|
|
private Vector2 _mouseDelta;
|
|
private Vector2 _rotation = Vector2.zero;
|
|
|
|
private float _sensitivityX = 0.1f;
|
|
private float _sensitivityY = 0.1f;
|
|
private float _slideStupidity = 1f;
|
|
private bool _isSliding;
|
|
|
|
public CleanerCameraState(CameraStateMachineContext context) : base(context)
|
|
{
|
|
}
|
|
|
|
private void LookCancelled(InputAction.CallbackContext obj) => _mouseDelta = Vector2.zero;
|
|
private void LookPerformed(InputAction.CallbackContext obj) => _mouseDelta = obj.ReadValue<Vector2>();
|
|
|
|
public override CameraMode MyState => CameraMode.OpenMovement;
|
|
|
|
public override UniTask EnterState()
|
|
{
|
|
Context.InputService.BindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookPerformed);
|
|
Context.InputService.BindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookCancelled);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override UniTask ExitState()
|
|
{
|
|
Context.InputService.UnbindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookPerformed);
|
|
Context.InputService.UnbindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookCancelled);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
}
|
|
|
|
public override void LateUpdate()
|
|
{
|
|
if (_target is null) return;
|
|
|
|
var camTransform = Context.cameraLoadedData.Camera.transform;
|
|
|
|
camTransform.position = _target.position;
|
|
|
|
var mouseDeltaX = _mouseDelta.x * _sensitivityX;
|
|
var mouseDeltaY = _mouseDelta.y * _sensitivityY;
|
|
|
|
if (_isSliding)
|
|
{
|
|
mouseDeltaX = Mathf.Clamp(mouseDeltaX, -_slideStupidity, _slideStupidity);
|
|
mouseDeltaY = Mathf.Clamp(mouseDeltaY, -_slideStupidity, _slideStupidity);
|
|
}
|
|
|
|
_rotation.x += mouseDeltaX;
|
|
_rotation.y -= mouseDeltaY;
|
|
|
|
_rotation.y = Mathf.Clamp(_rotation.y, -90f, 90f);
|
|
|
|
camTransform.localRotation = Quaternion.Euler(_rotation.y, _rotation.x, 0f);
|
|
}
|
|
}
|
|
} |