public Level(LevelData data, IRandomGenerator random) { Data = data; _random = random; _animals = new Animal[Width, Height]; _tiles = new BaseTile[Width, Height]; for (var y = 0; y < Height; y++) { for (var x = 0; x < Width; x++) { switch (Data.Map[x, y]) { case 1: _tiles[x, y] = new SimpleTile(); break; } } } _possibleSwaps = new List<Swap>(); }
public LevelData Load(string filePath) { if (!filePath.EndsWith(".zbl")) filePath += ".zbl"; if (!File.Exists(filePath)) return LevelData.Empty; var index = 0; var source = File.ReadAllText(filePath, Encoding.UTF8); var data = new LevelData { Width = -1, Height = -1, Moves = -1, TargetScore = -1 }; if (string.IsNullOrWhiteSpace(source)) throw new Exception($"The map file '{filePath}' was empty."); IList<int> mapValueList = null; while (index < source.Length) { index = SkipWhitespace(source, index); string word; index = ReadWord(source, index, out word); word = word.ToLower(); if (!word.StartsWith("@")) throw new Exception("Expected '@'"); switch (word.Remove(0, 1)) { case "width": data.Width = ReadInteger(filePath, source, ref index, word); break; case "height": data.Height = ReadInteger(filePath, source, ref index, word); break; case "score": data.TargetScore = ReadInteger(filePath, source, ref index, word); break; case "moves": data.Moves = ReadInteger(filePath, source, ref index, word); break; case "map": mapValueList = ReadMap(source, ref index); break; } } if (data.Width == -1) data.Width = 9; if (data.Height == -1) data.Height = 9; if (data.TargetScore == -1) data.TargetScore = 1000; if (data.Moves == -1) data.Moves = 15; if (mapValueList == null) { mapValueList = new List<int>(); for (var i = 0; i < data.Width*data.Height; i++) mapValueList.Add(1); } data.Map = new int[data.Width, data.Height]; for (var y = 0; y < data.Height; y++) for (var x = 0; x < data.Width; x++) data.Map[x, y] = mapValueList[(data.Width*y) + x]; return data; }