public static EVector2 Normalized(EVector2 a) { float magn = Mathf.Sqrt (a.x * a.x + a.y * a.y); if (magn > 1E-05f) { return a / magn; } else { return EVector2.zero; } }
public static float SqrMagnitude (EVector2 a) { return a.x * a.x + a.y * a.y; }
IEnumerator SeparationCalc(List<EVector2> allCreeps, System.Action <EVector2> SeparationCallback,EVector2 position) { int j = 0; EVector2 separationForce = new EVector2(0,0); EVector2 averageDirection = new EVector2(0,0); EVector2 distance = new EVector2(0,0); if(allCreeps != null){ for (int i = 0; i < allCreeps.Count - 1; i++) { if(allCreeps[i] != null){ distance = position - allCreeps[i]; if (Mathf.Sqrt((distance.x * distance.x)+(distance.y * distance.y)) < 0.5f && allCreeps[i] != position) { j++; separationForce += position - allCreeps[i]; separationForce = EVector2.Normalized(separationForce); separationForce = separationForce * (4f); averageDirection = averageDirection + separationForce; } } } } if (j == 0) { yield return null; } else { averageDirection = averageDirection / j; //yield return Ninja.JumpToUnity; SeparationCallback (averageDirection); yield return null; } }
void SeparationResult(EVector2 result){ Vector3 final = new Vector3(result.x,result.y,0); thisTransform.position += final * 1f * Time.deltaTime; }
IEnumerator CheckSeparation(){ while(true){ //yield return Ninja.JumpToUnity; Collider2D[] colls = new Collider2D[11]; int numColls = Physics2D.OverlapCircleNonAlloc(thisTransform.position,detectionRadius,colls); if(colls.Length > 10){ NearbyAllies.Clear(); for(int i = 0; i < colls.Length;i++){ if(colls[i] != null) NearbyAllies.Add(new EVector2(colls[i].transform.position.x,colls[i].transform.position.y)); } EVector2 tempEvector2 = new EVector2(thisTransform.position.x,thisTransform.position.y); //this.StartCoroutineAsync(SeparationCalc(NearbyAllies,SeparationResult,tempEvector2)); //yield return Ninja.JumpBack; } System.Random rnd = new System.Random(); int temp = rnd.Next(30,60); yield return new WaitForSeconds(temp/100f); } }