Beispiel #1
0
void Map::load(JSONValue &mapObject)
{
	// Check if the map is already loaded
	
	if (loaded)
		throw bit::Exception("Map is already loaded");
	
	// The map must be orthogonal
	
	if (mapObject["orientation"].toString() != "orthogonal")
		throw bit::Exception("Map must be orthogonal");
	
	// Set the tile dimensions
	
	tileSize.x = mapObject["tilewidth"].toInteger();
	tileSize.y = mapObject["tileheight"].toInteger();
	
	mapSize.x = mapObject["width"].toInteger();
	mapSize.y = mapObject["height"].toInteger();
	
	if (tileSize.x <= 0 || tileSize.y <= 0)
		throw bit::Exception("Tile dimensions must be greater than zero");
	
	// Build tilesets
	
	JSONValue tilesetArray = mapObject["tilesets"];
	int tilesetLength = tilesetArray.arrayLength();
	
	for (int index = 0; index < tilesetLength; ++index)
	{
		// Create tileset
		
		JSONValue tilesetObject = tilesetArray[index];
		loadTileset(tilesetObject);
	}
	
	// Build layers
	
	JSONValue layerArray = mapObject["layers"];
	int layerLength = layerArray.arrayLength();
	
	for (int index = 0; index < layerLength; ++index)
	{
		// Create layer
		
		JSONValue layerObject = layerArray[index];
		
		// The index (i.e. the order which the layer appears, zero-based)
		// will be its z-order number
		
		loadLayer(layerObject, index);
	}
	
	// Clear the temporary tiles map
	
	tiles.clear();
	
	loaded = true;
}
Beispiel #2
0
void MapManager::loadMap(JSONValue &mapObject)
{
	if (mapObject["orientation"].toString() != "orthogonal")
		throw bit::Exception("Map must be orthogonal");
	
	// Set the tile dimensions
	
	tileWidth = mapObject["tilewidth"].toInteger();
	tileHeight = mapObject["tileheight"].toInteger();
	
	mapWidth = mapObject["width"].toInteger();
	mapHeight = mapObject["height"].toInteger();
	
	// Build tilesets
	
	JSONValue tilesetArray = mapObject["tilesets"];
	int tilesetLength = tilesetArray.arrayLength();
	
	for (int index = 0; index < tilesetLength; ++index)
	{
		// Create tileset
		
		JSONValue tilesetObject = tilesetArray[index];
		loadTileset(tilesetObject);
	}
	
	// Build layers
	
	JSONValue layerArray = mapObject["layers"];
	int layerLength = layerArray.arrayLength();
	
	for (int index = 0; index < layerLength; ++index)
	{
		// Create layer
		
		JSONValue layerObject = layerArray[index];
		MapLayerPtr mapLayer(new MapLayer(this, layerObject));
		
		// The index (i.e. the order which the layer appears, zero-based)
		// will be its z-order number
		
		layers.insert(std::pair<int, MapLayerPtr>(index, mapLayer));
	}
}
Beispiel #3
0
void Map::loadLayer(JSONValue &layerObject, int zOrder)
{
	// Check that this is a tile layer
	
	std::string type = layerObject["type"].toString();
	
	if (type != "tilelayer")
		return;
	
	// Extract layer data
	
	std::string name = layerObject["name"].toString();
	
	int mapX = layerObject["x"].toInteger();
	int mapY = layerObject["y"].toInteger();
	int mapWidth = layerObject["width"].toInteger();
	int mapHeight = layerObject["height"].toInteger();
	int mapLength = mapWidth * mapHeight;
	
	// Extract tile data
	
	JSONValue dataArray = layerObject["data"];
	int dataLength = dataArray.arrayLength();
	
	// Check that the number of gids agrees with the map dimensions
	
	if (dataLength != mapLength)
		throw bit::Exception("Layer does not have the correct number of tiles");
	
	// Iterate through each tile
	
	for (int index = 0; index < dataLength; ++index)
	{
		int gid = dataArray[index].toInteger();
		
		// If gid is zero, don't make a Graphic
		
		if (gid == 0)
			continue;
		
		int pixelX = (mapX + index % mapWidth) * tileSize.x;
		int pixelY = (mapY + index / mapWidth) * tileSize.y;
		
		// Create the Graphic from the appropriate tile
		
		shared_ptr<SharedSprite> sprite(new SharedSprite);
		sprite->setTexture(getTile(gid));
		sprite->setPosition(pixelX, pixelY);
		
		// Insert the sprite in the graphics map
		
		std::pair<int, shared_ptr<SharedSprite> > spritePair(zOrder, sprite);
		sprites.insert(spritePair);
	}
}