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