Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. //Chances to spawn this obj on map
  2.     public static Dictionary<MapObjects, float> SpawnChances = new Dictionary<MapObjects, float>()
  3.         {
  4.             { MapObjects.Dirt, 15f },
  5.             { MapObjects.Rock, 5f },
  6.             { MapObjects.Gemstone, 5f },
  7.             { MapObjects.Empty, 3f }
  8.         };
  9. public static Dictionary<MapObjects, char> Identificators = new Dictionary<MapObjects, char>()
  10.         {
  11.             { MapObjects.Dirt, 'd' },
  12.             { MapObjects.Rock, 'r' },
  13.             { MapObjects.Gemstone, 'g' },
  14.             { MapObjects.Empty, ' ' }
  15.         };
  16.  
  17. public static List<string> GenerateNewChunk()
  18.     {
  19.         List<string> result = new List<string>();
  20.         //generate chunks until one will be valid.
  21.         do
  22.         {
  23.             result.Clear();
  24.             float sumChances = 0f;
  25.             foreach (var chance in Balance.SpawnChances)
  26.             {
  27.                 sumChances += chance.Value;
  28.             }
  29.  
  30.             for (int i = 0; i < Cnst.ChunkSize; i++)
  31.             {
  32.                 string row = string.Empty;
  33.                 for (int j = 0; j < Cnst.MapWidth; j++)
  34.                 {
  35.                     var value = Random.Range(0, sumChances);
  36.                     //to make it extendable, we're not doing if-else statements, but making a loop through all map objects
  37.                     float offset = 0f;
  38.                     foreach (var chance in Balance.SpawnChances)
  39.                     {
  40.                         if (value <= chance.Value + offset)
  41.                         {
  42.                             row += Cnst.Identificators[chance.Key];
  43.                             break;
  44.                         }
  45.                         else
  46.                         {
  47.                             offset += chance.Value;
  48.                         }
  49.                     }
  50.                 }
  51.                 result.Add(row);
  52.             }
  53.         } while (!CheckChunk(result));
  54.  
  55.         return result;
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement