Beispiel #1
0
    /*public static void SpawnLake(Transform lakePos)
     * {
     *  NatureObject no = NatureObject.Get("Teich");
     *  Vector3 gpos = Grid.ToGrid(lakePos.position);
     *  GameNatureObject go = new GameNatureObject(no, (int)gpos.x, (int)gpos.z);
     *  go.SetPosition(lakePos.transform.position);
     *  go.SetRotation(Quaternion.identity);
     *  SpawnNatureObject(go);
     * }*/

    public static void SpawnForest(int treeAm, Vector3 pos)
    {
        NatureObject     no = NatureObject.Get("Birke");
        GameNatureObject go;

        Vector3 node = Grid.ToGrid(pos);
        int     gx   = (int)node.x;
        int     gy   = (int)node.z;

        for (int i = 0; i < treeAm; i++)
        {
            int x     = 0;
            int z     = 0;
            int count = 0;
            do
            {
                int spread = treeAm / 10 + count + 1;
                x = gx + Random.Range(-spread, spread);
                z = gy + Random.Range(-spread, spread);
            } while (!Grid.ValidNode(x, z) || (Grid.GetNode(x, z).IsOccupied() || (Mathf.Abs(Grid.SpawnY - x) < 5 && Mathf.Abs(Grid.SpawnY - z) < 5)) && (++count) < 100);

            if (count == 100)
            {
                continue;
            }

            go = new GameNatureObject(no, x, z);
            go.SetPosition(Grid.ToWorld(x + no.gridWidth / 2, z + no.gridHeight / 2));
            go.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0));
            SpawnNatureObject(go);
        }
    }
Beispiel #2
0
    private void SetupRandomNature()
    {
        // If in editor, spawn less nature to speed up testing
        if (Application.isEditor)
        {
            Spawn(30, 5, 5, 5, 30, 5, 5, 5, 5);                      // Spawn(50, 80, 15, 5, 30, 40, 20); //Spawn(200,50,15,10,70,15);//Spawn(50,20,5,5,30,5);
        }
        // Spawn some random plants
        else
        {
            Spawn(850, 320, 45, 25, 340, 40, 20, 200, 200);

            for (int i = 0; i < forestParent.childCount; i++)
            {
                Forest f = forestParent.GetChild(i).GetComponent <Forest>();
                SpawnForest(f.trees, f.transform.position);
            }
        }

        /*for (int i = 0; i < lakeParent.childCount; i++)
         * {
         *  Transform lk = lakeParent.GetChild(i);
         *  SpawnLake(lk);
         * }*/

        int count = 0;
        int x, z;

        do
        {
            x = Random.Range(-5, 5) + Grid.SpawnX;
            z = Random.Range(-5, 5) + Grid.SpawnY;
        } while ((Grid.GetNode(x, z).IsOccupied() || Grid.GetNode(x, z).IsWater() || (Mathf.Abs(Grid.SpawnX) < 5 && Mathf.Abs(Grid.SpawnY) < 5)) && (++count) < 100);
        if (count < 100)
        {
            NatureObject     no = NatureObject.Get("Pilzstrunk");
            GameNatureObject go = new GameNatureObject(no, x, z);
            go.SetPosition(Grid.ToWorld(x + no.gridWidth / 2, z + no.gridHeight / 2));
            go.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0));
            SpawnNatureObject(go);
        }
    }
Beispiel #3
0
    public static void SpawnRandomPosNatureObject(NatureObject baseNo, int rndSize)
    {
        Terrain terrain = Terrain.activeTerrain;

        bool invalid = false;

        int x     = 0;
        int z     = 0;
        int count = 0;

        if (baseNo.type == NatureObjectType.Reed)
        {
            List <Node> sn = new List <Node>(shore);
            if (sn.Count == 0)
            {
                return;
            }
            Node rsn = sn[Random.Range(0, sn.Count)];
            while (rsn.nodeObject != null && (++count) < 100)
            {
                rsn = sn[Random.Range(0, sn.Count)];
            }
            if (count == 100)
            {
                return;
            }
            x = rsn.gridX;
            z = rsn.gridY;
        }
        else
        {
            x = UnityEngine.Random.Range(0, Grid.WIDTH);
            z = UnityEngine.Random.Range(0, Grid.HEIGHT);
            while ((Grid.GetNode(x, z).IsOccupied() || Grid.GetNode(x, z).IsWater() || (Mathf.Abs(Grid.SpawnX - x) < Mathf.Max(5, baseNo.minDistToCenter) && Mathf.Abs(Grid.SpawnY - z) < Mathf.Max(5, baseNo.minDistToCenter))) && (++count) < 100)
            {
                x = UnityEngine.Random.Range(0, Grid.WIDTH);
                z = UnityEngine.Random.Range(0, Grid.HEIGHT);
            }
            if (count == 100)
            {
                return;
            }
            for (int dx = 0; dx < 3; dx++)
            {
                if (invalid)
                {
                    continue;
                }
                for (int dy = 0; dy < 3; dy++)
                {
                    if (invalid)
                    {
                        continue;
                    }
                    if (!Grid.ValidNode(x + dx, z + dy))
                    {
                        invalid = true;
                    }
                    else if (Grid.GetNode(x + dx, z + dy).IsOccupied())
                    {
                        invalid = true;
                    }
                }
            }
        }
        if (invalid)
        {
            return;
        }

        GameNatureObject toSpawn = new GameNatureObject(baseNo, x, z);

        toSpawn.SetPosition(Grid.ToWorld(x + baseNo.gridWidth / 2, z + baseNo.gridHeight / 2));
        toSpawn.SetRotation(Quaternion.Euler(0, Random.Range(0, 360), 0));

        NatureObjectScript nos = SpawnNatureObject(toSpawn);

        nos.SetRandSize(rndSize);
    }