void MapData::SetTileInfo(float x, float y, int tile, int layer, int id) { Tile* temp; switch (layer) { case 1: { temp = &mLayer1[tile]; break; } case 2: { temp = &mLayer2[tile]; break; } case 3: { temp = &mLayer3[tile]; break; } case 4: { temp = &mLayer4[tile]; break; } default: { break; } } temp->SetPosition(SVector2(x, y)); temp->SetType(id); }
void MapLayerTiled::Initialize(u32 tilesWidth, u32 tilesHeight) { tilesWidth = tilesWidth < iViewTilesWidth ? iViewTilesWidth : tilesWidth; tilesHeight = tilesHeight < iViewTilesHeight ? iViewTilesHeight : tilesHeight; if (pTiles) { for (u32 y = 0; y < iTilesHeight; y++) { for (u32 x = 0; x < iTilesWidth; x++) { Delete(TILE(x, y)); } } pMemoryManager->Free(pTiles); } iTilesWidth = tilesWidth; iTilesHeight = tilesHeight; pTiles = (Tile **)pMemoryManager->Alloc(iTilesWidth * iTilesHeight * sizeof(Tile *), pDefaultPool, "Grid", "MapLayerTiled"); for (u32 y = 0; y < iTilesHeight; y++) { for (u32 x = 0; x < iTilesWidth; x++) { Tile *tile = New(Tile); TILE(x, y) = tile; if (pTileData) tile->SetType(pTileData[(x) + (iTilesWidth * (y))]); else tile->SetType(0); } } iMapWidth = ptiTileSize.x * iTilesWidth; iMapHeight = ptiTileSize.y * iTilesHeight; iViewWidth = ptiTileSize.x * iViewTilesWidth; iViewHeight = ptiTileSize.y * iViewTilesHeight; this->UpdateTiles(0, 0); }
void MapLayerTiled::SetTileData(const u32 *data) { pTileData = data; if (pTiles) { for (u32 y = 0; y < iTilesHeight; y++) { for (u32 x = 0; x < iTilesWidth; x++) { Tile *tile = TILE(x, y); if (pTileData) tile->SetType(pTileData[(x) + (iTilesWidth * (y))]); else tile->SetType(0); } } } }
void Generator::PostProcessMap(Map& map) { // Sort tiles by connectivity std::map<std::pair<unsigned int, unsigned int>, unsigned int> tiles; unsigned int id = 0; for (Map::TileIterator itr = map.GetTilesBegin(); itr != map.GetTilesEnd(); ++itr) { if (!itr->second->IsBlocked()) { std::map<std::pair<unsigned int, unsigned int>, unsigned int>::iterator itr2 = tiles.find(itr->first); if (itr2 == tiles.end()) { tiles.insert(std::make_pair(itr->first, id)); PostProcessTile(map, *itr->second, tiles, id); ++id; } } } // Find largest connected area std::vector<unsigned int> count; for (unsigned int i = 0; i != id; ++i) { count.push_back(0); } for (std::map<std::pair<unsigned int, unsigned int>, unsigned int>::iterator itr = tiles.begin(); itr != tiles.end(); ++itr) { ++count[itr->second]; } unsigned int max = 0; unsigned int max_id = 0; for (unsigned int i = 0; i != id; ++i) { if (count[i] > max) { max = count[i]; max_id = i; } } // Blcok all tiles that aren't connected to the largest connected area for (std::map<std::pair<unsigned int, unsigned int>, unsigned int>::iterator itr = tiles.begin(); itr != tiles.end(); ++itr) { if (itr->second != max_id) { Tile* tile = map.GetTile(itr->first.first, itr->first.second); tile->SetType(TT_FOREST); } } }
Map* Generator::CreateMap(Game& game, unsigned int width, unsigned int height, unsigned int tile_width, unsigned int tile_height) { Map* map = new Map(game, width, height, tile_width, tile_height); // Generate water tiles on the edge of the map noise::module::Perlin perlin; perlin.SetSeed(Random::UInt()); perlin.SetNoiseQuality(noise::QUALITY_BEST); perlin.SetLacunarity(1.0f); Tile* tile; for (unsigned int x = 0; x != width; ++x) { double size = abs(perlin.GetValue(x * .02, 0, 0) * 16.0); for (unsigned int i = 0; i < size + 16; ++i) { tile = map->GetTile(x, i); if (tile) { tile->SetType(TT_WATER); } tile = map->GetTile(x, height - i - 1); if (tile) { tile->SetType(TT_WATER); } } } for (unsigned int y = 0; y != height; ++y) { double size = abs(perlin.GetValue(0, y * .02, 0) * 16.0); for (unsigned int i = 0; i < size + 16; ++i) { tile = map->GetTile(i, y); if (tile) { tile->SetType(TT_WATER); } tile = map->GetTile(width - i - 1, y); if (tile) { tile->SetType(TT_WATER); } } } // Generate forest or grass tiles on remaining tiles perlin.SetSeed(Random::UInt()); perlin.SetNoiseQuality(noise::QUALITY_BEST); perlin.SetLacunarity(1.0f); for (unsigned int x = 1; x != width; ++x) { for (unsigned int y = 1; y != height; ++y) { tile = map->GetTile(x, y); if (tile->GetType() != TT_WATER) { double value = perlin.GetValue(x * .02, y * .02, 0); if (value <= -.3 || value >= .3) { tile->SetType(TT_FOREST); } else { tile->SetType(TT_GRASS); } } } } // Remove unreachable tiles PostProcessMap(*map); return map; }