public static LevelMap FromString(string token)
    {
        var parts = token.Split(Separator[0]);

        if (!parts[0].Equals(Header))
        {
            throw new ArgumentException("Invalid Token");
        }

        var name            = parts[1];
        var size            = parts[2].Substring(4).TrimStart('[').TrimEnd(']').Split(',');
        var width           = int.Parse(size[0]);
        var height          = int.Parse(size[1]);
        var levelMapBuilder = new LevelMapBuilder(name, width, height);

        var floor = parts[2];

        new TwoDimensionalIterator(width, height)
        .ForEach(p => levelMapBuilder.With(
                     new TilePoint(p.Item1, p.Item2),
                     MapPieceSymbol.Piece(floor[p.Item1 + p.Item2 * width].ToString())));

        var objects = parts[3];

        new TwoDimensionalIterator(width, height)
        .ForEach(p => levelMapBuilder.With(
                     new TilePoint(p.Item1, p.Item2),
                     MapPieceSymbol.Piece(objects[p.Item1 + p.Item2 * width].ToString())));

        return(levelMapBuilder.Build());
    }
    private string LayerToString(MapPiece[,] layer)
    {
        var sb = new StringBuilder();

        new TwoDimensionalIterator(_map.Width, _map.Height)
        .ForEach(p => sb.Append(MapPieceSymbol.Symbol(layer[p.Item1, p.Item2])));
        return(sb.ToString());
    }
Esempio n. 3
0
    public LevelMapBuilder WithPiece(TilePoint tile, MapPiece piece)
    {
        if (!MapPieceSymbol.IsObject(piece))
        {
            throw new ArgumentException($"{piece} is not an object piece.");
        }

        var range = new TilePoint(_floors.GetLength(0), _floors.GetLength(1));

        if (tile.X > _floors.GetLength(0) || tile.X < 0)
        {
            throw new ArgumentException($"{tile} is out of range {range} for {piece}");
        }
        if (tile.Y > _floors.GetLength(1) || tile.Y < 0)
        {
            throw new ArgumentException($"{tile} is out of range {range} for {piece}");
        }

        _objects[tile.X, tile.Y] = piece;
        return(this);
    }
Esempio n. 4
0
 public LevelMapBuilder With(TilePoint tile, MapPiece piece) => MapPieceSymbol.IsFloor(piece) ? WithFloor(tile, piece) : WithPiece(tile, piece);