// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.Space)) { Vector2 mousePosition = cam.ScreenToWorldPoint(Input.mousePosition); //we want to only apply force if one active tentacle is out List <Vector2> activeTentacles = getActiveTentaclesExtendedPositions(); GameObject closestBlob = findClosestBlob(mousePosition); if (closestBlob == null) { return; } primaryBlob = closestBlob; List <Vector2> rays = collectAllRaysByBlob(closestBlob, mousePosition, 100); if (rays.Count > 0 && activeTentacles.Count > 0) { cam.GetComponent <CameraController>().blob = closestBlob; ClosestVector closestVector = findClosestPoint(rays, mousePosition, true); Vector2 closestPoint = new Vector2(closestVector.x, closestVector.y); List <GameObject> lineOfSightBlobs = subBlobs.Where(x => doesBlobHaveLineOfSight(x, closestPoint)).ToList <GameObject>(); applyForcesOnBlobs(closestBlob, lineOfSightBlobs, closestPoint); } //we need to make sure any blobs wont be cast if no space is available. //List<GameObject> otherBlobs = subBlobs.Where(x => !x.Equals(closestBlob) && doesBlobHaveLineOfSight(x, closestPoint)).ToList<GameObject>(); List <Vector2> raysForTentacles = collectAllRaysByBlob(closestBlob, mousePosition, 1); if (raysForTentacles.Count == 0) { return; } ClosestVector closestVectorForTentacles = findClosestPoint(raysForTentacles, mousePosition, false); if (closestVectorForTentacles.Found) { Vector2 closestPoint = new Vector2(closestVectorForTentacles.x, closestVectorForTentacles.y); closestBlob.GetComponent <Blob>().attach(closestPoint, maxSpeed); } } }
ClosestVector findClosestPoint(List <Vector2> rays, Vector2 mousePosition, bool ignoreTentacleLocation) { Vector2 closestRay = new Vector2(int.MaxValue, int.MaxValue); List <Vector2> activeTentacles = getActiveTentaclesExtendedPositions(); ClosestVector closest = new ClosestVector(false); foreach (Vector2 ray in rays) { if (!ignoreTentacleLocation && isThisPointTooCloseToAllTentacles(activeTentacles, ray)) { continue; } float currentClosestDistance = Vector2.Distance(closestRay, mousePosition); float proposedDistance = Vector2.Distance(mousePosition, ray); if (proposedDistance < currentClosestDistance) { closestRay = ray; closest = new ClosestVector(ray.x, ray.y); } } return(closest); }