Vigilante Game Framework  1.0.0.8
A 2D/3D C++ Game Framework using SFML.
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
VTilemap Class Reference

Object that renders a 2D tilemap, with custom collision and autotiling support. More...

#include <VTilemap.h>

Inheritance diagram for VTilemap:
VObject VBase

Public Types

typedef VObject VSUPERCLASS
 Used to call parent class functions when they are overrided in class.
 
- Public Types inherited from VObject
enum  SidesTouching : unsigned char {
  TOUCHNONE = 0, TOUCHLEFT = 1 << 0, TOUCHRIGHT = 1 << 1, TOUCHTOP = 1 << 2,
  TOUCHBOTTOM = 1 << 3, TOUCHWALL = TOUCHLEFT | TOUCHRIGHT, TOUCHALL = TOUCHLEFT | TOUCHRIGHT | TOUCHTOP | TOUCHBOTTOM
}
 Flags for which sides are allowed to check if being overlapped for collision purposes.
 
typedef VBase VSUPERCLASS
 Used to call parent class functions when they are overrided in class.
 

Public Member Functions

void LoadFromCSV (const sf::String &mapData, const sf::String &graphicFile, int tileWidth=0, int tileHeight=0, bool autoTile=false, const std::vector< char > &collision={ '#' }, const sf::IntRect &graphicsArea=sf::IntRect())
 
void LoadFromArray (std::vector< char > mapData, int mapWidth, int mapHeight, const sf::String &graphicFile, int tileWidth=0, int tileHeight=0, bool autoTile=false, const std::vector< char > &collision={ '#' }, const sf::IntRect &graphicsArea=sf::IntRect())
 
void LoadFrom2DArray (std::vector< std::vector< char >> mapData, const sf::String &graphicFile, int tileWidth=0, int tileHeight=0, bool autoTile=false, const std::vector< char > &collision={ '#' }, const sf::IntRect &graphicsArea=sf::IntRect())
 
void SetTileRenderID (char ID, int tileNumber=0, int autoTileNumber=0)
 
void SetTileCollisionID (char ID, int AllowCollisions=TOUCHALL, std::function< void(VObject *, VObject *)> Callback=nullptr)
 
char GetTileID (unsigned int x, unsigned int y) const
 
char GetTileID (sf::Vector2i position) const
 
char GetTileIDFromPosition (sf::Vector2f tilemapPosition) const
 
void ChangeTile (int x, int y, char ID)
 
void ChangeTile (const std::vector< sf::Vector2u > &positions, char ID)
 
void ResetCollision (const std::vector< char > &collision={ '#' })
 
virtual bool OverlapWithCallback (VObject *object, std::function< bool(VObject *, VObject *)> Callback=nullptr, bool FlipCallback=false)
 
void SetTint (const sf::Color &color)
 
sf::Color const & GetTint ()
 Get overall tint of tilemap.
 
int const & GetMapWidth ()
 Get width of tilemap grid.
 
int const & GetMapHeight ()
 Get height of tilemap grid.
 
const VTileGetCollisionTile (unsigned int index)
 
const unsigned int GetCollisionTileCount ()
 
virtual void Reset (sf::Vector2f newPos) override
 Not used in VTilemap.
 
virtual void Reset (float x, float y) override
 Not used in VTilemap.
 
virtual void Hurt (float damage) override
 Not used in VTilemap.
 
virtual void Destroy () override
 Deletes and clears all the tilemap data.
 
virtual void Update (float dt) override
 
virtual void Draw (sf::RenderTarget &RenderTarget) override
 
- Public Member Functions inherited from VObject
 VObject (sf::Vector2f position, sf::Vector2f size=sf::Vector2f())
 
 VObject (float x=0, float y=0, float width=0, float height=0)
 
void SetPositionAtCentre (float x, float y)
 
void SetPositionAtCentre (sf::Vector2f newPos)
 
bool IsSolid ()
 
- Public Member Functions inherited from VBase
virtual void Kill ()
 
virtual void Revive ()
 Revives the object by setting both alive and exists back to true.
 

Public Attributes

sf::RenderStates RenderState = sf::RenderStates::Default
 RenderState for the Tilemap.
 
sf::Vector2u TileSize
 Size of an individual tile in the tilemap.
 
sf::Vector2f Scale = sf::Vector2f(1, 1)
 Scale of the tilemap.
 
bool AutoTile = false
 Sets the tilemap to use AutoTile mode or not.
 
- Public Attributes inherited from VObject
sf::Vector2f Position
 Position of the object.
 
sf::Vector2f Size
 Size of the object.
 
sf::Vector2f Last
 Position of the object on the previous frame.
 
float Radius = 0
 Radius amount for Circle Collisions.
 
float Angle = 0
 Angle of object.
 
float AngleVelocity = 0
 Velocity of rotation.
 
float AngleAcceleration = 0
 Acceleration of rotation.
 
float AngleDrag = 0
 Drag of rotation.
 
float AngleMax = 10000
 Maximum Velocity of rotation.
 
bool Moves = true
 If true, updates the objects position and angle.
 
sf::Vector2f Velocity
 Velocity of movement.
 
sf::Vector2f Acceleration
 Acceleration of movement.
 
sf::Vector2f Drag
 Drag of movement.
 
sf::Vector2f MaxVelocity
 Maximum velocity of movement.
 
sf::Vector2f ScrollFactor
 The amount the object will appear to move when the camera scrolls (1.0f = Scrolls as fast as the camera, 0.5f = Scrolls half as fast as the camera). Good for parallax scrolling.
 
float RotateFactor = 1
 The amount the object will appear to rotate when the camera rotates (1.0f = Rotates as fast as the camera, 0.5f = Rotates half as fast as the camera). Good for parallax scrolling.
 
float ZoomFactor = 1
 The amount the object will appear to scale when the camera zooms (1.0f = Scales as fast as the camera, 0.5f = Scales half as fast as the camera). Good for parallax scrolling.
 
float Elasticity = 0
 Bounciness of the object (0 assumes no bounce)
 
float Mass = 1
 Mass of object in comparison to other objects in world.
 
float Health = 100
 Standard health value (object dies if health is <= 0 when modified in the Hurt function)
 
bool Immovable = false
 If true, the object will not change its velocity on collision.
 
unsigned char Touching = SidesTouching::TOUCHNONE
 Which sides are currently touching (is reset on each Update call!)
 
unsigned char WasTouching = SidesTouching::TOUCHNONE
 Which sides on previous frame were touching (is reset on each Update call!)
 
unsigned char AllowCollisions = SidesTouching::TOUCHALL
 Which sides are allowed to check collisions for.
 
bool CollisionXDrag = true
 Useful for moving platforms.
 
- Public Attributes inherited from VBase
bool active = true
 Base can be updated using Update if true.
 
bool visible = true
 Base can be rendered using Draw if true.
 
bool alive = true
 Helper member that is mainly used filter out behaviours to objects which aren't alive in the scene.
 
bool exists = true
 Base can be updated and rendered if true, else it would no longer be treated as being part of the scene.
 
VType type = NONE
 Base object type, typically set in the constructor of an inherited object to specify what type it is.
 
uint32_t RefCount = 0
 Counts the amount of times the object is referenced in groups, ensures an object is only destroyed when no longer part of a single group.
 

Protected Member Functions

void setupTilemap (const sf::String &graphicFile, int tileWidth=0, int tileHeight=0, bool autoTile=false, const std::vector< char > &collision={ '#' }, const sf::IntRect &graphicsArea=sf::IntRect())
 
virtual void updateTransform () override
 Updates the transform of the tilemap.
 
void updateTilemap ()
 Updates the tilemap rendering info.
 
void updateCollisionBox ()
 Updates the collision data in the tiles list.
 
void clearTiles ()
 Clears all the tilemap data.
 
- Protected Member Functions inherited from VObject
virtual void updateMotion (float dt)
 

Protected Attributes

std::vector< char > tilemap
 Tilemap data.
 
std::map< char, VTileCollisionInfo * > collisionDir
 List of tile collision information.
 
std::map< char, VTileRenderInfo * > renderDir
 List of tile render information.
 
std::vector< int > autotile
 Autotile Map data (used when in AutoTile mode).
 
int tileMapWidth = 0
 The amount of tiles horizontally in a texture.
 
sf::Vector2u textureOffset
 The offset of the texture to use.
 
int mapWidth = 0
 Width of the Tilemap in Tiles.
 
int mapHeight = 0
 Height of the Tilemap in Tiles.
 
sf::Vector2f last
 Position during the last update of the collision boxes.
 
bool dirty = true
 Updates the collision and render infomation of the tilemap if true.
 
bool helperAutoTile = false
 Updates the render infomation of the tilemap if different from AutoTile.
 
sf::VertexArray vertices
 Vertex data for render.
 
sf::Color colour = sf::Color::White
 Colour Tint of rendered tilemap.
 
std::vector< VTile * > tiles
 List of Collideable Tilesets.
 

Additional Inherited Members

- Static Public Member Functions inherited from VObject
static bool separate (VObject *a, VObject *b)
 
static bool separateCircle (VObject *a, VObject *b)
 
static bool touchFlag (VObject *a, VObject *b)
 
static float computeVelocity (float v, float a, float d, float max, float dt)
 
static bool TestInView (const sf::View &renderTargetView, sf::View *defaultView, VObject *o, const sf::FloatRect &renderBox=sf::FloatRect())
 
- Static Public Member Functions inherited from VBase
static const wchar_t * VString (const char *fmt,...)
 
static void VLog (const char *fmt,...)
 
static void VLogError (const char *fmt,...)
 
static void VClearLog ()
 
- Static Public Attributes inherited from VObject
static float SeparateBias
 Adds to the maximum overlap distance between objects.
 

Detailed Description

Object that renders a 2D tilemap, with custom collision and autotiling support.

Member Function Documentation

◆ ChangeTile() [1/2]

void VTilemap::ChangeTile ( int  x,
int  y,
char  ID 
)

Change the tile at a specific point to an ID.

Parameters
xX Coordinate in Tiles.
yY Coordinate in Tiles.
IDThe new tile ID for that position in the tilemap.

◆ ChangeTile() [2/2]

void VTilemap::ChangeTile ( const std::vector< sf::Vector2u > &  positions,
char  ID 
)

Change a list of tiles to a single ID.

Parameters
positionsA list of coordinates to change the tile for.
IDThe new tile ID for that position in the tilemap.

◆ Draw()

virtual void VTilemap::Draw ( sf::RenderTarget &  RenderTarget)
overridevirtual

Draws the tilemap.

Parameters
RenderTargetThe game's sf::RenderTarget object to render data onto.

Reimplemented from VObject.

◆ GetCollisionTile()

const VTile* VTilemap::GetCollisionTile ( unsigned int  index)
Returns
Tile object used for collisions, size of each tile is based on an area of tiles with the same ID.

◆ GetCollisionTileCount()

const unsigned int VTilemap::GetCollisionTileCount ( )
Returns
The amount of tile objects for collisions.

◆ GetTileID() [1/2]

char VTilemap::GetTileID ( unsigned int  x,
unsigned int  y 
) const

Get tile at specified map coordinate.

Parameters
xX Coordinate in Tiles.
yY Coordinate in Tiles.
Returns
Tile ID as a char value. Returns \0 of coordinates are invalid.

◆ GetTileID() [2/2]

char VTilemap::GetTileID ( sf::Vector2i  position) const

Get tile at specified map coordinate.

Parameters
positionCoordinate in Tiles.
Returns
Tile ID as a char value. Returns \0 of coordinates are invalid.

◆ GetTileIDFromPosition()

char VTilemap::GetTileIDFromPosition ( sf::Vector2f  tilemapPosition) const

Get tile at specified world position.

Parameters
tilemapPositionGets a tile at a specific position in the world (Position / TileSize).
Returns
Tile ID as a char value. Returns \0 of coordinates are invalid.

◆ LoadFrom2DArray()

void VTilemap::LoadFrom2DArray ( std::vector< std::vector< char >>  mapData,
const sf::String &  graphicFile,
int  tileWidth = 0,
int  tileHeight = 0,
bool  autoTile = false,
const std::vector< char > &  collision = { '#' },
const sf::IntRect &  graphicsArea = sf::IntRect() 
)

Load map as a single two-dimensional char array.

Parameters
mapDataThe two-dimensional char array that makes up the tilemap data.
graphicFileThe path and filename of the texture file.
tileWidthFrame Width of a single tile.
tileHeightFrame Height of a single tile.
autoTileIf true, the tilemap will render as an autotiled map.
collisionA list of chars in the mapData that are completely collideable.
graphicsAreaThe area of the texture to display. Default is the entire texture.

◆ LoadFromArray()

void VTilemap::LoadFromArray ( std::vector< char >  mapData,
int  mapWidth,
int  mapHeight,
const sf::String &  graphicFile,
int  tileWidth = 0,
int  tileHeight = 0,
bool  autoTile = false,
const std::vector< char > &  collision = { '#' },
const sf::IntRect &  graphicsArea = sf::IntRect() 
)

Load map as a single char array.

Parameters
mapDataThe char array that makes up the tilemap data.
mapWidthMap Width in tiles.
mapHeightMap Height in tiles.
graphicFileThe path and filename of the texture file.
tileWidthFrame Width of a single tile.
tileHeightFrame Height of a single tile.
autoTileIf true, the tilemap will render as an autotiled map.
collisionA list of chars in the mapData that are completely collideable.
graphicsAreaThe area of the texture to display. Default is the entire texture.

◆ LoadFromCSV()

void VTilemap::LoadFromCSV ( const sf::String &  mapData,
const sf::String &  graphicFile,
int  tileWidth = 0,
int  tileHeight = 0,
bool  autoTile = false,
const std::vector< char > &  collision = { '#' },
const sf::IntRect &  graphicsArea = sf::IntRect() 
)

Load map from a text file. Maps should be formatted so each tile is separated by comma (,). The amount of tiles on each row makes up the width of the map, and amount of lines the height.

Example: 0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0 1,1,1,0,0,0,0,0,0,0,1,1,1 0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,1,1,1,1,1,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0 1,1,1,1,1,1,1,1,1,1,1,1,1

Parameters
mapDataThe path and filename of the csv/text file of the path data.
graphicFileThe path and filename of the texture file.
tileWidthFrame Width of a single tile.
tileHeightFrame Height of a single tile.
autoTileIf true, the tilemap will render as an autotiled map.
collisionA list of chars in the mapData that are completely collideable.
graphicsAreaThe area of the texture to display. Default is the entire texture.

◆ OverlapWithCallback()

virtual bool VTilemap::OverlapWithCallback ( VObject object,
std::function< bool(VObject *, VObject *)>  Callback = nullptr,
bool  FlipCallback = false 
)
virtual

When object overlaps tilemap, this function goes through all tiles to test overlap with specified overlap. This function is ideal to test if an object overlaps any of the collieable tiles instead of using VGlobal::Overlaps (which will only test if the object overlaps the tilemap area).

Parameters
objectVObject to test for overlap with the tilemap.
CallbackThe Callback function that is used if an overlap has been found in the tilemap area (not individual tile sets!).
FlipCallbackIf true, will swap the parameters when calling the Callback.

◆ ResetCollision()

void VTilemap::ResetCollision ( const std::vector< char > &  collision = { '#' })

Reset collision infomation to all colliding tiles having default collision properties. The default tile collision allows collisions on all sides with no callback.

Parameters
collisionList of tile IDs which are collideable.

◆ SetTileCollisionID()

void VTilemap::SetTileCollisionID ( char  ID,
int  AllowCollisions = TOUCHALL,
std::function< void(VObject *, VObject *)>  Callback = nullptr 
)

Sets tile with specific char to have certain collision restrictions and responses.

Parameters
IDThe tile ID as defined in the tilemap data.
AllowCollisionsA filter to set which sides of a tile and object can collide with.
CallbackA callback function to call when a player is either colliding or overlapping a tile with the specified ID.

◆ SetTileRenderID()

void VTilemap::SetTileRenderID ( char  ID,
int  tileNumber = 0,
int  autoTileNumber = 0 
)

Sets tile to render at specific char in tilemap (ignored if using autotile)

Parameters
IDThe tile ID as defined in the tilemap data.
tileNumberThe frame index the tile with the specific ID should use.
autoTileNumberThe autotile level the tile should used if in AutoTile (useful if you want to display more than one set of autotiles in a single tilemap).

◆ SetTint()

void VTilemap::SetTint ( const sf::Color &  color)

Set overall tint of tilemap.

Parameters
colorThe colour tint to set the tilemap to.

◆ setupTilemap()

void VTilemap::setupTilemap ( const sf::String &  graphicFile,
int  tileWidth = 0,
int  tileHeight = 0,
bool  autoTile = false,
const std::vector< char > &  collision = { '#' },
const sf::IntRect &  graphicsArea = sf::IntRect() 
)
protected

Sets up all the tilemap visual infomation.

Parameters
graphicFileThe filename and path of the texture file.
tileWidthFrame width of a tile.
tileHeightFrame height of a tile.
autoTileIf true the tilemap will be rendered using the tilemap mode.
collisionA list of tiles that are collideable in the tilemap data.
graphicsAreaThe area of the texture to use. Default is the entire texture area.

◆ Update()

virtual void VTilemap::Update ( float  dt)
overridevirtual

Updates the tilemap. Sets up new render and collision infomation if flags have been set.

Parameters
dtDelta Time between the current and previous frames of the game.

Reimplemented from VObject.


The documentation for this class was generated from the following file: