// Use this for initialization void Start() { gameManager = FindObjectOfType <GameManager> (); Rb = GetComponent <Rigidbody> (); mp = GetComponentInParent <MP_Control> (); Home = GameObject.Find(mp.Party + "Home"); }
void DetermineFocus() { Debug.Log("Finding focus"); GameManager gm = FindObjectOfType <GameManager>(); // Copy the list of mps to a new array so that any changes to the list after yielding do not cause errors. // Get the most interesting position by getting the average position of hit mps but multiplying the positions by their HitsPerSecond cubed to shift the average position towards them. Vector3 mostInterestingPosition = Vector3.zero; Vector3 averagePosition = Vector3.zero; float totalInterest = 0.0f; int numMPs = 0; foreach (Party p in gm.partyManager.parties) { numMPs += p.mps.Count; // Access the mps through a normal for loop since the number of mps could change after yielding. for (int i = 0; i < p.mps.Count; ++i) { if (p.mps[i] != null) { averagePosition += p.mps[i].transform.position; MP_Control mpControl = p.mps[i].GetComponent <MP_Control>(); // If the mp has not been hit recently, ignore it. if (mpControl.HitsPerSecond != 0) { float interest = Mathf.Pow(1 + mpControl.HitsPerSecond, 3); mostInterestingPosition += p.mps[i].transform.Find("HeadTarget").position *interest; totalInterest += interest; } } } } // Rescale the average positions. mostInterestingPosition /= totalInterest; averagePosition /= numMPs; FocusDefaultPosition = percentageCentre; //Debug.Log ("Num MPS: " + numMPs + " | Interest: " + totalInterest); // If nothing specifically interesting is going on, just focus at the average position. if (totalInterest / numMPs < AutoFocusInterestThreshold) { Debug.Log("Percentage Centre 1"); SetFocus(percentageCentre); } else { // Get the most beat up MP nearest to the position. float bestDistance = float.MaxValue; Transform bestMP = null; foreach (Party p in gm.partyManager.parties) { for (int i = 0; i < p.mps.Count; ++i) { if (p.mps[i] != null) { MP_Control mpControl = p.mps[i].GetComponent <MP_Control>(); if (mpControl.HitsPerSecond != 0) { float distance = Vector3.Distance(p.mps[i].transform.Find("HeadTarget").position, mostInterestingPosition) / mpControl.HitsPerSecond; if (distance < bestDistance) { bestDistance = distance; bestMP = p.mps[i].transform; } } } } } SetFocus(bestMP == null ? null : bestMP.Find("HeadTarget")); } }