| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Models; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | internal class AvatarAttachHandler : IDisposable |
| | 12 | | { |
| | 13 | | private const float BOUNDARIES_CHECK_INTERVAL = 5; |
| | 14 | |
|
| 7 | 15 | | public AvatarAttachComponent.Model model { internal set; get; } = new AvatarAttachComponent.Model(); |
| 0 | 16 | | public IParcelScene scene { private set; get; } |
| 0 | 17 | | public IDCLEntity entity { private set; get; } |
| | 18 | |
|
| | 19 | | private AvatarAttachComponent.Model prevModel = null; |
| | 20 | |
|
| | 21 | | private IAvatarAnchorPoints anchorPoints; |
| | 22 | | private AvatarAnchorPointIds anchorPointId; |
| | 23 | |
|
| | 24 | | private Coroutine componentUpdate = null; |
| | 25 | |
|
| 7 | 26 | | private readonly GetAnchorPointsHandler getAnchorPointsHandler = new GetAnchorPointsHandler(); |
| 1 | 27 | | private ISceneBoundsChecker sceneBoundsChecker => Environment.i?.world?.sceneBoundsChecker; |
| | 28 | |
|
| | 29 | | private Vector2Int? currentCoords = null; |
| 7 | 30 | | private bool isInsideScene = true; |
| | 31 | | private float lastBoundariesCheckTime = 0; |
| | 32 | |
|
| | 33 | | public void Initialize(IParcelScene scene, IDCLEntity entity) |
| | 34 | | { |
| 2 | 35 | | this.scene = scene; |
| 2 | 36 | | this.entity = entity; |
| 2 | 37 | | getAnchorPointsHandler.OnAvatarRemoved += Detach; |
| 2 | 38 | | } |
| | 39 | |
|
| | 40 | | public void OnModelUpdated(string json) |
| | 41 | | { |
| 0 | 42 | | OnModelUpdated(model.GetDataFromJSON(json) as AvatarAttachComponent.Model); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public void OnModelUpdated(AvatarAttachComponent.Model newModel) |
| | 46 | | { |
| 7 | 47 | | prevModel = model; |
| 7 | 48 | | model = newModel; |
| | 49 | |
|
| 7 | 50 | | if (model == null) |
| | 51 | | { |
| 0 | 52 | | return; |
| | 53 | | } |
| | 54 | |
|
| 7 | 55 | | if (prevModel.avatarId != model.avatarId) |
| | 56 | | { |
| 6 | 57 | | Detach(); |
| | 58 | |
|
| 6 | 59 | | if (!string.IsNullOrEmpty(model.avatarId)) |
| | 60 | | { |
| 5 | 61 | | Attach(model.avatarId, (AvatarAnchorPointIds)model.anchorPointId); |
| | 62 | | } |
| | 63 | | } |
| 7 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Dispose() |
| | 67 | | { |
| 7 | 68 | | Detach(); |
| 7 | 69 | | getAnchorPointsHandler.OnAvatarRemoved -= Detach; |
| 7 | 70 | | getAnchorPointsHandler.Dispose(); |
| 7 | 71 | | } |
| | 72 | |
|
| | 73 | | internal virtual void Detach() |
| | 74 | | { |
| 13 | 75 | | if (componentUpdate != null) |
| | 76 | | { |
| 2 | 77 | | CoroutineStarter.Stop(componentUpdate); |
| 2 | 78 | | componentUpdate = null; |
| | 79 | | } |
| | 80 | |
|
| 13 | 81 | | if (entity != null) |
| | 82 | | { |
| 4 | 83 | | entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR; |
| | 84 | | } |
| | 85 | |
|
| 13 | 86 | | getAnchorPointsHandler.CancelCurrentSearch(); |
| 13 | 87 | | } |
| | 88 | |
|
| | 89 | | internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId) |
| | 90 | | { |
| 5 | 91 | | getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints => |
| | 92 | | { |
| 4 | 93 | | Attach(anchorPoints, anchorPointId); |
| 4 | 94 | | }); |
| 5 | 95 | | } |
| | 96 | |
|
| | 97 | | internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId) |
| | 98 | | { |
| 4 | 99 | | this.anchorPoints = anchorPoints; |
| 4 | 100 | | this.anchorPointId = anchorPointId; |
| | 101 | |
|
| 4 | 102 | | if (componentUpdate == null) |
| | 103 | | { |
| 4 | 104 | | componentUpdate = CoroutineStarter.Start(ComponentUpdate()); |
| | 105 | | } |
| 4 | 106 | | } |
| | 107 | |
|
| | 108 | | IEnumerator ComponentUpdate() |
| | 109 | | { |
| 4 | 110 | | currentCoords = null; |
| | 111 | |
|
| 0 | 112 | | while (true) |
| | 113 | | { |
| 4 | 114 | | if (entity == null || scene == null) |
| | 115 | | { |
| 2 | 116 | | componentUpdate = null; |
| 2 | 117 | | yield break; |
| | 118 | | } |
| | 119 | |
|
| 2 | 120 | | var anchorPoint = anchorPoints.GetTransform(anchorPointId); |
| | 121 | |
|
| 2 | 122 | | if (IsInsideScene(CommonScriptableObjects.worldOffset + anchorPoint.position)) |
| | 123 | | { |
| 1 | 124 | | entity.gameObject.transform.position = anchorPoint.position; |
| 1 | 125 | | entity.gameObject.transform.rotation = anchorPoint.rotation; |
| | 126 | |
|
| 1 | 127 | | if (Time.unscaledTime - lastBoundariesCheckTime > BOUNDARIES_CHECK_INTERVAL) |
| | 128 | | { |
| 1 | 129 | | CheckSceneBoundaries(entity); |
| | 130 | | } |
| 1 | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 1 | 134 | | entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR; |
| | 135 | | } |
| | 136 | |
|
| 2 | 137 | | yield return null; |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| | 141 | | internal virtual bool IsInsideScene(Vector3 position) |
| | 142 | | { |
| 0 | 143 | | bool result = isInsideScene; |
| 0 | 144 | | Vector2Int coords = Utils.WorldToGridPosition(position); |
| 0 | 145 | | if (currentCoords == null || currentCoords != coords) |
| | 146 | | { |
| 0 | 147 | | result = scene.IsInsideSceneBoundaries(coords, position.y); |
| | 148 | | } |
| 0 | 149 | | currentCoords = coords; |
| 0 | 150 | | isInsideScene = result; |
| 0 | 151 | | return result; |
| | 152 | | } |
| | 153 | |
|
| | 154 | | private void CheckSceneBoundaries(IDCLEntity entity) |
| | 155 | | { |
| 1 | 156 | | sceneBoundsChecker?.AddEntityToBeChecked(entity); |
| 1 | 157 | | lastBoundariesCheckTime = Time.unscaledTime; |
| 1 | 158 | | } |
| | 159 | | } |
| | 160 | | } |