void GridLayoutItem::removeItemView(int index) { if( m_listController ) { // Remove Item if(QGraphicsObject * itemViewToRemove = viewFromGridPos(gridPos(index))) { prepareGeometryChange(); removePair(itemViewToRemove); itemViewToRemove->deleteLater(); // Move Everything after index back one grid position for( int i = index + 1; i < m_listController->count() + 1; i++ ) { std::pair<int,int> oldPos = gridPos(i); QGraphicsObject * item = viewFromGridPos(oldPos); OS_ASSERT(item); std::pair<int,int> newPos = gridPos(i - 1); setItemViewGridPos(item,newPos); } } if( QGraphicsScene * _scene = scene() ) { _scene->setSceneRect(boundingRect()); } } }
void GridLayoutItem::insertItemView(int index) { if( m_listController ) { prepareGeometryChange(); // Move existing views forward one grid position for( int i = (m_listController->count() - 2); i >= index; i-- ) { std::pair<int,int> oldPos = gridPos(i); QGraphicsObject * item = viewFromGridPos(oldPos); std::pair<int,int> newPos = gridPos(i + 1); setItemViewGridPos(item,newPos); } // Create new item and position it at index grid position if( QGraphicsObject * item = createNewItemView(index) ) { std::pair<int,int> pos = gridPos(index); setItemViewGridPos(item,pos); } if( QGraphicsScene * _scene = scene() ) { _scene->setSceneRect(boundingRect()); } } }
// Calculates the path to the goal from the AI using the A* algorithm. // Returns a sf::vector2f vector where all elements represent the // position of a Cell. Pathfinder::PositionVector Pathfinder::getPath(sf::Vector2f startPos, sf::Vector2f endPos){ sf::Clock clock; Cell* endCell = GridManager::getInstance()->getClosestCell(endPos); GridManager::CellVector openList; GridManager::CellVector closedList; openList.push_back(GridManager::getInstance()->getClosestCell(startPos)); sf::Vector2f gridPos(*openList.back()->getGridPosition()); while ((closedList.empty() || closedList.back() != endCell) && !openList.empty()){ Cell* currentCell = getCellWithLowestFValue(&openList); closedList.push_back(currentCell); int cellIndex = closedList.back()->getIndex(); int index = closedList.back()->getIndex(); index -= GridManager::getInstance()->getGridSize2f().x + 1; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ if (indexIsInsideGrid(index)){ Cell* cell = GridManager::getInstance()->getCell(index); if (cell->getIsWalkable() && !vectorContains(&closedList, cell)){ if (!vectorContains(&openList, cell)){ cell->setParentCell(closedList.back()); cell->setGCost(calculateGValue(cell, i, j)); openList.push_back(cell); cell->setSpriteTexture(TextureManager::getInstance()->getTexture("Grid_B")); } else{ int gCost = calculateGValue(cell, i, j); if (gCost < cell->getGCost()){ cell->setParentCell(closedList.back()); cell->setGCost(gCost); } } } index++; } } index += GridManager::getInstance()->getGridSize2f().x - 3; } } PositionVector* path = getPath(endCell); int ms = clock.getElapsedTime().asMilliseconds(); std::cout << "Pathfinding took " << ms << "ms" << std::endl; GridManager::getInstance()->clearValues(); return *path; }
void HouseGenerator::GenerateAll(void) { // Warning: Not a very smart calculation on the number of houses allowed on the screen. Dimensions maxHouseDimensions(20, 20); XY numHouses = WORLD_DIMENSIONS / maxHouseDimensions; // Number of houses in X and Y direction numHouses.RoundToInt(); for (int i = 0; i < numHouses.x; i++) { for (int j = 0; j < numHouses.y; j++) { //HouseSize size = ComputeRandomSize(); const int numRooms = rand() % 3; // Compute the position of this house (in pixels) Coordinates gridPos(i, j); int separation(2); Coordinates position = gridPos * maxHouseDimensions + separation; House h(position, maxHouseDimensions, numRooms); } } }