using System; using System.Linq; using Code.Infrastructure.Camera; using Code.OfficeGame.Player; using Cysharp.Threading.Tasks; using Reflex.Attributes; using UnityEngine; namespace Code.Game.OfficeGame.Location { public class PlayerLocationChecker : MonoBehaviour { [Inject] private CameraStateMachine _cameraStateMachine; private LocationSegment _currentSegment; private LocationSegment _previousSegment; private async void OnTriggerEnter(Collider other) { try { if (!other.TryGetComponent(out LocationSegmentTrigger locationSegmentTrigger)) return; Debug.Log("Entered " + locationSegmentTrigger.segment.locationID); _previousSegment = _currentSegment; _currentSegment = locationSegmentTrigger.segment; if (_previousSegment == null || locationSegmentTrigger.segment == null) return; var point = locationSegmentTrigger.segment.transitionPoints .SingleOrDefault(x => x.locationID == _previousSegment.locationID); if (point == null) return; _cameraStateMachine.SwitchState(CameraMode.LocationTransition).Forget(); ((LocationTransitionState)_cameraStateMachine.CurrentBaseState).SetTarget(point.transform); await _cameraStateMachine.SwitchState(CameraMode.OpenMovement); } catch (Exception e) { Debug.LogException(e); } } private async void OnTriggerExit(Collider other) { try { if (!other.TryGetComponent(out LocationSegmentTrigger locationSegmentTrigger)) return; Debug.Log("Exited " + locationSegmentTrigger.segment.locationID); if (_currentSegment != locationSegmentTrigger.segment) return; (_previousSegment, _currentSegment) = (_currentSegment, _previousSegment); _currentSegment = locationSegmentTrigger.segment; if (_previousSegment == null || locationSegmentTrigger.segment == null) return; var point = locationSegmentTrigger.segment.transitionPoints .SingleOrDefault(x => x.locationID == _previousSegment.locationID); if (point == null) return; _cameraStateMachine.SwitchState(CameraMode.LocationTransition).Forget(); ((LocationTransitionState)_cameraStateMachine.CurrentBaseState).SetTarget(point.transform); await _cameraStateMachine.SwitchState(CameraMode.OpenMovement); } catch (Exception e) { Debug.LogException(e); } } } }