private IEnumerable <KeyValuePair <char, CharObj> > GetCharObjectPairs(IEnumerable <char> characters) { foreach (var character in characters) { if (!_blueprintTiles.TryGetValue(character, out var tile) || tile.Name == null) { continue; } var glyphColor = MonoGameExtensions.GetColorByString(tile.Foreground); if (glyphColor.A == 0) { continue; // Don't render transparent tiles on the fov window } yield return(new KeyValuePair <char, CharObj>(character, new CharObj(tile.Glyph, glyphColor, tile.Name))); } }
/// <summary> /// Retrieves the cells from the blueprint.txt file and blueprint.json config file. /// Cells are not cached by default. /// </summary> /// <returns></returns> public T[] GetCells() { var name = GetType().Name; var blueprintPath = Path.Combine(BlueprintPath, name + ".txt"); var blueprintConfigPath = Path.Combine(Constants.Blueprint.BlueprintsConfigPath, Constants.Blueprint.BlueprintTiles + ".json"); if (!File.Exists(blueprintPath) || !File.Exists(blueprintConfigPath) || !File.Exists(Constants.Blueprint.SpecialCharactersPath)) { return(Array.Empty <T>()); } var specialConfig = JsonConvert.DeserializeObject <BlueprintConfig>(File.ReadAllText(Constants.Blueprint.SpecialCharactersPath)); var specialChars = specialConfig.Tiles.ToDictionary(a => a.Glyph, a => a); var config = JsonConvert.DeserializeObject <BlueprintConfig>(File.ReadAllText(blueprintConfigPath)); var tiles = config.Tiles.ToDictionary(a => (char?)a.Glyph, a => a); var nullTile = BlueprintTile.Null(); foreach (var tile in tiles) { if (tile.Key == null) { continue; } if (specialChars.ContainsKey(tile.Key.Value)) { throw new Exception("Glyph '" + tile.Key.Value + "': is reserved as a special character and cannot be used in " + name); } } var blueprint = File.ReadAllText(blueprintPath).Replace("\r", "").Split('\n'); var cells = new List <T>(); for (int y = 0; y < GridSizeY; y++) { for (int x = 0; x < GridSizeX; x++) { char?charValue; if (y >= blueprint.Length || x >= blueprint[y].Length) { charValue = null; } else { charValue = blueprint[y][x]; } var position = new Point(x, y); BlueprintTile tile = nullTile; if (charValue != null && !tiles.TryGetValue(charValue, out tile)) { throw new Exception("Glyph '" + charValue + "' was not present in the config file for blueprint: " + name); } var foregroundColor = MonoGameExtensions.GetColorByString(tile.Foreground); var backgroundColor = tile.Background != null?MonoGameExtensions.GetColorByString(tile.Background) : Color.Black; var cell = new T() { Glyph = tile.Glyph, Position = position, Foreground = foregroundColor, Background = backgroundColor, CellProperties = new EmberCell.EmberCellProperties { NormalForeground = foregroundColor, NormalBackground = backgroundColor, ForegroundFov = foregroundColor == Color.Transparent ? Color.Transparent : Color.Lerp(foregroundColor, Color.Black, .5f), BackgroundFov = backgroundColor == Color.Transparent ? Color.Transparent : Color.Lerp(backgroundColor, Color.Black, .5f), Walkable = tile.Walkable, Interactable = tile.Interactable, Name = tile.Name, BlocksFov = tile.BlocksFov, }, LightProperties = new EmberCell.LightEngineProperties { EmitsLight = tile.EmitsLight, LightRadius = tile.LightRadius, Brightness = tile.Brightness } }; if (!string.IsNullOrWhiteSpace(tile.LightColor)) { cell.LightProperties.LightColor = MonoGameExtensions.GetColorByString(tile.LightColor); } cells.Add(cell); } } return(cells.ToArray()); }