< Summary

Class:DCL.ECSComponents.ECSTransformHandler
Assembly:DCL.ECSComponents.Transform.Handler
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Transform/Handler/ECSTransformHandler.cs
Covered lines:44
Uncovered lines:4
Coverable lines:48
Total lines:133
Line coverage:91.6% (44 of 48)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSTransformHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%6.176083.33%
OnComponentModelUpdated(...)0%330100%
ProcessNewParent(...)0%440100%
TryMoveCharacter(...)0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Transform/Handler/ECSTransformHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECSRuntime;
 3using DCL.Helpers;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL.ECSComponents
 8{
 9    public class ECSTransformHandler : IECSComponentHandler<ECSTransform>
 10    {
 11        private readonly IWorldState worldState;
 12        private readonly BaseVariable<Vector3> playerTeleportVariable;
 13
 3914        public ECSTransformHandler(IWorldState worldState, BaseVariable<Vector3> playerTeleportVariable)
 15        {
 3916            ECSTransformUtils.orphanEntities = new KeyValueSet<IDCLEntity, ECSTransformUtils.OrphanEntity>(100);
 3917            this.worldState = worldState;
 3918            this.playerTeleportVariable = playerTeleportVariable;
 3919        }
 20
 021        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 22
 23        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 24        {
 225            ECSTransformUtils.orphanEntities.Remove(entity);
 26
 27            // reset transform and re-parent to the scene
 228            entity.gameObject.transform.ResetLocalTRS();
 229            ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY);
 30
 31            // if entity has any parent
 232            if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 33            {
 34                // remove as child
 035                if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent))
 36                {
 037                    parent.childrenId.Remove(entity.entityId);
 38                }
 39
 040                entity.parentId = SpecialEntityId.SCENE_ROOT_ENTITY;
 41            }
 42
 43            // if entity has any children
 244            int childrenCount = entity.childrenId.Count;
 245            if (childrenCount > 0)
 46            {
 447                for (int i = childrenCount - 1; i >= 0; i--)
 48                {
 149                    long childId = entity.childrenId[i];
 50
 151                    if (!scene.entities.TryGetValue(childId, out IDCLEntity child))
 52                        continue;
 53
 54                    // re-parent child to the scene
 155                    ECSTransformUtils.TrySetParent(scene, child, SpecialEntityId.SCENE_ROOT_ENTITY);
 56
 57                    // add child as orphan
 158                    ECSTransformUtils.orphanEntities[child] = new ECSTransformUtils.OrphanEntity(scene, child, child.par
 59                }
 160                entity.childrenId.Clear();
 61            }
 262        }
 63
 64        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, ECSTransform model)
 65        {
 66            // From SDK `PLAYER_ENTITY` entity's transform can be modified to
 67            // move character across the scene
 1768            if (entity.entityId == SpecialEntityId.PLAYER_ENTITY)
 69            {
 370                TryMoveCharacter(scene, model.position, worldState, playerTeleportVariable);
 371                return;
 72            }
 73
 1474            Transform transform = entity.gameObject.transform;
 1475            transform.localPosition = model.position;
 1476            transform.localRotation = model.rotation;
 1477            transform.localScale = model.scale;
 78
 1479            if (entity.parentId != model.parentId)
 80            {
 981                ProcessNewParent(scene, entity, model.parentId);
 82            }
 1483        }
 84
 85        private static void ProcessNewParent(IParcelScene scene, IDCLEntity entity, long parentId)
 86        {
 87            //check for cyclic parenting
 988            if (ECSTransformUtils.IsCircularParenting(scene, entity, parentId))
 89            {
 190                Debug.LogError($"cyclic parenting found for entity {entity.entityId} " +
 91                               $"parenting to {parentId} at scene {scene.sceneData.id} ({scene.sceneData.basePosition})"
 192                return;
 93            }
 94
 95            // remove as child of previous parent
 896            if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 97            {
 298                if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent))
 99                {
 1100                    parent.childrenId.Remove(entity.entityId);
 101                }
 102            }
 103
 8104            entity.parentId = parentId;
 105
 106            // add as orphan so system can parent it
 8107            ECSTransformUtils.orphanEntities[entity] = new ECSTransformUtils.OrphanEntity(scene, entity, parentId);
 8108        }
 109
 110        private static bool TryMoveCharacter(IParcelScene scene, Vector3 localPosition,
 111            IWorldState worldState, BaseVariable<Vector3> playerTeleportVariable)
 112        {
 113            // If player is not at the scene that triggered this event
 114            // we'll ignore it
 3115            if (scene.sceneData.id != worldState.GetCurrentSceneId())
 116            {
 1117                return false;
 118            }
 119
 2120            Vector2Int targetCoords = scene.sceneData.basePosition + Utils.WorldToGridPosition(localPosition);
 121
 122            // If target coordinates are outside the scene we'll ignore it
 2123            if (!ECSTransformUtils.IsInsideSceneBoundaries(scene, targetCoords))
 124            {
 1125                return false;
 126            }
 127
 1128            playerTeleportVariable.Set(Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePos
 129                                       + localPosition);
 1130            return true;
 131        }
 132    }
 133}