< Summary

Class:DCLPlugins.ECSComponents.Raycast.RaycastComponentHandler
Assembly:DCLPlugins.ECSComponents.Raycast.Handler
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Raycast/Handler/RaycastHandler.cs
Covered lines:50
Uncovered lines:2
Coverable lines:52
Total lines:119
Line coverage:96.1% (50 of 52)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RaycastComponentHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%2100%
OnComponentModelUpdated(...)0%10100100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Raycast/Handler/RaycastHandler.cs

#LineLine coverage
 1using DCL.Configuration;
 2using DCL.Controllers;
 3using DCL.ECS7;
 4using DCL.ECS7.InternalComponents;
 5using DCL.ECSComponents;
 6using DCL.ECSRuntime;
 7using DCL.Helpers;
 8using DCL.Models;
 9using UnityEngine;
 10using Ray = UnityEngine.Ray;
 11using RaycastHit = UnityEngine.RaycastHit;
 12using Vector3 = DCL.ECSComponents.Vector3;
 13
 14namespace DCLPlugins.ECSComponents.Raycast
 15{
 16    public class RaycastComponentHandler : IECSComponentHandler<PBRaycast>
 17    {
 18        private IECSComponentWriter componentWriter;
 19        private LayerMask raycastLayerMaskTarget;
 20        private IInternalECSComponent<InternalColliders> physicsColliderComponent;
 21
 322        public RaycastComponentHandler(IECSComponentWriter componentWriter, IInternalECSComponent<InternalColliders> phy
 23        {
 324            this.componentWriter = componentWriter;
 325            this.physicsColliderComponent = physicsColliderComponent;
 26
 27            // Cast all layers except the OnPointerEvent one
 328            raycastLayerMaskTarget = ~(1 << PhysicsLayers.onPointerEventLayer);
 329        }
 30
 31        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 32        {
 033        }
 34
 35        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 36        {
 037        }
 38
 39        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBRaycast model)
 40        {
 341            var worldGridPosition = Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositi
 342            UnityEngine.Vector3 origin = new UnityEngine.Vector3(model.Origin.X, model.Origin.Y, model.Origin.Z);
 343            Ray ray = new Ray();
 344            ray.origin = PositionUtils.WorldToUnityPosition(origin + worldGridPosition);
 345            ray.direction = new UnityEngine.Vector3(model.Direction.X, model.Direction.Y, model.Direction.Z);
 46
 347            PBRaycastResult result = new PBRaycastResult();
 348            result.Direction = model.Direction.Clone();
 349            result.Origin = model.Origin.Clone();
 350            result.Timestamp = model.Timestamp;
 51
 352            RaycastHit[] hits = null;
 353            if (model.QueryType == RaycastQueryType.HitFirst)
 54            {
 255                bool hasHit = Physics.Raycast(ray, out RaycastHit hit, model.MaxDistance, raycastLayerMaskTarget);
 256                if (hasHit)
 57                {
 158                    hits = new RaycastHit[1];
 159                    hits[0] = hit;
 60                }
 161            }
 162            else if (model.QueryType == RaycastQueryType.QueryAll)
 63            {
 164                hits = Physics.RaycastAll(ray, model.MaxDistance, raycastLayerMaskTarget);
 65            }
 66
 367            if (hits != null)
 68            {
 1069                for (int i = 0; i < hits.Length; i++)
 70                {
 371                    IDCLEntity collisionEntity = null;
 72
 1773                    foreach (var currentEntity in scene.entities.Values)
 74                    {
 775                        var collider = physicsColliderComponent.GetFor(scene, currentEntity);
 776                        if (collider == null)
 77                            continue;
 78
 479                        if (collider.model.colliders.Contains(hits[i].collider))
 80                        {
 381                            collisionEntity = currentEntity;
 382                            break;
 83                        }
 84                    }
 85
 386                    DCL.ECSComponents.RaycastHit hit = new DCL.ECSComponents.RaycastHit();
 387                    hit.MeshName = hits[i].collider.name;
 388                    hit.Length = hits[i].distance;
 389                    hit.Origin = model.Origin.Clone();
 90
 391                    var worldPosition = PositionUtils.UnityToWorldPosition(hits[i].point - worldGridPosition);
 392                    hit.Position = new Vector3();
 393                    hit.Position.X = worldPosition.x;
 394                    hit.Position.Y = worldPosition.y;
 395                    hit.Position.Z = worldPosition.z;
 96
 397                    hit.NormalHit = new Vector3();
 398                    hit.NormalHit.X = hits[i].normal.x;
 399                    hit.NormalHit.Y = hits[i].normal.y;
 3100                    hit.NormalHit.Z = hits[i].normal.z;
 101
 3102                    if (collisionEntity != null)
 103                    {
 3104                        hit.EntityId = (int)collisionEntity.entityId;
 105                    }
 106
 3107                    result.Hits.Add(hit);
 108                }
 109            }
 110
 3111            componentWriter.PutComponent(
 112                scene.sceneData.id, entity.entityId,
 113                ComponentID.RAYCAST_RESULT,
 114                result,
 115                ECSComponentWriteType.WRITE_STATE_LOCALLY | ECSComponentWriteType.SEND_TO_SCENE
 116            );
 3117        }
 118    }
 119}