Time for action - handling tiles

  1. Add methods to the TileMap class that relate to reading and setting the tile index associated with individual map squares:
    #region Information about Map Tiles
    static public int GetTileAtSquare(int tileX, int tileY)
    {
    if ((tileX >= 0) && (tileX < MapWidth) &&
    (tileY >= 0) && (tileY < MapHeight))
    {
    return mapSquares[tileX, tileY];
    }
    else
    {
    return -1;
    }
    }
    static public void SetTileAtSquare(int tileX, int tileY, int tile)
    {
    if ((tileX >= 0) && (tileX < MapWidth) &&
    (tileY >= 0) && (tileY < MapHeight))
    {
    mapSquares[tileX, tileY] = tile;
    }
    }
    static public int GetTileAtPixel(int pixelX, int pixelY)
    {
    return GetTileAtSquare(
    GetSquareByPixelX(pixelX),
    GetSquareByPixelY(pixelY));
    }
    static public int GetTileAtPixel(Vector2 pixelLocation)
    {
    return GetTileAtPixel(
    (int)pixelLocation.X,
    (int)pixelLocation.Y);
    }
    titleshandlingstatic public bool IsWallTile(int tileX, int tileY)
    {
    int tileIndex = GetTileAtSquare(tileX, tileY);
    if (tileIndex == -1)
    {
    return false;
    }
    return tileIndex >= WallTileStart;
    }
    static public bool IsWallTile(Vector2 square)
    {
    return IsWallTile((int)square.X, (int)square.Y);
    }
    static public bool IsWallTileByPixel(Vector2 pixelLocation)
    {
    return IsWallTile(
    GetSquareByPixelX((int)pixelLocation.X),
    GetSquareByPixelY((int)pixelLocation.Y));
    }
    #endregion
    

What just happened?

At the most basic level, we need to be able to determine the tile index associated with any particular square on the map. GetTileAtSquare() provides this information and the corresponding SetTileAtSquare() allows the index of any square to be changed.

For convenience, the GetTileAtPixel() methods combine the GetTileAtSquare() along with the GetSquareByPixel...() methods we have already established. They do not contain any additional processing themselves, but provide more convenient access to tile information rather than having to do the pixel to tile conversions in external code every time we want to access tile information.

Finally, IsWallTile() and IsWallTileByPixel() examine the contents of the given square and return true if the tile index is greater than or equal to the first defined wall tile index (WallTileStart). Again, we could do this check externally, but since we will often need to know if a tile is a wall, it is convenient to summarize all of the checking into a single set of methods.

Drawing the map

The TileMap class in Robot Rampage will not contain an Update() method, because there is nothing about the map that changes on a per-frame basis. Thus, all that remains to make the class functional is the ability to draw the map 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.138.122.4