Exemple #1
0
const bool MapLoader::load( const std::string filename, MapLoader::callback tile_callback, MapLoader::callback object_callback ){
	auto app = Application::getInstance();
	Tmx::Map map;

	map.ParseFile(filename);

	if( map.HasError() ){
		app->getRenderSystem().getDebugConsole()<<"\n"<<"Error Loading "<<filename<<": "<<map.GetErrorText();
		return false;
	}

	// Load all of the tilesets
	boost::unordered_map< std::string, phoenix::TexturePtr > tileset_textures;
	for (int i = 0; i < map.GetNumTilesets(); ++i) {
		const Tmx::Tileset *tileset = map.GetTileset(i);

		auto t = app->getRenderSystem().loadTexture( tileset->GetImage()->GetSource() );

		tileset_textures.insert( std::pair<std::string, phoenix::TexturePtr>( tileset->GetName(), t) );

		app->getRenderSystem().getDebugConsole()<<"\n"<<"Loaded tileset texture "<<tileset->GetImage()->GetSource()<<" for "<<tileset->GetName();
	}

	// Go through each layer
	for (int i = 0; i < map.GetNumLayers(); ++i) {
		const Tmx::Layer *layer = map.GetLayer(i);
		bea::TilemapPtr tiles = 0;

		for (int x = 0; x < layer->GetWidth(); ++x) {
			for (int y = 0; y < layer->GetHeight(); ++y) {
				auto tile_id = layer->GetTileId(x, y);
				if( tile_id == 0 ) continue;

				const Tmx::Tileset *tileset = map.FindTileset(tile_id);
				if( tileset == 0 ) continue;

				// Create the tileset when we get to the first tile in the map 
				if( !tiles ){
					tiles = new bea::Tilemap( 
						app->getObjectManager() , 
						app->getRenderSystem().getBatchRenderer(), 
						layer->GetWidth(), layer->GetHeight(),
						tileset->GetTileWidth(), tileset->GetTileHeight(),
						tileset_textures[tileset->GetName()]
					);
					app->getRenderSystem().getDebugConsole()<<"\n"<<"Added tilemap layer "<<layer->GetName()<<" with tileset "<<tileset->GetName()<<" W:"<<tiles->getMapWidth()<<" H:"<<tiles->getMapHeight();
				}

				// finally, set the tile
				tile_id -= tileset->GetFirstGid();
				if(tiles) tiles->setTile( x, y, tile_id );

				// callback
				if( tile_callback ){
					bea::PropertyContainer props;
					props["id"] = tile_id;
					props["tilemap"] = tiles;
					props["x"] = x;
					props["y"] = y;
					props["layer"] = layer->GetName();
					props["tile_width"] = tileset->GetTileWidth();
					props["tile_height"] = tileset->GetTileHeight();

					auto tile = tileset->GetTile(tile_id);
					if( tile != 0 ){
						auto tile_props = tile->GetProperties();
						if( !tile_props.Empty() ){
							auto list = tile_props.GetList();
							for( auto pit = list.begin(); pit != list.end(); ++pit ){
								props[ pit->first ] = pit->second;
							}
						}
					}

					tile_callback(props);
				}
			}
		}
		
		if( tiles ) {
			tiles->setDepth( tiles->getDepth() + (float(i)/10.0f) );
			tiles->update();
		}
	}

	// Go through the object groups
	if( object_callback ){

		for (int i = 0; i < map.GetNumObjectGroups(); ++i) {
			const Tmx::ObjectGroup *objectGroup = map.GetObjectGroup(i);
			for (int j = 0; j < objectGroup->GetNumObjects(); ++j) {

				const Tmx::Object *object = objectGroup->GetObject(j);

				bea::PropertyContainer props;
				props["name"] = object->GetName();
				props["group"] = objectGroup->GetName();
				props["type"] = object->GetType();
				props["x"] = object->GetX();
				props["y"] = object->GetY();
				props["width"] = object->GetWidth();
				props["height"] = object->GetHeight();

				auto obj_props = object->GetProperties();
				if( !obj_props.Empty() ){
					auto list = obj_props.GetList();
					for( auto pit = list.begin(); pit != list.end(); ++pit ){
						props[ pit->first ] = pit->second;
					}
				}

				object_callback(props);
			}
		}
	}

	return true;
}
Exemple #2
0
bool Level::Load(const std::string& filename) {
  Tmx::Map map;
  map.ParseFile(filename);

  if(map.HasError()) {
    Debug::logger->message("Error while loading level %s: %s\n", filename.c_str(), map.GetErrorText().c_str());
    return false;
  }

  _width = map.GetWidth();
  _height = map.GetHeight();
  _tileWidth = map.GetTileWidth();
  _tileHeight = map.GetTileHeight();

  std::map<const Tmx::Tileset*, Tileset*> tilesetMap;

  for(int i = 0; i < map.GetNumTilesets(); i++) {
    const Tmx::Tileset* tmxTileset = map.GetTileset(i);

    Tileset* tileset = new Tileset(_tileWidth, _tileHeight);
    tileset->LoadImage(map.GetFilepath() + tmxTileset->GetImage()->GetSource());

    _tilesets.push_back(tileset);

    tilesetMap.insert(std::pair<const Tmx::Tileset*, Tileset*>(tmxTileset, tileset));
  }

  _collisions = new bool[_width * _height];
  for(int i = 0; i < (_width * _height); i++) {
    _collisions[i] = false;
  }

  for(int i = 0; i < map.GetNumLayers(); i++) {
    const Tmx::Layer* tmxLayer = map.GetLayer(i);

    if(!strcasecmp(tmxLayer->GetName().c_str(), "collision")) {
      for(int x = 0; x < _width; x++) {
        for(int y = 0; y < _height; y++) {
          Tmx::MapTile tile = tmxLayer->GetTile(x, y);
          _collisions[y * _width + x] = tile.tilesetId > -1;
        }
      }
      continue;
    }
    else if(!strcasecmp(tmxLayer->GetName().c_str(), "middle")) {
      _middleLayer = i;
    }

    Layer* layer = new Layer(
      tmxLayer->GetWidth(), tmxLayer->GetHeight(),
      _tileWidth, _tileHeight);

    for(int x = 0; x < layer->GetWidth(); x++) {
      for(int y = 0; y < layer->GetHeight(); y++) {
        Tmx::MapTile tmxTile = tmxLayer->GetTile(x, y);

        MapTile tile;
        if(tmxTile.tilesetId != -1) {
          const Tmx::Tileset* tmxTileset = map.GetTileset(tmxTile.tilesetId);
          tile.id = tmxTile.id;
          tile.tileset = tilesetMap.find(tmxTileset)->second;
        } else {
          tile.id = 0;
          tile.tileset = NULL;
        }
        layer->SetTile(x, y, tile);
      }
    }

    _layers.push_back(layer);
  }

  if(_middleLayer == -1) {
    _middleLayer = int(floor(float(_layers.size()) / 2.0f)); // <-- nasty
  }

  for(int i = 0; i < map.GetNumObjectGroups(); i++) {
    const Tmx::ObjectGroup* tmxGroup = map.GetObjectGroup(i);
    for(int j = 0; j < tmxGroup->GetNumObjects(); j++) {
      const Tmx::Object* tmxObject = tmxGroup->GetObject(j);
      if(!strncasecmp(tmxObject->GetName().c_str(), "NPC", 3)) {
        NPC* npc = new NPC(this);
        npc->LoadSprites(tmxObject->GetProperties().GetLiteralProperty("image").c_str());
        npc->SetXY(tmxObject->GetX(), tmxObject->GetY());
        _npcs.push_back(npc);
      }
      else if(!strncasecmp(tmxObject->GetName().c_str(), "Warp", 4)) {
        Warp* warp = new Warp();
        warp->SetXY(tmxObject->GetX(), tmxObject->GetY());
        warp->SetWidthHeight(tmxObject->GetWidth(), tmxObject->GetHeight());
        warp->SetTargetMap(tmxObject->GetProperties().GetLiteralProperty("map").c_str());
        warp->SetTargetX(tmxObject->GetProperties().GetNumericProperty("x") * 32);
        warp->SetTargetY(tmxObject->GetProperties().GetNumericProperty("y") * 32);
        _warps.push_back(warp);
      }
    }
  }

  std::map<std::string, std::string> mapProps = map.GetProperties().GetList();
  for(std::map<std::string, std::string>::iterator i = mapProps.begin(); i != mapProps.end(); ++i) {
    if(i->first == "BGM") {
      _bgm = musicManager.Load(i->second);
    }
  }

  return true;
}