internal Path GenerateNewPath_Sync(float startTime, Vector2 origin, Vector2 target, List <Ray2D> rayList, int maxLevelDepth, LayerMask rayMask) { Path newPath = new Path(startTime); rayList = new List <Ray2D>(); if (hybrid && Vector2.SqrMagnitude(HelperClass.V3toV2(target) - HelperClass.V3toV2(origin)) < hybridDistance * hybridDistance) { Queue <Vector2> nodes = new Queue <Vector2>(); rayList.Add(new Ray2D(origin, target)); nodes.Enqueue(target); newPath.Finalize(nodes); } else { RayFan rayFan = new RayFan(maxLevelDepth, this, origin, (target - origin).normalized, new List <Vector2>(), rayMask); RayFanResult res = rayFan.Trigger(); if (res.result == RayFanResult.resultType.Hit) { Queue <Vector2> nodes = new Queue <Vector2>(); res.pathSoFar.ForEach((x) => { nodes.Enqueue(x); }); newPath.Finalize(nodes); } } return(newPath); }
public RayFanResult NextRay() { if (numRays > maxRays) { // oh f**k go back return(new RayFanResult(null, null, RayFanResult.resultType.NoMoreRays)); } float dir = parent.raySpacing * (numRays / 2); if (numRays % 2 == 0) { dir = -dir; } numRays++; Vector2 rayDir = HelperClass.RotateAroundAxis(straightDirection, Vector2.zero, dir); Ray2D ray = new Ray2D(origin, rayDir); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 20f, rayMask); Ray2D playerRay = new Ray2D(hit.point, HelperClass.V3toV2(parent.target.position) - hit.point); RaycastHit2D playerHit = Physics2D.Raycast(playerRay.origin, playerRay.direction, 200f, rayMask); parent.rayList.Add(ray); parent.rayList.Add(playerRay); List <Vector2> path = new List <Vector2>(); pathYet.ForEach((x) => { path.Add(x); }); path.Add(hit.point); NextRayFanData data = new NextRayFanData(depthLeft - 1, parent, origin, hit.normal, path, rayMask); RayFanResult.resultType hitPlayer = RayFanResult.resultType.NoHit; if (playerHit.collider.gameObject.tag == "Player") { hitPlayer = RayFanResult.resultType.Hit; path.Add(HelperClass.V3toV2(parent.target.position)); } RayFanResult res = new RayFanResult(path, data, hitPlayer); return(null); }
public RayFanResult Trigger() { List <NextRayFanData> subFans = new List <NextRayFanData>(); RayFanResult lastReturn = new RayFanResult(null, null, RayFanResult.resultType.NoHit); while (lastReturn.result == RayFanResult.resultType.NoHit) { RayFanResult res = NextRay(); if (res != null && res.next != null) { subFans.Add(res.next); } } if (lastReturn.result == RayFanResult.resultType.NoMoreRays) { if (depthLeft > 1) { foreach (NextRayFanData fan in subFans) { RayFanResult res = fan.Generate().Trigger(); if (res.result == RayFanResult.resultType.Hit) //we found a path { return(res); } else //RayFanResult.resultType.NoMoreRays { // we couldnt find a path originating from this ray } } } //no rays gave good results } return(null); }