Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Chances to spawn this obj on map
- public static Dictionary<MapObjects, float> SpawnChances = new Dictionary<MapObjects, float>()
- {
- { MapObjects.Dirt, 15f },
- { MapObjects.Rock, 5f },
- { MapObjects.Gemstone, 5f },
- { MapObjects.Empty, 3f }
- };
- public static Dictionary<MapObjects, char> Identificators = new Dictionary<MapObjects, char>()
- {
- { MapObjects.Dirt, 'd' },
- { MapObjects.Rock, 'r' },
- { MapObjects.Gemstone, 'g' },
- { MapObjects.Empty, ' ' }
- };
- public static List<string> GenerateNewChunk()
- {
- List<string> result = new List<string>();
- //generate chunks until one will be valid.
- do
- {
- result.Clear();
- float sumChances = 0f;
- foreach (var chance in Balance.SpawnChances)
- {
- sumChances += chance.Value;
- }
- for (int i = 0; i < Cnst.ChunkSize; i++)
- {
- string row = string.Empty;
- for (int j = 0; j < Cnst.MapWidth; j++)
- {
- var value = Random.Range(0, sumChances);
- //to make it extendable, we're not doing if-else statements, but making a loop through all map objects
- float offset = 0f;
- foreach (var chance in Balance.SpawnChances)
- {
- if (value <= chance.Value + offset)
- {
- row += Cnst.Identificators[chance.Key];
- break;
- }
- else
- {
- offset += chance.Value;
- }
- }
- }
- result.Add(row);
- }
- } while (!CheckChunk(result));
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement