public static Level FromFile(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException("Could not find level file '" + fileName + "'"); } var sLevel = new List<string>(); using (StreamReader sr = new StreamReader(fileName)) { while (sr.Peek() >= 0) { sLevel.Add(sr.ReadLine()); } } if (0 == sLevel.Count) return null; int width = 0; foreach (string sLine in sLevel) { //find longest line if (sLine.Length > width) { width = sLine.Length; } } //use line count and the longest line as level dimensions Level level = new Level(width, sLevel.Count); int y = level.Height - 1; foreach (string sLine in sLevel) { int x = 0; //each character is a grid element foreach (char symbol in sLine) { ElementType type = ElementType.Floor; switch (symbol) { case '#': type = ElementType.Wall; break; case '-': type = ElementType.Floor; break; case '@': type = ElementType.Man; break; case '$': type = ElementType.Box; break; case '.': type = ElementType.Goal; break; case '*': type = ElementType.BoaxOnGoal; break; case '+': type = ElementType.ManOnGoal; break; }; level.SetElement(x, y, type); ++x; } --y; } return level; }
public static void MovePlayer(this Level level, Point oldPos, Point newPos) { ElementType type = level.GetElement(oldPos); switch (type) { case ElementType.Man: level.SetElement(oldPos, ElementType.Floor); break; case ElementType.ManOnGoal: level.SetElement(oldPos, ElementType.Goal); break; default: return; } ElementType type2 = level.GetElement(newPos); switch (type2) { case ElementType.Floor: level.SetElement(newPos, ElementType.Man); break; case ElementType.Goal: level.SetElement(newPos, ElementType.ManOnGoal); break; default: return; } }
public static Level FromString(string levelString) { if (string.IsNullOrWhiteSpace(levelString)) { return(null); } var lines = levelString.Split(new string[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries); int longestLength = lines.Max(s => s.Length); //use the longest line and line count as level dimensions Level level = new Level(longestLength, lines.Length); int y = level.Height - 1; foreach (string sLine in lines) { int x = 0; //each character is a grid element foreach (char symbol in sLine) { ElementType type = ElementType.Floor; switch (symbol) { case '#': type = ElementType.Wall; break; case '-': type = ElementType.Floor; break; case '@': type = ElementType.Man; break; case '$': type = ElementType.Box; break; case '.': type = ElementType.Goal; break; case '*': type = ElementType.BoaxOnGoal; break; case '+': type = ElementType.ManOnGoal; break; } ; level.SetElement(x, y, type); ++x; } --y; } return(level); }
public static void SetElement(this Level level, Point position, ElementType type) { level.SetElement(position.X, position.Y, type); }