bool World::canBePlacedAtCartesianPos(sf::Vector2i _pos){ //If out of bounds, return false immediately if (_pos.x > getDimensions().x - 1 || _pos.y > getDimensions().y - 1 || _pos.x < 0 || _pos.y < 0){ return false; } TerrainTile* here = terrainLayer[indexAtCartesianPos(_pos)].get(); if (here->getUnit() == nullptr){ return true; } else{ return false; } }
//Returns true if can be placed at this position bool World::canBePlacedAtPixelPos(sf::Vector2i _pos){ //If out of bounds, return false immediately if (_pos.x >= getDimensionsInPixels().x || _pos.y >= getDimensionsInPixels().y || _pos.x <= 0 || _pos.y <= 0){ return false; } TerrainTile* here = terrainLayer[indexAtPixelPos(_pos)].get(); if (here->getUnit() == nullptr){ return true; } else{ return false; } }
bool World::calculateViewDistance(UnitTile* unit, TerrainTile* target, bool randomisePerceivedPositions){ //IMPORTANT: //Due to the fact that Tile::getCartesianPos() bases its result on the physical location of the sprite, //which for units can be in disagreement with its real position, we use getTruePosition() instead. bool enemyFound{false}; sf::Vector2i currentPos = target->getCartesianPos(); int unitViewDistance = unit->getDefaultUnitViewDistance() - getWeatherUnitViewDistance(); int flagViewDistance = unit->getDefaultFlagViewDistance() - getWeatherFlagViewDistance(); if(getIsNighttime()){ unitViewDistance /= 2; flagViewDistance /= 2; } if(unitViewDistance < 1){ unitViewDistance = 1; } if(flagViewDistance < 1){ flagViewDistance = 1; } unit->setCurrentUnitViewDistance(unitViewDistance); unit->setCurrentFlagViewDistance(flagViewDistance); Player* owner = unit->getPlayer(); for (int y{-1 * unitViewDistance}; y <= unitViewDistance; ++y){ for(int x{-1 * unitViewDistance}; x <= unitViewDistance; ++x){ sf::Vector2i adjacentPos{currentPos.x + x, currentPos.y + y}; TerrainTile* terrainHere = terrainAtCartesianPos(adjacentPos); if(terrainHere == nullptr){ continue; } UnitTile* targetUnit = terrainHere->getUnit(); visibleTiles.insert(terrainHere); if (targetUnit != nullptr){ if(targetUnit->getPlayer() != owner){ if(!targetUnit->drawUnit){ enemyFound = true; targetUnit->drawUnit = true; } targetUnit->updateStats(randomisePerceivedPositions); } } } } for (int y{-1 * flagViewDistance}; y <= flagViewDistance; ++y){ for(int x{-1 * flagViewDistance}; x <= flagViewDistance; ++x){ sf::Vector2i adjacentPos{currentPos.x + x, currentPos.y + y}; TerrainTile* terrainHere = terrainAtCartesianPos(adjacentPos); if(terrainHere == nullptr){ continue; } UnitTile* targetUnit = terrainHere->getUnit(); if (targetUnit != nullptr){ if(targetUnit->getPlayer() != owner){ targetUnit->drawFlag = true; targetUnit->updateStats(randomisePerceivedPositions); } } } } return enemyFound; }