Time for action - the TileMap class - part 2

  1. Add methods dealing with locating map cells to the TileMap class:
    #region Information about Map Cells
    static public int GetCellByPixelX(int pixelX)
    {
    return pixelX / TileWidth;
    }
    static public int GetCellByPixelY(int pixelY)
    {
    return pixelY / TileHeight;
    }
    static public Vector2 GetCellByPixel(Vector2 pixelLocation)
    {
    return new Vector2(
    GetCellByPixelX((int)pixelLocation.X),
    GetCellByPixelY((int)pixelLocation.Y));
    }
    static public Vector2 GetCellCenter(int cellX, int cellY)
    {
    return new Vector2(
    (cellX * TileWidth) + (TileWidth / 2),
    (cellY * TileHeight) + (TileHeight / 2));
    }
    static public Vector2 GetCellCenter(Vector2 cell)
    {
    return GetCellCenter(
    (int)cell.X,
    (int)cell.Y);
    }
    static public Rectangle CellWorldRectangle(int cellX, int cellY)
    {
    return new Rectangle(
    cellX * TileWidth,
    cellY * TileHeight,
    TileWidth,
    TileHeight);
    }
    static public Rectangle CellWorldRectangle(Vector2 cell)
    {
    return CellWorldRectangle(
    (int)cell.X,
    (int)cell.Y);
    }
    static public Rectangle CellScreenRectangle(int cellX, int cellY)
    {
    return Camera.WorldToScreen(CellWorldRectangle(cellX, cellY));
    }
    static public Rectangle CellSreenRectangle(Vector2 cell)
    {
    return CellScreenRectangle((int)cell.X, (int)cell.Y);
    }
    static public bool CellIsPassable(int cellX, int cellY)
    {
    MapSquare square = GetMapSquareAtCell(cellX, cellY);
    if (square == null)
    return false;
    else
    return square.Passable;
    }
    static public bool CellIsPassable(Vector2 cell)
    {
    return CellIsPassable((int)cell.X, (int)cell.Y);
    }
    static public bool CellIsPassableByPixel(Vector2 pixelLocation)
    {
    return CellIsPassable(
    GetCellByPixelX((int)pixelLocation.X),
    GetCellByPixelY((int)pixelLocation.Y));
    }
    static public string CellCodeValue(int cellX, int cellY)
    {
    MapSquare square = GetMapSquareAtCell(cellX, cellY);
    if (square == null)
    return "";
    else
    return square.CodeValue;
    }
    static public string CellCodeValue(Vector2 cell)
    {
    return CellCodeValue((int)cell.X, (int)cell.Y);
    }
    #endregion
    
  2. Add methods for manipulating MapSquares to the TileMap class:
    #region Information about MapSquare objects
    static public MapSquare GetMapSquareAtCell(int tileX, int tileY)
    {
    if ((tileX >= 0) && (tileX < MapWidth) &&
    (tileY >= 0) && (tileY < MapHeight))
    {
    return mapCells[tileX, tileY];
    }
    else
    {
    return null;
    }
    }
    static public void SetMapSquareAtCell(
    int tileX,
    int tileY,
    MapSquare tile)
    {
    if ((tileX >= 0)&& (tileX < MapWidth) &&
    (tileY >= 0) && (tileY < MapHeight))
    {
    mapCells[tileX, tileY] = tile;
    }
    }
    static public void SetTileAtCell(
    int tileX,
    int tileY,
    int layer,
    int tileIndex)
    {
    if ((tileX >= 0) && (tileX < MapWidth) &&
    (tileY >= 0) && (tileY < MapHeight))
    {
    mapCells[tileX, tileY].LayerTiles[layer] = tileIndex;
    }
    }
    static public MapSquare GetMapSquareAtPixel(int pixelX, int
    pixelY)
    {
    return GetMapSquareAtCell(
    GetCellByPixelX(pixelX),
    GetCellByPixelY(pixelY));
    }
    static public MapSquare GetMapSquareAtPixel(Vector2 pixelLocation)
    {
    return GetMapSquareAtPixel(
    (int)pixelLocation.X,
    (int)pixelLocation.Y);
    }
    #endregion
    

What just happened?

Much of the code we need for dealing with the tile map is unchanged from our simpler tile engine, aside from the changes to accommodate our new cell and MapSquare terminology. We also use the MapSquare type when getting and setting the contents of map cells instead of integers.

The SetTileAtCell() method may seem out of place among the methods dealing with MapSquare objects. Its purpose is to provide a way to change the tile index of a single layer in a cell without repackaging the cell's entire MapSquare object. By passing SetTileAtCell() a cell location, layer number, and tile index, we can change the content of a single layer exactly what we will need to do when building the map editor.

Because the game engine will need easy access to the Passable and CodeValue members of a cell (without the need to deal with the tile layer values), we have created the shortcut methods CellIsPassable() and CellCodeValue(). When the time comes to move the player and enemy objects during game play, we will make extensive use of these methods to determine what map squares are accessible to game entities.

Drawing the tile map

We are now ready to assemble the code necessary to draw the enhanced tile map to the screen. We need to account for all three layers of the map, ensuring that each will be drawn in the proper relationship to the others the background layer appearing furthest away, the interactive layer drawn above it, and finally the foreground layer drawn nearest to the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.119.114