Пример #1
0
    // Use this for initialization
    void Start()
    {
        List <Vector2> mountainsPositions = new List <Vector2>();
        List <Vector2> forestsPositions   = new List <Vector2>();


        int nbMountains = Random.Range(2, 5);

        for (int i = 0; i < nbMountains; i++)
        {
            mountainsPositions.Add(new Vector2(Random.Range(0, widthOfTheWorld), Random.Range(0, lengthOfTheWorld)));
        }


        List <List <Tile> > world = new List <List <Tile> >();

        for (int i = 0; i < widthOfTheWorld; i++)
        {
            List <Tile> line = new List <Tile>();
            for (int j = 0; j < lengthOfTheWorld; j++)
            {
                int tileY = 0;
                for (int m = 0; m < mountainsPositions.Count; m++)
                {
                    int dist  = tileDistance(mountainsPositions[m], new Vector2(i, j));
                    int value = areaOfMountain - dist;
                    if (dist < areaOfMountain && value > tileY)
                    {
                        tileY = (value + Random.Range(-1, 2)) * strengthOfMountain;
                    }
                }
                Tile tmp;
                if (tileY > 0)
                {
                    tmp = Instantiate(rock) as Tile;
                }
                else
                {
                    if (i < 2 || i >= widthOfTheWorld - 2 || j < 2 || j >= lengthOfTheWorld - 2)
                    {
                        tmp = Instantiate(sand) as Tile;
                    }
                    else
                    {
                        tmp = Instantiate(grass) as Tile;
                    }
                }

                tmp.y = tileY;
                line.Add(tmp);
            }
            world.Add(line);
        }

        int nbForests = Random.Range(2, 5);

        for (int i = 0; i < nbForests; i++)
        {
            Vector2 v    = new Vector2(Random.Range(0, widthOfTheWorld), Random.Range(0, lengthOfTheWorld));
            bool    redo = true;
            while (redo)
            {
                redo = false;
                for (int k = 0; k < nbMountains; k++)
                {
                    if (tileDistance(v, mountainsPositions[k]) <= areaOfMountain + 8)
                    {
                        v    = new Vector2(Random.Range(0, widthOfTheWorld), Random.Range(0, lengthOfTheWorld));
                        redo = true;
                        break;
                    }
                }
            }

            int nbTrees = Random.Range(200, 500);
            for (int j = 0; j < nbTrees; j++)
            {
                float x = Random.Range(-80f, 80f);
                float y = Random.Range(-80f, 80f);

                GameObject t = Instantiate(trees[Random.Range(0, trees.Count)]) as GameObject;
                float      rX = Mathf.Clamp(v.x * 10 + x, 0, widthOfTheWorld * 10), rY = Mathf.Clamp(v.y * 10 + y, 0, lengthOfTheWorld * 10);
                t.transform.position = new Vector3(rX, 0, rY);
                float size = Random.Range(0.5f, 1.5f);
                t.transform.localScale = new Vector3(size, size, size);

                t.transform.rotation = Quaternion.Euler(Random.Range(0, 5), Random.Range(0, 360), Random.Range(0, 5));
            }

            int nbBush = Random.Range(200, 500);
            for (int j = 0; j < nbBush; j++)
            {
                float x = Random.Range(-80f, 80f);
                float y = Random.Range(-80f, 80f);

                GameObject t = Instantiate(bushes[Random.Range(0, bushes.Count)]) as GameObject;
                float      rX = Mathf.Clamp(v.x * 10 + x, 0, widthOfTheWorld * 10), rY = Mathf.Clamp(v.y * 10 + y, 0, lengthOfTheWorld * 10);
                t.transform.position = new Vector3(rX, 0, rY);
                float size = Random.Range(0.5f, 1.5f);
                t.transform.localScale = new Vector3(size, size, size);
            }
        }

        for (int k = 0; k < nbMountains; k++)
        {
            int nbRocks = Random.Range(25, 100);
            for (int l = 0; l < nbRocks; l++)
            {
                Vector2    rockPos  = new Vector2(Random.Range(-80f, 80f), Random.Range(-80f, 80f));
                GameObject t        = Instantiate(rocks[Random.Range(0, rocks.Count)]) as GameObject;
                Vector2    rRockPos = rockPos + (mountainsPositions[k] * 10);
                rRockPos             = new Vector2(Mathf.Clamp(rRockPos.x, 0, widthOfTheWorld * 10 - 1), Mathf.Clamp(rRockPos.y, 0, lengthOfTheWorld * 10 - 1));
                t.transform.position = new Vector3(rRockPos.x, getYOn2DPos(world, rRockPos), rRockPos.y);
                float size = Random.Range(2f, 5f);
                t.transform.localScale = new Vector3(size, size, size);
                t.transform.rotation   = new Quaternion(0, Random.Range(0, 360), 0, 0);
            }
        }


        /*for (int i = 0; i < widthOfTheWorld; i++)
         * {
         *  for (int j = 0; j < lengthOfTheWorld; j++)
         *  {
         *      for (int x = 0; x < 10; x++)
         *      {
         *          for (int y = 0; y < 10; y++)
         *          {
         *
         *              GameObject g = Instantiate(grasses[Random.Range(0, grasses.Count)]) as GameObject;
         *              g.transform.position = new Vector3(i * 10 + x + Random.Range(-0.5f, 0.5f), 1, j * 10 + y + Random.Range(-0.5f, 0.5f));
         *              g.transform.rotation = Quaternion.Euler(0, 0, 0);
         *
         *
         *
         *
         *          }
         *      }
         *  }
         * }*/



        creator.loadWorld(world);
    }