std::vector<std::vector<std::shared_ptr<GameObject>>> MapGenerator::placeGameObjects(std::vector<std::vector<TileType>>& baseMap, std::vector<Team>& teams) { auto objectGrid = std::vector<std::vector<std::shared_ptr<GameObject>>>(); int width = baseMap.size(); int height = baseMap[0].size(); // Reserve memory objectGrid.resize(width); for (int x = 0; x < width; ++x) { // Reserve memory for each column objectGrid[x].resize(height); for (int y = 0; y < height; ++y) { objectGrid[x][y] = nullptr; } } // Place spawn point for every team for (int i = 0; i < teams.size(); ++i) { // Get random empty tile auto pos = findEmptyTile(baseMap); // Create and place spawn point auto newSpawnPoint = SpawnPoint(teams[i].getId(), teams[i].getColour(), pos, "ant", "antAgent"); objectGrid[pos.x][pos.y] = std::make_shared<GameObject>(newSpawnPoint); // Add to the team's spawnPoint list teams[i].addSpawnPoint(newSpawnPoint); // Turn surrounding tiles into empty tiles for (int x = pos.x - 1; x <= pos.x + 1; ++x) { for (int y = pos.y - 1; y <= pos.y + 1; ++y) { baseMap[x][y] = TileType::empty; } } } return objectGrid; }
Vec2 GameMap::generateTileForCell() { int cellSize = _cellList.size(); int mapLen = _mapData.size(); int maxLoop = (mapLen > 100 ? 100 : mapLen); do { int tile = RandomHelper::random_int(0, mapLen-1); if (_mapData[tile] == GameConst::NUMBER_ID::ID_EMPTY) { int tileX = tile % _width; int tileY = int(tile / _width); return Vec2(tileX, tileY); } maxLoop--; } while (maxLoop > 0); // reach here mean that can not random a suitable tile pos // so find the suitable manually return findEmptyTile(); }