Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        currDelay -= Time.deltaTime;
        if (currDelay < 0)
        {
            currDelay = spawnDelay;// * (70 / GameManager.Instance.forestHealth));
            SpawnGoodItems();
        }

        for (int i = 0; i < helpers.Length; i++)
        {
            HelperSpawns newHelper = helpers[i];
            if (!spawnTracker[i] && newHelper.forestHealthThreshold < GameManager.Instance.forestHealth)
            {
                // Find good spot to spawn
                float x             = 0;
                float z             = 0;
                var   viewPoint     = Camera.main.WorldToViewportPoint(new Vector3(x, 0, z));
                bool  pointOnCamera = true;
                int   loopCount     = 0;
                bool  failedSpawn   = false;

                while (pointOnCamera)
                {
                    x             = Random.Range(-50, 50);
                    z             = Random.Range(-50, 50);
                    pointOnCamera = (viewPoint.x > 0 && viewPoint.x < 1 && viewPoint.z > 0 && viewPoint.z < 1);
                    loopCount++;
                    if (loopCount > 10)
                    {
                        failedSpawn = true;
                        print("No valid location for helper found");
                        break;
                    }
                }
                if (!failedSpawn)
                {
                    CritterHelper newCritter = Instantiate(newHelper.helper, new Vector3(x, 0, z), Quaternion.identity);
                    spawnTracker[i] = true;
                }
            }
        }
    }
Exemplo n.º 2
0
    // Start is called before the first frame update
    void Start()
    {
        Collider mCollider       = GetComponent <MeshCollider>();
        Vector3  mSize           = mCollider.bounds.size;
        float    exclusionRadius = 10f;

        for (int i = 0; i < terrainObjects.Length; i++)
        {
            for (int j = 0; j < terrainObjects[i].count; j++)
            {
                float theta     = Random.Range(0, 2 * Mathf.PI);
                float loopCount = 0f;
                float distance  = Mathf.Sqrt(Random.Range(exclusionRadius * exclusionRadius, totalRadius * totalRadius));

                float x = distance * Mathf.Cos(theta);
                float z = distance * Mathf.Sin(theta);

                while (IntersectsSand(new Vector3(x, 0, z)))
                {
                    distance = Mathf.Sqrt(Random.Range(exclusionRadius * exclusionRadius, totalRadius * totalRadius));

                    x = distance * Mathf.Cos(theta);
                    z = distance * Mathf.Sin(theta);
                    loopCount++;
                    if (loopCount > 10f)
                    {
                        break;
                    }
                }


                GameObject newTerrainObject     = Instantiate(terrainObjects[i].envObj, new Vector3(x, 0, z), Quaternion.identity);
                Vector3    randomRotationVector = new Vector3(0, Random.Range(0, 360), 0);
                newTerrainObject.transform.Rotate(randomRotationVector);
            }
        }

        for (int i = 0; i < helpers.Length; i++)
        {
            HelperSpawns newHelper = helpers[i];
            spawnTracker.Add(false);
        }
    }