-
Notifications
You must be signed in to change notification settings - Fork 5
TiledMaps
Modar Nasser edited this page Sep 16, 2020
·
11 revisions
TiledMap is part of the tilemapping module
Tile mapping one of the most common technique we can find in games.
NasNas supports TiledMap rendering from Tiled tmx files exported in csv format.
Almost all Tiled features are implemented, except :
- Isometric and hexagonal maps
- Layers groups
- Image layers
- Tile objects in objectsgroups
- Tile collisions data
These features will be implemented in future versions.
Loading and drawing a tiled map is effortless in NasNas, let's see an example :
First of all, add a TiledMap
member variable to the Game class header.
Game::Game(){
auto* scene = createScene("main");
// let's suppose we want to load the level.tmx tiled map.
// it has 2 tile layers named bg and front, and an object layer named coins
this->tiled_map.loadFromFile("assets/level.tmx");
// iterate over point objects in the coin object layer
for (auto& object : this->tiled_map.getObjectLayer("coins")->allPoints()) {
// do something with the objects
}
// adding tile layers to the scene
scene->getDefaultLayer()->add(this->tiled_map.getTileLayer("bg"));
scene->getDefaultLayer()->add(this->tiled_map.getTileLayer("front"));
// we can also add an object layer to the Scene
// the objects will be drawn like how they appear in Tiled map editor
scene->getDefaultLayer()->add(this->tiled_map.getObjectLayer("coins"));
auto* camera = createCamera("main", 0);
camera->lookAt(scene);
// we can set camera limits to be the tiled map bounds
camera->setLimitsRect({{0, 0}, sf::Vector2i(tiled_map.getSize())});
}
Game::update() {
this->tiled_map.update();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
getCamera("main")->move(1, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
getCamera("main")->move(0, 1);
// if (...) ... etc
}
This example will load a Tiled map, draw it on the screen and update its animated tiles. Press the arrow keys to move the Camera.
Other things you can do with the loaded tiled map :
- Get specific tile data
tiled_map.getTileLayer("layer_name")->getTile(10, 5).flip;
tiled_map.getTileLayer("layer_name")->getTile(10, 5).gid;
// etc
- Get tilesets data
tiled_map.allTilesets()[0]->firstgid;
tiled_map.allTilesets()[0]->getTexture();
auto tile_gid = tiled_map.getTileLayer("layer_name")->getTile(5, 10).gid;
tiled_map.getTileTileset(tile_gid)->name;
// etc
- Get properties (from map, layer, object, tile or tileset)
tiled_map.getProperty<std::string>("prop_name");
tiled_map.getTileLayer("layer_name")->getProperty<bool>("prop_name");
tiled_map.getObjectLayer("layer_name")->getEllipse(5).printProperties();
tiled_map.getTileLayer("layer_name")->getTile(10, 5).properties->addProperty("prop_name", sf::Color(0, 0, 0));
tiled_map.allTilesets()[0]->getProperty<float>("prop_name");
// etc
- Get objects shapes
// works also for any type of shape
tiled_map.getObjectLayer("layer_name")->getPolygon(6).getShape().getPointCount();
tiled_map.getObjectLayer("layer_name")->getEllipse(6).getShape().setFillColor(sf::Color::Red);
//etc
The tilemapping module is quite big, please check its documentation for more details