| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Helpers |
| | 6 | | { |
| | 7 | | public class RaycastHandler : IRaycastHandler |
| | 8 | | { |
| | 9 | | private void SetHitInfo(ref HitInfo hitInfo, RaycastHit hit, IParcelScene scene) |
| | 10 | | { |
| 26 | 11 | | hitInfo.point = WorldStateUtils.ConvertUnityToScenePosition(hit.point, scene); |
| 26 | 12 | | hitInfo.distance = hit.distance; |
| 26 | 13 | | hitInfo.normal = hit.normal; |
| 26 | 14 | | hitInfo.collider = hit.collider; |
| 26 | 15 | | } |
| | 16 | |
|
| | 17 | | private bool Raycast(Ray ray, out HitInfo hitInfo, float distance, LayerMask layerMask, IParcelScene scene) |
| | 18 | | { |
| | 19 | | RaycastHit hit; |
| 28 | 20 | | hitInfo = new HitInfo(); |
| | 21 | |
|
| 28 | 22 | | if (Physics.Raycast(ray, out hit, distance, layerMask)) |
| | 23 | | { |
| 26 | 24 | | SetHitInfo(ref hitInfo, hit, scene); |
| 26 | 25 | | return true; |
| | 26 | | } |
| | 27 | |
|
| 2 | 28 | | return false; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public RaycastResultInfo Raycast(Ray ray, float distance, LayerMask layerMaskTarget, IParcelScene scene) |
| | 32 | | { |
| 28 | 33 | | RaycastResultInfo raycastInfo = new RaycastResultInfo(); |
| | 34 | |
|
| 28 | 35 | | raycastInfo.ray = ray; |
| 28 | 36 | | raycastInfo.hitInfo = new RaycastHitInfo(); |
| | 37 | |
|
| 28 | 38 | | if (Raycast(raycastInfo.ray, out raycastInfo.hitInfo.hit, distance, layerMaskTarget, scene)) |
| | 39 | | { |
| 26 | 40 | | SetRaycastInfoData(ref raycastInfo.hitInfo, scene); |
| | 41 | | } |
| | 42 | |
|
| 28 | 43 | | return raycastInfo; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public RaycastResultInfoList RaycastAll(Ray ray, float distance, LayerMask layerMaskTarget, IParcelScene scene) |
| | 47 | | { |
| 0 | 48 | | RaycastResultInfoList raycastInfo = new RaycastResultInfoList(); |
| 0 | 49 | | raycastInfo.ray = ray; |
| | 50 | |
|
| 0 | 51 | | RaycastHit[] result = Physics.RaycastAll(raycastInfo.ray, distance, layerMaskTarget); |
| | 52 | |
|
| 0 | 53 | | if (result != null) |
| | 54 | | { |
| 0 | 55 | | raycastInfo.hitInfo = new RaycastHitInfo[result.Length]; |
| 0 | 56 | | int count = result.Length; |
| | 57 | |
|
| 0 | 58 | | for (int i = 0; i < count; i++) |
| | 59 | | { |
| 0 | 60 | | raycastInfo.hitInfo[i] = new RaycastHitInfo(); |
| 0 | 61 | | raycastInfo.hitInfo[i].hit = new HitInfo(); |
| 0 | 62 | | SetHitInfo(ref raycastInfo.hitInfo[i].hit, result[i], scene); |
| 0 | 63 | | SetRaycastInfoData(ref raycastInfo.hitInfo[i], scene); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | return raycastInfo; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | private void SetRaycastInfoData(ref RaycastHitInfo hitInfo, IParcelScene scene) |
| | 71 | | { |
| 26 | 72 | | if (hitInfo.hit.collider == null) |
| 0 | 73 | | return; |
| | 74 | |
|
| 26 | 75 | | if (!CollidersManager.i.GetColliderInfo(hitInfo.hit.collider, out ColliderInfo info)) |
| 0 | 76 | | return; |
| | 77 | |
|
| 26 | 78 | | if (scene != null) |
| 26 | 79 | | hitInfo.isValid = (info.scene == scene) || (scene is GlobalScene globalScene && globalScene.isPortableEx |
| 0 | 80 | | else if (scene == null && WorldStateUtils.IsCharacterInsideScene(info.scene)) |
| 0 | 81 | | hitInfo.isValid = true; |
| 0 | 82 | | } |
| | 83 | | } |
| | 84 | | } |