Building Game Engines
TileMap Class Reference

Define the tilemap functions and variables. More...

#include <TileMap.hpp>

Inheritance diagram for TileMap:
Inheritance graph
Collaboration diagram for TileMap:
Collaboration graph

Public Member Functions

 TileMap (std::string tileSheetFileName, int rows, int cols, int _TileWidth, int _TileHeight, int _TileWindowWidth, int _TileWindowHeight, int _mapX, int _mapY, SDL_Renderer *ren)
 Constructor. More...
 
 ~TileMap ()
 Destructor. More...
 
void GenerateSimpleMap (const char *const filePath)
 Read the data in local tile sheet and call SetTile(). More...
 
void PrintMap ()
 Output the value in m_Tiles into console. More...
 
void SetTile (int x, int y, int type)
 Set type of each tile. More...
 
void UpdateTile (int x, int y, int type)
 Update the type of a specified tile. More...
 
int GetTileType (int x, int y)
 Get the type of a specified tile. More...
 
int CheckCollidable (int x, int y)
 Determines whether the given tile is a collidable tile. More...
 
int * GetTileArr ()
 Get the array which stores the whole tilemap. More...
 
void Render (SDL_Renderer *ren, Camera *camera)
 Render something to the active viewport. More...
 
void SetupTileMap ()
 Set the value of all the tiles to -1. More...
 
void SaveTileMap (const char *const filePath)
 Save current tilemap to local file. More...
 
void addTile (int srcRow, int srcCol, int destRow, int destCol)
 Change the type of a specifed tile. More...
 
int GetBeneathGround (int x, int y)
 Given enemy pos, return the landable tile pos. More...
 
int RayCast (int x1, int y1, int x2, int y2)
 Create a raycast to detect player's position. More...
 
int fastDist (int dx, int dy)
 fastly approximate how many tiles there are between 2 tiles. ref: https://stackoverflow.com/questions/7913511/distance-approximation More...
 
bool CheckWalkable (int x, int y, int movingRight)
 check if the given tile is walkable More...
 
Position generateRandomDungeon ()
 generate a random dungeon with the BSP Map Generator More...
 
void autoTiling ()
 automatically redraw the tile map to make the whole map look nicer More...
 
void autoTileGrid (int x, int y)
 look at the 3*3 area around the current grid to determine the proper tile to draw More...
 
- Public Member Functions inherited from Component
 Component ()
 
 ~Component ()
 

Detailed Description

Define the tilemap functions and variables.

This class offers functions to initialize, update as well as download the whole map.

Constructor & Destructor Documentation

◆ TileMap()

TileMap::TileMap ( std::string  tileSheetFileName,
int  rows,
int  cols,
int  _TileWidth,
int  _TileHeight,
int  _TileWindowWidth,
int  _TileWindowHeight,
int  _mapX,
int  _mapY,
SDL_Renderer *  ren 
)

Constructor.

Parameters
tileSheetFileNameThe name of the source file which stores the tile sheet.
rowsHow many rows are in the tile sheet.
colsHow many columns are in the tile sheet.
_TileWidthEach tile's width in pixel in source file.
_TileHeightEach tile's height in pixel in source file.
_TileWindowWidthEach tile's width in pixel in game.
_TileWindowHeightEach tile's height in pixel in game.
_mapXTotal cols of tiles in the current map.
_mapYTotal rows of tiles in the current map.
renReference to SDL renderer.

◆ ~TileMap()

TileMap::~TileMap ( )

Destructor.

Member Function Documentation

◆ addTile()

void TileMap::addTile ( int  srcRow,
int  srcCol,
int  destRow,
int  destCol 
)

Change the type of a specifed tile.

Change the type of a specified tile based on the selected tile. This function only works in Tilemap editor page.

Parameters
srcRowThe number of rows of the selected tile.
srcColThe number of columns of the selected tile.
destRowThe number of rows of the tile you want to modify.
destColThe number of columns of the tile you want to modify.

◆ autoTileGrid()

void TileMap::autoTileGrid ( int  x,
int  y 
)

look at the 3*3 area around the current grid to determine the proper tile to draw

◆ autoTiling()

void TileMap::autoTiling ( )

automatically redraw the tile map to make the whole map look nicer

◆ CheckCollidable()

int TileMap::CheckCollidable ( int  x,
int  y 
)

Determines whether the given tile is a collidable tile.

◆ CheckWalkable()

bool TileMap::CheckWalkable ( int  x,
int  y,
int  movingRight 
)

check if the given tile is walkable

◆ fastDist()

int TileMap::fastDist ( int  dx,
int  dy 
)

fastly approximate how many tiles there are between 2 tiles. ref: https://stackoverflow.com/questions/7913511/distance-approximation

Parameters
dxX-coordinate difference.
dyY-coordinate difference.

◆ generateRandomDungeon()

Position TileMap::generateRandomDungeon ( )

generate a random dungeon with the BSP Map Generator

Returns
the start position of the dungeon

◆ GenerateSimpleMap()

void TileMap::GenerateSimpleMap ( const char *const  filePath)

Read the data in local tile sheet and call SetTile().

Parameters
filePathThe file name of the local tile sheet.

◆ GetBeneathGround()

int TileMap::GetBeneathGround ( int  x,
int  y 
)

Given enemy pos, return the landable tile pos.

Check the tile under the character.

Parameters
xThe number of rows of the tile based on player's position.
yThe number of columns of the tile based on player's position..
Returns
The first tile under the player.

◆ GetTileArr()

int * TileMap::GetTileArr ( )

Get the array which stores the whole tilemap.

Returns
A pointer of an array.

◆ GetTileType()

int TileMap::GetTileType ( int  x,
int  y 
)

Get the type of a specified tile.

Parameters
xThe number of columns of this tile.
yThe number of rows of this tile.
Returns
The type of this tile.

◆ PrintMap()

void TileMap::PrintMap ( )

Output the value in m_Tiles into console.

◆ RayCast()

int TileMap::RayCast ( int  x1,
int  y1,
int  x2,
int  y2 
)

Create a raycast to detect player's position.

Check is p(x1, y1) could see p(x2, y2)

Parameters
x1X-coordinate of this enemy.
y1Y-coordinate of this enemy.
x2X-coordinate of the player.
y2Y-coordinate of the player.
Returns
The distance between this enemy and the player.

◆ Render()

void TileMap::Render ( SDL_Renderer *  ren,
Camera camera 
)

Render something to the active viewport.

Parameters
renReference to SDL renderer
cameraReference to Camera

◆ SaveTileMap()

void TileMap::SaveTileMap ( const char *const  filePath)

Save current tilemap to local file.

Parameters
filePathReference to the target file name of this tilemap.

◆ SetTile()

void TileMap::SetTile ( int  x,
int  y,
int  type 
)

Set type of each tile.

Parameters
xThe number of columns of the current tile.
yThe number of rows of the current tile.
typeThe type of the current tile.

◆ SetupTileMap()

void TileMap::SetupTileMap ( )

Set the value of all the tiles to -1.

◆ UpdateTile()

void TileMap::UpdateTile ( int  x,
int  y,
int  type 
)

Update the type of a specified tile.

Parameters
xThe number of columns of this tile.
yThe number of rows of this tile.
typeThe new type of this tile.