< 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:46
Uncovered lines:3
Coverable lines:49
Total lines:134
Line coverage:93.8% (46 of 49)
Covered branches:0
Total branches:0

Metrics

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

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 IBaseVariable<Vector3> playerTeleportVariable;
 13
 3814        public ECSTransformHandler(IWorldState worldState, IBaseVariable<Vector3> playerTeleportVariable)
 15        {
 3816            ECSTransformUtils.orphanEntities = new KeyValueSet<IDCLEntity, ECSTransformUtils.OrphanEntity>(100);
 3817            this.worldState = worldState;
 3818            this.playerTeleportVariable = playerTeleportVariable;
 3819        }
 20
 621        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 22
 23        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 24        {
 725            ECSTransformUtils.orphanEntities.Remove(entity);
 26
 27            // reset transform and re-parent to the scene
 728            entity.gameObject.transform.ResetLocalTRS();
 729            ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY);
 30
 31            // if entity has any parent
 732            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
 744            int childrenCount = entity.childrenId.Count;
 745            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            }
 762        }
 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
 2768            if (entity.entityId == SpecialEntityId.PLAYER_ENTITY)
 69            {
 470                TryMoveCharacter(scene, model.position, worldState, playerTeleportVariable);
 471                return;
 72            }
 73
 2374            Transform transform = entity.gameObject.transform;
 2375            transform.localPosition = model.position;
 2376            transform.localRotation = model.rotation;
 2377            transform.localScale = model.scale;
 78
 2379            if (entity.parentId != model.parentId)
 80            {
 1181                ProcessNewParent(scene, entity, model.parentId);
 82            }
 2383        }
 84
 85        private static void ProcessNewParent(IParcelScene scene, IDCLEntity entity, long parentId)
 86        {
 87            //check for cyclic parenting
 1188            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.sceneNumber} ({scene.sceneData.basePo
 192                return;
 93            }
 94
 95            // remove as child of previous parent
 1096            if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY)
 97            {
 398                if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent))
 99                {
 2100                    parent.childrenId.Remove(entity.entityId);
 101                }
 3102                ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY);
 103            }
 104
 10105            entity.parentId = parentId;
 106
 107            // add as orphan so system can parent it
 10108            ECSTransformUtils.orphanEntities[entity] = new ECSTransformUtils.OrphanEntity(scene, entity, parentId);
 10109        }
 110
 111        private static bool TryMoveCharacter(IParcelScene scene, Vector3 localPosition,
 112            IWorldState worldState, IBaseVariable<Vector3> playerTeleportVariable)
 113        {
 114            // If player is not at the scene that triggered this event
 115            // we'll ignore it
 4116            if (scene.sceneData.sceneNumber != worldState.GetCurrentSceneNumber() && !scene.isPersistent)
 117            {
 1118                return false;
 119            }
 120
 3121            Vector2Int targetCoords = scene.sceneData.basePosition + Utils.WorldToGridPosition(localPosition);
 122
 123            // If target coordinates are outside the scene we'll ignore it
 3124            if (!ECSTransformUtils.IsInsideSceneBoundaries(scene, targetCoords))
 125            {
 1126                return false;
 127            }
 128
 2129            playerTeleportVariable.Set(Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePos
 130                                       + localPosition, true);
 2131            return true;
 132        }
 133    }
 134}