| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class StandingPeopleStreet : MonoBehaviour {
- [HideInInspector] public GameObject planePrefab;
- [HideInInspector] public GameObject circlePrefab;
- [HideInInspector] public GameObject surface;
- [HideInInspector] public Vector2 planeSize = new Vector2(1, 1);
- [Tooltip("People prefabs / Префабы людей")] public GameObject[] peoplePrefabs = new GameObject[0];
- [HideInInspector]
- public List<Vector3> spawnPoints = new List<Vector3>();
- [HideInInspector] public int peopleCount;
- [HideInInspector] public bool isCircle;
- [HideInInspector] public float circleDiametr = 1;
- [HideInInspector] public bool showSurface = true;
- public enum TestEnum{Rectangle, Circle};
- [Tooltip("Type of surface / Тип поверхности")] public TestEnum SurfaceType;
- [HideInInspector]
- public GameObject par;
- [HideInInspector] public float highToSpawn;
- public void OnDrawGizmos()
- {
- if (!isCircle)
- surface.transform.localScale = new Vector3(planeSize.x, 1, planeSize.y);
- else
- surface.transform.localScale = new Vector3(circleDiametr, 1, circleDiametr);
- }
- public void SpawnRectangleSurface()
- {
- if(surface != null) DestroyImmediate(surface);
- var plane = Instantiate(planePrefab, transform.position, Quaternion.identity) as GameObject;
- surface = plane;
- isCircle = false;
- plane.transform.eulerAngles = new Vector3(
- plane.transform.eulerAngles.x,
- plane.transform.eulerAngles.y,
- plane.transform.eulerAngles.z
- );
- plane.transform.position += new Vector3(0, 0.01f, 0);
- plane.transform.parent = transform;
- plane.name = "surface";
- }
- public void SpawnCircleSurface()
- {
- if(surface != null) DestroyImmediate(surface);
- var circle = Instantiate(circlePrefab, transform.position, Quaternion.identity) as GameObject;
- isCircle = true;
- circle.transform.eulerAngles = new Vector3(
- circle.transform.eulerAngles.x,
- circle.transform.eulerAngles.y,
- circle.transform.eulerAngles.z
- );
- circle.transform.position += new Vector3(0, 0.01f, 0);
- circle.transform.parent = transform;
- circle.name = "surface";
- surface = circle;
- }
- public void RemoveButton()
- {
- if(par != null)
- DestroyImmediate(par);
- par = null;
- }
- public void PopulateButton()
- {
- RemoveButton();
- GameObject parGo = new GameObject();
- par = parGo;
- parGo.transform.parent = gameObject.transform;
- parGo.name = "people";
- spawnPoints.Clear();
- SpawnPeople(peopleCount);
- }
- void SpawnPeople (int _peopleCount)
- {
- int three = Random.Range(0, (int) (_peopleCount) / 3) * 3;
- int two = Random.Range(0, (int) (_peopleCount - three) / 2) * 2;
- int one = _peopleCount - three - two;
- int[] peoplePrefabIndexes = CommonUtils.GetRandomPrefabIndexes(peopleCount, ref peoplePrefabs);
- int peoplePrefabCount = 0;
- for(int i = 0; i < one; i++)
- {
- Vector3 randomPosition;
- if(!isCircle)
- randomPosition = RandomRectanglePosition();
- else
- randomPosition = RandomCirclePosition();
- if (randomPosition != Vector3.zero)
- {
- RaycastHit hit;
- GameObject obj = null;
- if (Physics.Raycast(randomPosition + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
- {
- obj = Instantiate(peoplePrefabs[peoplePrefabIndexes[peoplePrefabCount]], new Vector3(randomPosition.x, hit.point.y, randomPosition.z), Quaternion.identity) as GameObject;
- peoplePrefabCount++;
- }
- else
- {
- continue;
- }
- obj.AddComponent<PeopleController>();
- spawnPoints.Add(obj.transform.position);
- obj.transform.localEulerAngles = new Vector3(obj.transform.rotation.x, Random.Range(1, 359), obj.transform.rotation.z);
- obj.GetComponent<PeopleController>().animNames = new string[2]{"idle1", "idle2"};
- obj.transform.parent = par.transform;
- }
- }
-
- for(int i = 0; i < two / 2; i++)
- {
- Vector3 randomPosition;
- if(!isCircle)
- randomPosition = RandomRectanglePosition();
- else
- randomPosition = RandomCirclePosition();
-
- if(randomPosition != Vector3.zero)
- {
- Vector3 randomPos1 = Vector3.zero;
- Vector3 randomPos2 = Vector3.zero;
- for(int f = 0; f < 100; f++)
- {
- for(int z = 0; z < 10; z++)
- {
- randomPos1 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
- if(IsRandomPositionFree(randomPos1, Vector3.zero, Vector3.zero))
- break;
- else randomPos1 = Vector3.zero;
- }
- for(int x = 0; x < 10; x++)
- {
- randomPos2 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
- if(IsRandomPositionFree(randomPos2, randomPos1, Vector3.zero))
- break;
- else randomPos2 = Vector3.zero;
- }
-
- if(randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
- {
- spawnPoints.Add(randomPos1);
- spawnPoints.Add(randomPos2);
- break;
- }
- else
- {
- randomPos1 = Vector3.zero;
- randomPos2 = Vector3.zero;
- }
- }
- if(randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
- {
- int prefabNum = Random.Range(0, peoplePrefabs.Length);
- RaycastHit hit;
- GameObject obj = null;
- if (Physics.Raycast(randomPos1 + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
- {
- obj = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos1.x, hit.point.y, randomPos1.z), Quaternion.identity) as GameObject;
- peoplePrefabCount++;
- }
- else
- {
- continue;
- }
- obj.AddComponent<PeopleController>();
- obj.GetComponent<PeopleController>().animNames = new string[3]{"talk1", "talk2", "listen"};
- obj.transform.parent = par.transform;
- prefabNum = Random.Range(0, peoplePrefabs.Length);
- RaycastHit hit1;
- GameObject obs = null;
- if (Physics.Raycast(randomPos2 + Vector3.up * highToSpawn, Vector3.down, out hit1, Mathf.Infinity))
- {
- obs = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos2.x, hit1.point.y, randomPos2.z), Quaternion.identity) as GameObject;
- }
- else
- {
- continue;
- }
- obs.AddComponent<PeopleController>();
- obs.GetComponent<PeopleController>().animNames = new string[3]{"talk1", "talk2", "listen"};
- obs.transform.parent = par.transform;
- obs.GetComponent<PeopleController>().SetTarget(obj.transform.position);
- obj.GetComponent<PeopleController>().SetTarget(obs.transform.position);
- }
- }
- }
-
- for(int i = 0; i < three / 3; i++)
- {
- Vector3 randomPosition;
- if(!isCircle)
- randomPosition = RandomRectanglePosition();
- else
- randomPosition = RandomCirclePosition();
- if(randomPosition != Vector3.zero)
- {
- int prefabNum = Random.Range(0, peoplePrefabs.Length);
- Vector3 randomPos0 = Vector3.zero;
- Vector3 randomPos1 = Vector3.zero;
- Vector3 randomPos2 = Vector3.zero;
- for(int f = 0; f < 100; f++)
- {
- int z;
- int x;
- int c;
- for(z = 0; z < 10; z++)
- {
- randomPos0 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
- if(IsRandomPositionFree(randomPos0, Vector3.zero, Vector3.zero))
- {
- break;
- }
- else randomPos0 = Vector3.zero;
- }
- for(x = 0; x < 10; x++)
- {
- if(randomPos0 != Vector3.zero)
- {
- randomPos1 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
- if(IsRandomPositionFree(randomPos1, randomPos0, Vector3.zero))
- {
- break;
- }
- else
- {
- randomPos1 = Vector3.zero;
- }
- }
- else randomPos1 = Vector3.zero;
- }
- for(c = 0; c < 10; c++)
- {
- if(randomPos1 != Vector3.zero && randomPos0 != Vector3.zero)
- {
- randomPos2 = randomPosition + new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f));
- if(IsRandomPositionFree(randomPos2, randomPos0, randomPos1))
- {
- break;
- }
- else
- {
- randomPos2 = Vector3.zero;
- }
- }
- else randomPos2 = Vector3.zero;
- }
- if(randomPos0 != Vector3.zero && randomPos1 != Vector3.zero && randomPos2 != Vector3.zero)
- {
- spawnPoints.Add(randomPos0);
- spawnPoints.Add(randomPos1);
- spawnPoints.Add(randomPos2);
- break;
- }
- else
- {
- randomPos0 = Vector3.zero;
- randomPos1 = Vector3.zero;
- randomPos2 = Vector3.zero;
- }
- }
- if(randomPos0 != Vector3.zero)
- {
- if (randomPos0 != Vector3.zero)
- {
- RaycastHit hit;
- GameObject obj = null;
- if (Physics.Raycast(randomPos0 + Vector3.up * highToSpawn, Vector3.down, out hit, Mathf.Infinity))
- {
- obj = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos0.x, hit.point.y, randomPos0.z), Quaternion.identity) as GameObject;
- peoplePrefabCount++;
- }
- else
- {
- continue;
- }
- obj.AddComponent<PeopleController>();
- obj.GetComponent<PeopleController>().SetTarget(randomPosition);
- obj.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
- obj.transform.parent = par.transform;
- }
- prefabNum = Random.Range(0, peoplePrefabs.Length);
- if (randomPos0 != Vector3.zero)
- {
- RaycastHit hit1;
- GameObject obs = null;
- if (Physics.Raycast(randomPos1 + Vector3.up * highToSpawn, Vector3.down, out hit1, Mathf.Infinity))
- {
- obs = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos1.x, hit1.point.y, randomPos1.z), Quaternion.identity) as GameObject;
- peoplePrefabCount++;
- }
- else
- {
- continue;
- }
- obs.AddComponent<PeopleController>();
- obs.GetComponent<PeopleController>().SetTarget(randomPosition);
- obs.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
- obs.transform.parent = par.transform;
- }
- prefabNum = Random.Range(0, peoplePrefabs.Length);
- if (randomPos0 != Vector3.zero)
- {
- RaycastHit hit2;
- GameObject obl = null;
- if (Physics.Raycast(randomPos2 + Vector3.up * highToSpawn, Vector3.down, out hit2, Mathf.Infinity))
- {
- obl = Instantiate(peoplePrefabs[prefabNum], new Vector3(randomPos2.x, hit2.point.y, randomPos2.z), Quaternion.identity) as GameObject;
- peoplePrefabCount++;
- }
- else
- {
- continue;
- }
- obl.AddComponent<PeopleController>();
- obl.GetComponent<PeopleController>().SetTarget(randomPosition);
- obl.GetComponent<PeopleController>().animNames = new string[3] { "talk1", "talk2", "listen" };
- obl.transform.parent = par.transform;
- }
- }
- }
- }
- }
- Vector3 RandomRectanglePosition ()
- {
- Vector3 randomPosition = new Vector3(0, 0, 0);
- for(int i = 0; i < 10; i++)
- {
- randomPosition.x = surface.transform.position.x - GetRealPlaneSize().x / 2 + 0.3f + Random.Range(0.0f, GetRealPlaneSize().x - 0.6f);
- randomPosition.z = surface.transform.position.z - GetRealPlaneSize().y / 2 + 0.3f + Random.Range(0.0f, GetRealPlaneSize().y - 0.6f);
- randomPosition.y = surface.transform.position.y;
- if(IsRandomPositionFree(randomPosition, Vector3.zero, Vector3.zero))
- return randomPosition;
- }
- return Vector3.zero;
- }
- Vector3 RandomCirclePosition ()
- {
- Vector3 center = surface.transform.position;
- float radius = GetRealPlaneSize().x / 2;
- for(int i = 0; i < 10; i++)
- {
- float randomRadius = Random.value * radius;
- float ang = Random.value * 360;
- Vector3 pos;
- pos.x = center.x + randomRadius * Mathf.Sin(ang * Mathf.Deg2Rad);
- pos.y = center.y;
- pos.z = center.z + randomRadius * Mathf.Cos(ang * Mathf.Deg2Rad);
- if(Vector3.Distance(pos, center) < GetRealPlaneSize().x / 2 - 0.3f && IsRandomPositionFree(pos, Vector3.zero, Vector3.zero))
- return pos;
- }
- return Vector3.zero;
- }
- bool IsRandomPositionFree (Vector3 pos, Vector3 helpPoint1, Vector3 helpPoint2)
- {
- for(int i = 0; i < spawnPoints.Count; i++)
- {
- if(spawnPoints[i].x - 0.5f < pos.x && spawnPoints[i].x + 0.5f > pos.x && spawnPoints[i].z - 0.5f < pos.z && spawnPoints[i].z + 0.5f > pos.z)
- return false;
- }
- if(helpPoint1 != Vector3.zero)
- {
- if(helpPoint1.x - 0.6f < pos.x && helpPoint1.x + 0.6f > pos.x && helpPoint1.z - 0.6f < pos.z && helpPoint1.z + 0.6f > pos.z)
- {
- return false;
- }
- if(!isCircle)
- {
- if(
- !(helpPoint1.x + 0.3f > surface.transform.position.x - GetRealPlaneSize().x / 2) &&
- !(helpPoint1.x - 0.3f < surface.transform.position.x + GetRealPlaneSize().x / 2) &&
- !(helpPoint1.z + 0.3f > surface.transform.position.z - GetRealPlaneSize().y / 2) &&
- !(helpPoint1.z - 0.3f < surface.transform.position.z + GetRealPlaneSize().y / 2)
- )
- return false;
- }
- else
- {
- if(Vector3.Distance(helpPoint1, surface.transform.position) >= GetRealPlaneSize().x / 2 - 0.3f)
- return false;
- }
- }
- if(helpPoint2 != Vector3.zero)
- {
- if(helpPoint2.x - 0.6f < pos.x && helpPoint2.x + 0.6f > pos.x && helpPoint2.z - 0.6f < pos.z && helpPoint2.z + 0.6f > pos.z)
- {
- return false;
- }
- if(!isCircle)
- {
- if(
- !(helpPoint2.x + 0.3f > surface.transform.position.x - GetRealPlaneSize().x / 2) &&
- !(helpPoint2.x - 0.3f < surface.transform.position.x + GetRealPlaneSize().x / 2) &&
- !(helpPoint2.z + 0.3f > surface.transform.position.z - GetRealPlaneSize().y / 2) &&
- !(helpPoint2.z - 0.3f < surface.transform.position.z + GetRealPlaneSize().y / 2)
- )
- return false;
- }
- else
- {
- if(Vector3.Distance(helpPoint2, surface.transform.position) >= GetRealPlaneSize().x / 2 - 0.3f)
- return false;
- }
- }
- return true;
- }
- Vector2 GetRealPlaneSize()
- {
- Vector3 meshSize = surface.GetComponent<MeshRenderer>().bounds.size;
- return new Vector2(meshSize.x, meshSize.z);
- }
- Vector2 GetRealPeopleModelSize()
- {
- Vector3 meshSize = peoplePrefabs[1].GetComponent<MeshRenderer>().bounds.size;
- return new Vector2(meshSize.x, meshSize.z);
- }
- }
|