< 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:52
Uncovered lines:2
Coverable lines:54
Total lines:128
Line coverage:96.2% (52 of 54)
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%
ResetRaycastResponseTimestamp()0%110100%

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 = Decentraland.Common.Vector3;
 13
 14namespace DCLPlugins.ECSComponents.Raycast
 15{
 16    public class RaycastComponentHandler : IECSComponentHandler<PBRaycast>
 17    {
 18        // Lamport timestamp for raycast responses, we may want to move it to scene-level in the future
 19        private static int responseTimestamp = 0;
 20
 21        private IECSComponentWriter componentWriter;
 22        private LayerMask raycastLayerMaskTarget;
 23        private IInternalECSComponent<InternalColliders> physicsColliderComponent;
 24
 425        public RaycastComponentHandler(IECSComponentWriter componentWriter, IInternalECSComponent<InternalColliders> phy
 26        {
 427            this.componentWriter = componentWriter;
 428            this.physicsColliderComponent = physicsColliderComponent;
 29
 30            // Cast all layers except the OnPointerEvent one
 431            raycastLayerMaskTarget = ~(1 << PhysicsLayers.onPointerEventLayer);
 432        }
 33
 34        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 35        {
 036        }
 37
 38        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 39        {
 040        }
 41
 42        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBRaycast model)
 43        {
 644            var worldGridPosition = Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePositi
 645            UnityEngine.Vector3 origin = new UnityEngine.Vector3(model.Origin.X, model.Origin.Y, model.Origin.Z);
 646            Ray ray = new Ray();
 647            ray.origin = PositionUtils.WorldToUnityPosition(origin + worldGridPosition);
 648            ray.direction = new UnityEngine.Vector3(model.Direction.X, model.Direction.Y, model.Direction.Z);
 49
 650            PBRaycastResult result = new PBRaycastResult();
 651            result.Direction = model.Direction.Clone();
 652            result.Origin = model.Origin.Clone();
 653            result.Timestamp = responseTimestamp;
 654            responseTimestamp++;
 55
 656            RaycastHit[] hits = null;
 657            if (model.QueryType == RaycastQueryType.RqtHitFirst)
 58            {
 559                bool hasHit = Physics.Raycast(ray, out RaycastHit hit, model.MaxDistance, raycastLayerMaskTarget);
 560                if (hasHit)
 61                {
 462                    hits = new RaycastHit[1];
 463                    hits[0] = hit;
 64                }
 65            }
 166            else if (model.QueryType == RaycastQueryType.RqtQueryAll)
 67            {
 168                hits = Physics.RaycastAll(ray, model.MaxDistance, raycastLayerMaskTarget);
 69            }
 70
 671            if (hits != null)
 72            {
 2273                for (int i = 0; i < hits.Length; i++)
 74                {
 675                    IDCLEntity collisionEntity = null;
 76
 3277                    foreach (var currentEntity in scene.entities.Values)
 78                    {
 1379                        var collider = physicsColliderComponent.GetFor(scene, currentEntity);
 1380                        if (collider == null)
 81                            continue;
 82
 783                        if (collider.model.colliders.Contains(hits[i].collider))
 84                        {
 685                            collisionEntity = currentEntity;
 686                            break;
 87                        }
 88                    }
 89
 690                    DCL.ECSComponents.RaycastHit hit = new DCL.ECSComponents.RaycastHit();
 691                    hit.MeshName = hits[i].collider.name;
 692                    hit.Length = hits[i].distance;
 693                    hit.Origin = model.Origin.Clone();
 94
 695                    var worldPosition = DCL.WorldStateUtils.ConvertUnityToScenePosition(hits[i].point, scene);
 696                    hit.Position = new Vector3();
 697                    hit.Position.X = worldPosition.x;
 698                    hit.Position.Y = worldPosition.y;
 699                    hit.Position.Z = worldPosition.z;
 100
 6101                    hit.NormalHit = new Vector3();
 6102                    hit.NormalHit.X = hits[i].normal.x;
 6103                    hit.NormalHit.Y = hits[i].normal.y;
 6104                    hit.NormalHit.Z = hits[i].normal.z;
 105
 6106                    if (collisionEntity != null)
 107                    {
 6108                        hit.EntityId = (int)collisionEntity.entityId;
 109                    }
 110
 6111                    result.Hits.Add(hit);
 112                }
 113            }
 114
 6115            componentWriter.PutComponent(
 116                scene.sceneData.sceneNumber, entity.entityId,
 117                ComponentID.RAYCAST_RESULT,
 118                result,
 119                ECSComponentWriteType.WRITE_STATE_LOCALLY | ECSComponentWriteType.SEND_TO_SCENE
 120            );
 6121        }
 122
 123        public static void ResetRaycastResponseTimestamp()
 124        {
 1125            responseTimestamp = 0;
 1126        }
 127    }
 128}