/// <summary> /// Sets the boids behavior to flocking (FlockWander) /// </summary> void BoidsFlocking() { var wander = new FlockWander(); flockState = wander; wander.anchor = player.GetComponent<Starling>(); player.GetComponent<Starling>().maxSpeed = 10f; player.transform.Find("Sphere").gameObject.SetActive(true); player.transform.position = new Vector3(700f, 90f, 350f); player.transform.eulerAngles = new Vector3(0f, 0f, 0f); var starlings = GameObject.FindGameObjectsWithTag("Starling"); for (int i = 0; i < starlings.Length; ++i) { if( starlings[i].name == "Player" ) continue; var starling = starlings[i].GetComponent<Starling>(); starlings[i].transform.Find("Sphere").gameObject.SetActive(false); starling.state = wander; wander.Add(starling); } wander.Init(); cameraDistanceFromFlock = 150f; cameraAngle = 90f; }
/// <summary> /// Draws the options for the Separation of the flock /// </summary> void DrawSeparationOptions(float x, float y, FlockWander flockWander, out float width, out float height) { if (separationMenuShown) { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "-")) separationMenuShown = !separationMenuShown; float temp; int tempInt; GUI.Box(new Rect(x, y, 175f, 255f), "Separation"); width = x + 175f; height = y + 255; // slider - flock separation weight GUI.Label(new Rect(x + 12.5f, y + 30f, 150, 25), "Weight"); flockWander.separationWeight = GUI.HorizontalSlider(new Rect(x + 12.5f, y + 50f, 100, 30), flockWander.separationWeight, 0.0f, 2.0f); GUI.Label(new Rect(x + 117.5f, y + 45f, 100, 25), flockWander.separationWeight.ToString("0.000")); // slider - flock cohesion angle GUI.Label(new Rect(x + 12.5f, y + 65f, 150, 25), "Angle"); flockWander.SeparationAngle = GUI.HorizontalSlider(new Rect(x + 12.5f, y + 85f, 100, 30), flockWander.SeparationAngle, 0.0f, 360.0f); GUI.Label(new Rect(x + 117.5f, y + 80f, 100, 25), flockWander.SeparationAngle.ToString("0.000")); // SEPARATION a-knn approximation value GUI.Label(new Rect(x + 12.5f, y + 95, 150, 25), "a-knn epsilon"); if (float.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 115f, 150, 25), flockWander.separationAknnVal.ToString(".000")), out temp)) flockWander.separationAknnVal = temp; else flockWander.separationAknnVal = 1.0f; // SEPARATION distance GUI.Label(new Rect(x + 12.5f, y + 145, 150, 25), "neighbors within (m)"); if(float.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 165, 150, 25), flockWander.separationDistance.ToString(".000")), out temp)) flockWander.separationDistance = temp; else flockWander.separationDistance = 5.0f; // SEPARATION k (number of neighbors) GUI.Label(new Rect(x + 12.5f, y + 195, 150, 25), "# of neighbors"); if( int.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 215, 150, 25), flockWander.separationK.ToString()), out tempInt) ) flockWander.separationK = tempInt; else flockWander.separationK = 5; } else { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "+")) separationMenuShown = !separationMenuShown; GUI.Box(new Rect(x, y, 175f, 30f), "Separation"); width = x + 175f; height = y + 30f; } }
/// <summary> /// Draws the options for the Velocity Match of the flock /// </summary> void DrawVelocityMatchOptions(float x, float y, FlockWander flockWander, out float width, out float height) { if (velocityMatchMenuShown) { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "-")) velocityMatchMenuShown = !velocityMatchMenuShown; float temp; int tempInt; GUI.Box(new Rect(x, y, 175f, 255f), "Velocity Match"); width = x + 175f; height = y + 255f; // slider - flock velocity match weight GUI.Label(new Rect(x + 12.5f, y + 30f, 150, 25), "Weight"); flockWander.velocityMatchWeight = GUI.HorizontalSlider(new Rect(x + 12.5f, y + 50f, 100, 30), flockWander.velocityMatchWeight, 0.0f, 2.0f); GUI.Label(new Rect(x + 117.5f, y + 45f, 100, 25), flockWander.velocityMatchWeight.ToString("0.000")); // slider - flock angle GUI.Label(new Rect(x + 12.5f, y + 65f, 150, 25), "Angle"); flockWander.VelocityMatchAngle = GUI.HorizontalSlider(new Rect(x + 12.5f, y + 85f, 100, 30), flockWander.VelocityMatchAngle, 0.0f, 360.0f); GUI.Label(new Rect(x + 117.5f, y + 80f, 100, 25), flockWander.VelocityMatchAngle.ToString("0.000")); // velocityMatch a-knn approximation value GUI.Label(new Rect(x + 12.5f, y + 95f, 150, 25), "a-knn epsilon"); if (float.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 115f, 150, 25), flockWander.velocityMatchAknnVal.ToString(".000")), out temp)) flockWander.velocityMatchAknnVal = temp; else flockWander.velocityMatchAknnVal = 50.0f; // velocityMatch distance GUI.Label(new Rect(x + 12.5f, y + 145f, 150, 25), "neighbors within (m)"); if (float.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 165f, 150, 25), flockWander.velocityMatchDistance.ToString(".000")), out temp)) flockWander.velocityMatchDistance = temp; else flockWander.velocityMatchDistance = 100.0f; // velocityMatch k (number of neighbors) GUI.Label(new Rect(x + 12.5f, y + 195f, 150, 25), "# of neighbors"); if (int.TryParse(GUI.TextField(new Rect(x + 12.5f, y + 215f, 150, 25), flockWander.velocityMatchK.ToString()), out tempInt)) flockWander.velocityMatchK = tempInt; else flockWander.velocityMatchK = 30; } else { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "+")) velocityMatchMenuShown = !velocityMatchMenuShown; GUI.Box(new Rect(x, y, 175f, 30f), "Velocity Match"); width = x + 175f; height = y + 30f; } }
void DrawOtherOptions(float x, float y, FlockWander flockWander, out float width, out float height) { if (otherOptionsShown) { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "-")) otherOptionsShown = !otherOptionsShown; GUI.Box(new Rect(x, y, 175f, 195f), "Other Options"); width = x + 175f; height = y + 195f; // bird trails bool oldShowTrails = showTrails; showTrails = GUI.Toggle(new Rect(x + 12.5f, y + 30f, 150f, 20f), showTrails, " Show trails"); if (oldShowTrails != showTrails) { var starlings = GameObject.FindGameObjectsWithTag("Starling"); foreach(var starling in starlings) if( starling.name != "Player" ) // skip the Player starling.GetComponent<TrailRenderer>().enabled = showTrails; } var cameraModes = new GUIContent[]{new GUIContent("Flock leader"), new GUIContent("Random boid")}; GUI.Label(new Rect(x + 12.5f, y + 55f, 100f, 25f), "Camera Mode"); int oldCameraModeIndex = cameraModeIndex; cameraModeIndex = GUI.SelectionGrid(new Rect(x + 25f, y + 75f, 100f, 65f), cameraModeIndex, cameraModes, 1); // if camera mode has changed and it was set to follow a random boid if (oldCameraModeIndex != cameraModeIndex) { if (cameraModeIndex == 0) cameraDistanceFromFlock = 135f; else if( cameraModeIndex == 1 ) { cameraDistanceFromFlock = 10f; var boid = GameObject.Find("s-" + UnityEngine.Random.Range(0, int.Parse(numBirds))); boidTranformToFollow = boid.transform; } } // slider - percentage of birds to update every frame GUI.Label(new Rect(x + 12.5f, y + 150f, 150, 25), "% boids to update"); flockWander.percentageOfBoidsToUpdate = GUI.HorizontalSlider(new Rect(x + 12.5f, y + 170f, 100, 30), flockWander.percentageOfBoidsToUpdate, 0.01f, 1.0f); GUI.Label(new Rect(x + 117.5f, y + 165f, 100, 25), (flockWander.percentageOfBoidsToUpdate * 100f).ToString("0.0") + "%"); } else { if (GUI.Button(new Rect(x + 150f, y + 5f, 20, 20), "+")) otherOptionsShown = !otherOptionsShown; GUI.Box(new Rect(x, y, 175f, 30f), "Other Options"); width = x + 175f; height = y + 30f; } }