| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 4 | | using DCLServices.MapRendererV2.Culling; |
| | 5 | | using System; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCLServices.MapRendererV2.MapLayers.PlayerMarker |
| | 10 | | { |
| | 11 | | internal class PlayerMarkerController : MapLayerControllerBase, IMapLayerController<PlayerMarkerParameter>, IZoomSca |
| | 12 | | { |
| | 13 | | internal delegate IPlayerMarker PlayerMarkerBuilder(Transform parent); |
| | 14 | |
|
| | 15 | | private readonly PlayerMarkerBuilder builder; |
| | 16 | | private readonly BaseVariable<Vector3> playerWorldPosition; |
| | 17 | | private readonly Vector3Variable playerRotation; |
| | 18 | |
|
| | 19 | | private IPlayerMarker playerMarker; |
| | 20 | |
|
| | 21 | | public PlayerMarkerController( |
| | 22 | | PlayerMarkerBuilder builder, |
| | 23 | | BaseVariable<Vector3> playerWorldPosition, Vector3Variable playerRotation, |
| | 24 | | Transform instantiationParent, ICoordsUtils coordsUtils, IMapCullingController cullingController) |
| 96 | 25 | | : base(instantiationParent, coordsUtils, cullingController) |
| | 26 | | { |
| 96 | 27 | | this.builder = builder; |
| 96 | 28 | | this.playerWorldPosition = playerWorldPosition; |
| 96 | 29 | | this.playerRotation = playerRotation; |
| 96 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Initialize() |
| | 33 | | { |
| 96 | 34 | | playerMarker = builder(instantiationParent); |
| 96 | 35 | | } |
| | 36 | |
|
| | 37 | | public UniTask Enable(CancellationToken cancellationToken) |
| | 38 | | { |
| 0 | 39 | | playerMarker.SetActive(true); |
| | 40 | |
|
| 0 | 41 | | SetRotation(); |
| 0 | 42 | | SetPosition(); |
| | 43 | |
|
| 0 | 44 | | playerWorldPosition.OnChange += OnPlayerWorldPositionChange; |
| 0 | 45 | | playerRotation.OnChange += OnPlayerRotationChange; |
| 0 | 46 | | return UniTask.CompletedTask; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public UniTask Disable(CancellationToken cancellationToken) |
| | 50 | | { |
| 88 | 51 | | playerMarker.SetActive(false); |
| | 52 | |
|
| 88 | 53 | | playerWorldPosition.OnChange -= OnPlayerWorldPositionChange; |
| 88 | 54 | | playerRotation.OnChange -= OnPlayerRotationChange; |
| 88 | 55 | | return UniTask.CompletedTask; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public void SetParameter(PlayerMarkerParameter param) |
| | 59 | | { |
| 0 | 60 | | playerMarker?.SetBackgroundVisibility(param.BackgroundIsActive); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | private void SetPosition() |
| | 64 | | { |
| 0 | 65 | | var gridPosition = Utils.WorldToGridPositionUnclamped(playerWorldPosition.Get()); |
| 0 | 66 | | playerMarker.SetPosition(coordsUtils.PivotPosition(playerMarker, coordsUtils.CoordsToPositionWithOffset(grid |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private void OnPlayerRotationChange(Vector3 current, Vector3 previous) |
| | 70 | | { |
| 0 | 71 | | SetRotation(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void SetRotation() |
| | 75 | | { |
| 0 | 76 | | var playerRot = playerRotation.Get(); |
| 0 | 77 | | var markerRot = Quaternion.Euler(0, 0, Mathf.Atan2(-playerRot.x, playerRot.z) * Mathf.Rad2Deg); |
| 0 | 78 | | playerMarker.SetRotation(markerRot); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void ApplyCameraZoom(float baseZoom, float zoom) |
| | 82 | | { |
| 0 | 83 | | playerMarker.SetZoom(baseZoom, zoom); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public void ResetToBaseScale() |
| | 87 | | { |
| 0 | 88 | | playerMarker.ResetToBaseScale(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnPlayerWorldPositionChange(Vector3 current, Vector3 previous) |
| | 92 | | { |
| 0 | 93 | | SetPosition(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | protected override void DisposeImpl() |
| | 97 | | { |
| 6 | 98 | | playerMarker?.Dispose(); |
| 6 | 99 | | } |
| | 100 | | } |
| | 101 | | } |