Example #1
0
std::vector<Entity*>
GetEntities(
	const Tmx::Map& aMap,
	int roomX,
	int roomY,
	int roomW,
	int roomH,
	std::string		aLayerName)
{

	const std::vector<Tmx::Layer*> layers = aMap.GetLayers();
	Tmx::Layer* layer = 0;

	for (unsigned int i = 0; i < layers.size(); i++)
	{
		if (layers[i]->GetName() == aLayerName)
		{
			layer = layers[i];
			break;
		}
	}

	std::vector<Entity*> entities;

	if (layer == 0) {
		return entities;
	}

	for (int y = 0; y < roomH; y++) {
		for (int x = 0; x < roomW; x++) {
			Tmx::MapTile tile = layer->GetTile(x + roomX, y + roomY);
			const Tmx::Tileset *tileset = aMap.FindTileset(tile.gid);

			if (!tileset) {
				continue;
			}

			int firstGid = tileset->GetFirstGid();
			int entityId = tile.gid - firstGid;

			Entity* entity = EntityFactory::create(entityId);

			if (!entity)
			{
				continue;
			}

			float2 pos(aMap.GetTileWidth() * x + aMap.GetTileWidth() / 2, aMap.GetTileHeight() * (y + 1));
			pos.y -= entity->getHalfSize().y;

			entity->setInitialPosition(pos);
			entities.push_back(entity);
		}
	}

	return entities;
}
Example #2
0
    void Map::drawMap(int x_offset, int y_offset, int scr_width, int scr_height) {
        const std::vector<Tmx::Layer*> &layers = this->map->GetLayers();
        int tile_id = 0;
        int tileset_i = -1;
        int tile_w = this->map->GetTileWidth();
        int tile_h = this->map->GetTileHeight();
        Tmx::Layer* layer;

        // Draw all layers from bottom to top.
        for(int i = 0; i < layers.size(); ++i){
            layer = layers[i];

            // Set tile edges of the layer portion to be drawn.
            int start_x = 0;
            int end_x = layer->GetWidth() - 1;
            int start_y = 0;
            int end_y = layer->GetHeight() - 1;

            // Clip off-screen map tiles.
            if(x_offset + scr_width < (layer->GetWidth() - 1) * tile_w) {
                end_x = (x_offset + scr_width) / tile_w;
            }
            if(y_offset + scr_height < (layer->GetHeight() - 1) * tile_h) {
                end_y = (y_offset + scr_height) / tile_h;
            }
            if(x_offset > tile_w) {
                start_x = x_offset / tile_w;
            }
            if(y_offset > tile_h) {
                start_y = y_offset / tile_h;
            }

            // Draw all tiles in this layer after clipping.
            for(int y = start_y; y <= end_y; ++y){
                for(int x = start_x; x <= end_x; ++x){
                    tile_id = layer->GetTile(x, y).id;
                    tileset_i = layer->GetTile(x,y).tilesetId;
                    int x_pos = x * tile_w - x_offset;
                    int y_pos = y * tile_h - y_offset;
                    if(tileset_i != -1){
                        this->drawTile(tile_id, tileset_i, x_pos, y_pos);
                    }
                }
            }
        }
    }