| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECS7.InternalComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.ECSComponents |
| | 11 | | { |
| | 12 | | public class AvatarAttachComponentHandler : IECSComponentHandler<PBAvatarAttach> |
| | 13 | | { |
| | 14 | | internal PBAvatarAttach prevModel = null; |
| | 15 | |
|
| | 16 | | internal IAvatarAnchorPoints anchorPoints; |
| | 17 | | internal AvatarAnchorPointIds anchorPointId; |
| | 18 | | private IDCLEntity entity; |
| | 19 | | private IParcelScene scene; |
| | 20 | |
|
| | 21 | | private Action componentUpdate = null; |
| | 22 | |
|
| | 23 | | internal readonly GetAnchorPointsHandler getAnchorPointsHandler; |
| | 24 | | private readonly IUpdateEventHandler updateEventHandler; |
| | 25 | | private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent; |
| | 26 | |
|
| | 27 | | private Vector2Int? currentCoords = null; |
| 5 | 28 | | private bool isInsideScene = true; |
| | 29 | |
|
| 5 | 30 | | public AvatarAttachComponentHandler(IUpdateEventHandler updateEventHandler, IInternalECSComponent<InternalSceneB |
| | 31 | | { |
| 5 | 32 | | this.updateEventHandler = updateEventHandler; |
| 5 | 33 | | this.sbcInternalComponent = sbcInternalComponent; |
| | 34 | |
|
| 5 | 35 | | getAnchorPointsHandler = new GetAnchorPointsHandler(); |
| 5 | 36 | | getAnchorPointsHandler.OnAvatarRemoved += Detach; |
| 5 | 37 | | } |
| | 38 | |
|
| | 39 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 40 | | { |
| 3 | 41 | | this.scene = scene; |
| 3 | 42 | | this.entity = entity; |
| 3 | 43 | | } |
| | 44 | |
|
| | 45 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 46 | | { |
| 0 | 47 | | Dispose(); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAvatarAttach model) |
| | 51 | | { |
| | 52 | | // If is the same model, we skip |
| 5 | 53 | | if (model == null || (prevModel != null && prevModel.AvatarId == model.AvatarId && model.AnchorPointId == pr |
| 1 | 54 | | return; |
| | 55 | |
|
| | 56 | | // Detach previous attachments |
| 4 | 57 | | Detach(); |
| 4 | 58 | | Attach(model.AvatarId, (AvatarAnchorPointIds)model.AnchorPointId); |
| | 59 | |
|
| 4 | 60 | | prevModel = model; |
| 4 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Dispose() |
| | 64 | | { |
| 5 | 65 | | Detach(); |
| 5 | 66 | | getAnchorPointsHandler.OnAvatarRemoved -= Detach; |
| 5 | 67 | | getAnchorPointsHandler.Dispose(); |
| 5 | 68 | | } |
| | 69 | |
|
| | 70 | | internal virtual void Detach() |
| | 71 | | { |
| 10 | 72 | | StopComponentUpdate(); |
| | 73 | |
|
| 10 | 74 | | if (entity != null) |
| 7 | 75 | | entity.gameObject.transform.localPosition = EnvironmentSettings.MORDOR; |
| | 76 | |
|
| 10 | 77 | | getAnchorPointsHandler.CancelCurrentSearch(); |
| 10 | 78 | | } |
| | 79 | |
|
| | 80 | | internal virtual void Attach(string avatarId, AvatarAnchorPointIds anchorPointId) |
| | 81 | | { |
| 4 | 82 | | getAnchorPointsHandler.SearchAnchorPoints(avatarId, anchorPoints => |
| | 83 | | { |
| 3 | 84 | | Attach(anchorPoints, anchorPointId); |
| 3 | 85 | | }, supportNullId: true); |
| 4 | 86 | | } |
| | 87 | |
|
| | 88 | | internal virtual void Attach(IAvatarAnchorPoints anchorPoints, AvatarAnchorPointIds anchorPointId) |
| | 89 | | { |
| 3 | 90 | | this.anchorPoints = anchorPoints; |
| 3 | 91 | | this.anchorPointId = anchorPointId; |
| | 92 | |
|
| 3 | 93 | | StartComponentUpdate(); |
| 3 | 94 | | } |
| | 95 | |
|
| | 96 | | internal void LateUpdate() |
| | 97 | | { |
| 1 | 98 | | if (entity == null || scene == null) |
| | 99 | | { |
| 0 | 100 | | StopComponentUpdate(); |
| 0 | 101 | | return; |
| | 102 | | } |
| | 103 | |
|
| 1 | 104 | | var anchorPoint = anchorPoints.GetTransform(anchorPointId); |
| | 105 | |
|
| 1 | 106 | | entity.gameObject.transform.position = anchorPoint.position; |
| 1 | 107 | | entity.gameObject.transform.rotation = anchorPoint.rotation; |
| 1 | 108 | | sbcInternalComponent.SetPosition(scene, entity, anchorPoint.position); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | private void StartComponentUpdate() |
| | 112 | | { |
| 3 | 113 | | if (componentUpdate != null) |
| 0 | 114 | | return; |
| | 115 | |
|
| 3 | 116 | | currentCoords = null; |
| 3 | 117 | | componentUpdate = LateUpdate; |
| 3 | 118 | | updateEventHandler?.AddListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate); |
| 3 | 119 | | } |
| | 120 | |
|
| | 121 | | private void StopComponentUpdate() |
| | 122 | | { |
| 10 | 123 | | if (componentUpdate == null) |
| 7 | 124 | | return; |
| | 125 | |
|
| 3 | 126 | | updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, componentUpdate); |
| 3 | 127 | | componentUpdate = null; |
| 3 | 128 | | } |
| | 129 | | } |
| | 130 | | } |