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