| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.ECSComponents |
| | 9 | | { |
| | 10 | | public class ECSTransformHandler : IECSComponentHandler<ECSTransform> |
| | 11 | | { |
| | 12 | | private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent; |
| | 13 | |
|
| 16 | 14 | | public ECSTransformHandler(IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent) |
| | 15 | | { |
| 16 | 16 | | ECSTransformUtils.orphanEntities = new KeyValueSet<IDCLEntity, ECSTransformUtils.OrphanEntity>(100); |
| 16 | 17 | | this.sbcInternalComponent = sbcInternalComponent; |
| 16 | 18 | | } |
| | 19 | |
|
| 2 | 20 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 21 | |
|
| | 22 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 23 | | { |
| 3 | 24 | | ECSTransformUtils.orphanEntities.Remove(entity); |
| | 25 | |
|
| | 26 | | // reset transform and re-parent to the scene |
| 3 | 27 | | entity.gameObject.transform.ResetLocalTRS(); |
| 3 | 28 | | ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 29 | |
|
| | 30 | | // if entity has any parent |
| 3 | 31 | | if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 32 | | { |
| | 33 | | // remove as child |
| 0 | 34 | | if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent)) |
| | 35 | | { |
| 0 | 36 | | parent.childrenId.Remove(entity.entityId); |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | entity.parentId = SpecialEntityId.SCENE_ROOT_ENTITY; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | // if entity has any children |
| 3 | 43 | | int childrenCount = entity.childrenId.Count; |
| | 44 | |
|
| 3 | 45 | | if (childrenCount > 0) |
| | 46 | | { |
| 4 | 47 | | for (int i = childrenCount - 1; i >= 0; i--) |
| | 48 | | { |
| 1 | 49 | | long childId = entity.childrenId[i]; |
| | 50 | |
|
| 1 | 51 | | if (!scene.entities.TryGetValue(childId, out IDCLEntity child)) |
| | 52 | | continue; |
| | 53 | |
|
| | 54 | | // re-parent child to the scene |
| 1 | 55 | | ECSTransformUtils.TrySetParent(scene, child, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 56 | |
|
| | 57 | | // add child as orphan |
| 1 | 58 | | ECSTransformUtils.orphanEntities[child] = new ECSTransformUtils.OrphanEntity(scene, child, child.par |
| | 59 | | } |
| | 60 | |
|
| 1 | 61 | | entity.childrenId.Clear(); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // Reset value in SBC internal component |
| 3 | 65 | | sbcInternalComponent.SetPosition(scene, entity, Vector3.zero, false); |
| 3 | 66 | | } |
| | 67 | |
|
| | 68 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, ECSTransform model) |
| | 69 | | { |
| 20 | 70 | | Transform transform = entity.gameObject.transform; |
| | 71 | |
|
| 20 | 72 | | bool positionChange = transform.localPosition != model.position; |
| 20 | 73 | | bool scaleChange = transform.localScale != model.scale; |
| 20 | 74 | | bool rotationChange = transform.localRotation != model.rotation; |
| | 75 | |
|
| 20 | 76 | | transform.localPosition = model.position; |
| 20 | 77 | | transform.localRotation = model.rotation; |
| 20 | 78 | | transform.localScale = model.scale; |
| | 79 | |
|
| 20 | 80 | | if (entity.parentId != model.parentId) |
| | 81 | | { |
| 11 | 82 | | Vector3 previousGlobalPosition = transform.position; |
| 11 | 83 | | ProcessNewParent(scene, entity, model.parentId); |
| | 84 | |
|
| | 85 | | // reparenting may end up changing the entity's position... |
| 11 | 86 | | if (!positionChange) |
| 10 | 87 | | positionChange = transform.position != previousGlobalPosition; |
| | 88 | | } |
| | 89 | |
|
| 20 | 90 | | if (positionChange) |
| 6 | 91 | | sbcInternalComponent.SetPosition(scene, entity, transform.position); |
| | 92 | |
|
| 20 | 93 | | if (scaleChange || rotationChange) |
| 2 | 94 | | sbcInternalComponent.OnTransformScaleRotationChanged(scene, entity); |
| | 95 | |
|
| | 96 | | // Same AvatarShape interpolation used at DCLTransform from SDK6 |
| 20 | 97 | | entity.OnTransformChange?.Invoke(model.position, model.rotation); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | private static void ProcessNewParent(IParcelScene scene, IDCLEntity entity, long parentId) |
| | 101 | | { |
| | 102 | | //check for cyclic parenting |
| 11 | 103 | | if (ECSTransformUtils.IsCircularParenting(scene, entity, parentId)) |
| | 104 | | { |
| 1 | 105 | | Debug.LogError($"cyclic parenting found for entity {entity.entityId} " + |
| | 106 | | $"parenting to {parentId} at scene {scene.sceneData.sceneNumber} ({scene.sceneData.basePo |
| | 107 | |
|
| 1 | 108 | | return; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // remove as child of previous parent |
| 10 | 112 | | if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 113 | | { |
| 3 | 114 | | if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent)) |
| | 115 | | { |
| 2 | 116 | | parent.childrenId.Remove(entity.entityId); |
| | 117 | | } |
| | 118 | |
|
| 3 | 119 | | ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 120 | | } |
| | 121 | |
|
| 10 | 122 | | entity.parentId = parentId; |
| | 123 | |
|
| | 124 | | // add as orphan so system can parent it |
| 10 | 125 | | ECSTransformUtils.orphanEntities[entity] = new ECSTransformUtils.OrphanEntity(scene, entity, parentId); |
| 10 | 126 | | } |
| | 127 | | } |
| | 128 | | } |