DarkTornado

World Generator V2.0

May 25th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. public void Generate2()
  2.         {
  3.  
  4.            
  5.             Random rnd = new Random();
  6.             Queue<Vector2> heightChanges = new Queue<Vector2>();
  7.             Queue<Vector2> lastHeightChanges = new Queue<Vector2>();
  8.             int initialHeight = rnd.Next(1) + 2;
  9.             for (int y = 0; y < initialHeight; y++)
  10.             {
  11.                 cubes[0, y, 0] = new BlockGround(new Vector3(0, 10 * y, 0));
  12.             }
  13.  
  14.             for (int z = 1; z < _depth; z++)
  15.             {
  16.                 int currentHeight = GetHeight(0, z - 1);
  17.                 if (rnd.Next(4) == 0)
  18.                 {
  19.                     int heightDiff = rnd.Next(-1, 1);
  20.                     if (heightDiff == 0)
  21.                     {
  22.                         heightDiff++;
  23.                     }
  24.                     if (rnd.Next(100) == 0)
  25.                     {
  26.                         heightDiff *= 2;
  27.                     }
  28.                     heightChanges.Enqueue(new Vector2(z, heightDiff));
  29.                     currentHeight += heightDiff;
  30.                     currentHeight = Math.Min(_height, Math.Max(1, currentHeight));
  31.                 }
  32.                 for (int y = 0; y < currentHeight; y++)
  33.                 {
  34.                     cubes[0, y, z] = new BlockGround(new Vector3(0, 10 * y, 10 * z));
  35.                 }
  36.  
  37.             }
  38.             for (int x = 1; x < _width; x++)
  39.             {
  40.                 int zeroHeight = GetHeight(x, 0);
  41.                 lastHeightChanges = heightChanges;
  42.                 heightChanges = new Queue<Vector2>();
  43.                 for (int z = 0; z < _depth; z++)
  44.                 {
  45.                     int currentHeight;
  46.                     if (z == 0)
  47.                     {
  48.                         currentHeight = GetHeight(x - 1, z);
  49.                     }
  50.                     else
  51.                     {
  52.                         currentHeight = GetHeight(x, z - 1);
  53.                     }
  54.  
  55.                     if (lastHeightChanges.Count > 0 && lastHeightChanges.Peek().X == z)
  56.                     {
  57.                         currentHeight += (int)lastHeightChanges.Peek().Y;
  58.                         lastHeightChanges.Dequeue();
  59.                     }
  60.  
  61.                     if (rnd.Next(4) == 0)
  62.                     {
  63.                         int heightDiff = rnd.Next(-1, 1);
  64.                         if (heightDiff == 0)
  65.                         {
  66.                             heightDiff++;
  67.                         }
  68.                         if (rnd.Next(100) == 0)
  69.                         {
  70.                             heightDiff *= 2;
  71.                         }
  72.                         heightChanges.Enqueue(new Vector2(z, heightDiff));
  73.                     }
  74.                     currentHeight = Math.Min(_height, Math.Max(1, currentHeight));
  75.                     currentHeight = Math.Min(zeroHeight + 3, Math.Max(zeroHeight - 3, currentHeight));
  76.                     for (int y = 0; y < currentHeight; y++)
  77.                     {
  78.                         if(y < currentHeight - 1)
  79.                             cubes[x, y, z] = new BlockGround(new Vector3(10 * x, 10 * y, 10 * z));
  80.                         else
  81.                             cubes[x, y, z] = new BlockGrass(new Vector3(10 * x, 10 * y, 10 * z));
  82.  
  83.                     }
  84.                 }
  85.             }
  86.             AddAllCubes();
  87.         }
Advertisement
Add Comment
Please, Sign In to add comment