104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using System;
|
|
using Code.Infrastructure.Camera;
|
|
using Code.Infrastructure.Camera.CameraStates;
|
|
using Code.Infrastructure.Input;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Game.OfficeGame.BattleMode
|
|
{
|
|
[Infrastructure.Generators.State(typeof(CameraMode), typeof(CameraStateMachineContext), ManualInherit = true)]
|
|
public partial class CameraBattleModeState : BaseCameraState
|
|
{
|
|
private Vector3 _rotationOffset = new(30f, 0, 0);
|
|
private int _cameraZoneMask = LayerMask.GetMask("CameraZone");
|
|
|
|
public CameraBattleModeState(CameraStateMachineContext context) : base(context)
|
|
{
|
|
}
|
|
public override CameraMode MyState => CameraMode.BattleMode;
|
|
public override UniTask EnterState()
|
|
{
|
|
Context.InputService.BindActionPerformed(
|
|
(int)ActionMappings.BattleMode,
|
|
(int)BattleModeActions.Movement,
|
|
SetMovement);
|
|
Context.InputService.BindActionCancelled(
|
|
(int)ActionMappings.BattleMode,
|
|
(int)BattleModeActions.Movement,
|
|
ResetMovement);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override UniTask ExitState()
|
|
{
|
|
Context.InputService.UnbindActionPerformed(
|
|
(int)ActionMappings.BattleMode,
|
|
(int)BattleModeActions.Movement,
|
|
SetMovement);
|
|
Context.InputService.UnbindActionCancelled(
|
|
(int)ActionMappings.BattleMode,
|
|
(int)BattleModeActions.Movement,
|
|
ResetMovement);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
private Vector2 _movement;
|
|
private float _speed = 5f; // ToConf
|
|
private void SetMovement(InputAction.CallbackContext obj)
|
|
{
|
|
_movement = obj.ReadValue<Vector2>();
|
|
}
|
|
|
|
private void ResetMovement(InputAction.CallbackContext obj)
|
|
{
|
|
_movement = Vector2.zero;
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
|
|
}
|
|
|
|
private const float ZoneSearchRadius = 3f;
|
|
private static readonly Collider[] _zoneBuffer = new Collider[8];
|
|
|
|
public override void LateUpdate()
|
|
{
|
|
var camTransform = Context.cameraLoadedData.Camera.transform;
|
|
var desired = camTransform.position
|
|
+ new Vector3(_movement.x, 0 , _movement.y) * (Time.deltaTime * _speed);
|
|
|
|
int count = Physics.OverlapSphereNonAlloc(
|
|
camTransform.position,
|
|
ZoneSearchRadius,
|
|
_zoneBuffer,
|
|
_cameraZoneMask,
|
|
QueryTriggerInteraction.Collide);
|
|
|
|
if (count == 0)
|
|
{
|
|
if (Physics.CheckSphere(desired, 0.01f, _cameraZoneMask, QueryTriggerInteraction.Collide))
|
|
camTransform.position = desired;
|
|
return;
|
|
}
|
|
|
|
var best = camTransform.position;
|
|
var bestSqr = float.MaxValue;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var p = _zoneBuffer[i].ClosestPoint(desired);
|
|
var sqr = (p - desired).sqrMagnitude;
|
|
if (sqr < bestSqr)
|
|
{
|
|
bestSqr = sqr;
|
|
best = p;
|
|
}
|
|
}
|
|
|
|
camTransform.position = best;
|
|
}
|
|
}
|
|
} |