// Return true if it is possible to spawn a Protester at the given coordinates bool CheckSpawnCoords(Vector2 pos) { Vector3 pos3D = Protester.TopVec2ToVec3(pos); for (int i = 0; i != population; ++i) { if (Vector3.Distance(pos3D, protesters[i].transform.position) < 1f) { return(false); } } return(true); }
// Spawn a Protester and store it in the internal array at index idx void SpawnProtester(int idx) { Vector2 pos = new Vector2(); int attempt = 0; do { // Check that the generation isn't stuck float t = Time.time; if (attempt > maxGenAttempts) { Debug.LogError("Crowd generation taking too long, aborting"); settingsAreShit = true; return; } // Generate spawn coordinates switch (parameters.crowdShape) { case Shape.Circle: pos = Random.insideUnitCircle * parameters.circle.radius; break; case Shape.Rectangle: pos = new Vector2(Random.Range(parameters.rectangle.topLeft.x, parameters.rectangle.bottomRight.x), Random.Range(parameters.rectangle.bottomRight.y, parameters.rectangle.topLeft.y)); break; } ++attempt; } while (!CheckSpawnCoords(pos)); // Actually spawn the Protester Protester newProt = GameObject.Instantiate(protesterPrefab, crowdRoot.transform) as Protester; newProt.transform.GetChild(1).GetComponent <Renderer>().material = parameters.SwapColor[Random.Range(0, 3)]; newProt.transform.position = Protester.TopVec2ToVec3(pos) + new Vector3(0, 0.5f, 0); protesters [idx] = newProt; population += 1; }