bool Mob::IntersectsWall(sf::Rect<int> const& position, std::vector<std::vector<gen::Tile>> const& map){ for (int x = position.left/32 - 1, y = position.top/32 - 1; x < (position.left + position.width)/32 + 2; x++) { if (x > 0 && x < map.size()) { for (y = position.top/32 - 1; y < (position.top + position.height)/32 + 2; y++) { if (y > 0 && y < map[x].size()) { if (map[x][y].type == 2) { if (position.intersects(sf::Rect<int>(x*32, y*32, 32, 32))) { return true; } } } } } } return false; }
bool Building::intersects(sf::Rect<float> const& other) const { return other.intersects(bb); }
static bool isInRect(sf::Rect<T> rect) { return rect.contains(getPosition().x, getPosition().y); }
int Map::CheckCollision(sf::Rect<int> CheckRect, int X, int Y, int CheckType) { int Xmin = (int)(X / CellSize); int Xmax = (int)((X + CheckRect.width) / CellSize); int Ymin = (int)(Y / CellSize); int Ymax = (int)((Y + CheckRect.height) / CellSize); if(CheckType == 0) if (X < 0 || Y < 0) return 1;//0 returns 1.... if (CheckType == -1) if (X >= (MapWidth - 1) * 32 || Y >= (MapWidth - 1) * 32)//Shitty method of stoping players from leaving the room. return -1;//Outside Room CheckRect.left = X; CheckRect.top = Y; WCR.LimitVariable(0, MapWidth - 1, Xmin); WCR.LimitVariable(0, MapWidth - 1, Xmax); WCR.LimitVariable(0, MapHeight - 1, Ymin); WCR.LimitVariable(0, MapHeight - 1, Ymax); for (int i = Xmin; i <= Xmax; i++) for (int ii = Ymin; ii <= Ymax; ii++) { bool IsTrue = false; switch (CheckType)//Check for any { case -2:IsTrue = (MapMatrix[i][ii].objectType != 0); break; case 1:IsTrue = (MapMatrix[i][ii].objectType == 1 || MapMatrix[i][ii].objectType == 5); break;//solids case 0:break;//Nothing, don't search for empty space. Only search for map edge. default:IsTrue = (MapMatrix[i][ii].objectType == CheckType); break; } if (IsTrue)//Object here { sf::Rect<int> SolidBoundBox; SolidBoundBox.left = i * CellSize; SolidBoundBox.top = ii * CellSize; SolidBoundBox.width = CellSize; SolidBoundBox.height = CellSize; if (CheckRect.intersects(SolidBoundBox)) { if (!MapMatrix[i][ii].pixelPerfect) return MapMatrix[i][ii].objectType; else { //get image and player box overlap int Left_ = X - i * 32; int Top_ = Y - ii * 32; int SLeft_ = Left_; int STop_ = Top_; int Wid_ = CheckRect.width-1; int Hei_ = CheckRect.height - 1; if (Left_ < 0) { Wid_ += Left_; Left_ = 0; } if (Top_ < 0) { Hei_ += Top_; Top_ = 0; } /*if (Top_ > 46) Top_ = 46; if (Hei_ > 35) Hei_ = 35;*/ if (Wid_ > 31) Wid_ = 31; if (Hei_ > 31) Hei_ = 31; for (int f = Wid_; f >= Left_; f--)//<= ?? for (int ff = Hei_; ff >= Top_; ff--)//<= ?? { if (WCR.MMPtr->TileSets[MapMatrix[i][ii].tileSetID].CollisionMaps[MapMatrix[i][ii].tileID].getPixel(f, ff).a != 0 && WCR.PlrPtr->PlayerMask.getPixel(f - SLeft_, ff - STop_).a != 0) return MapMatrix[i][ii].objectType; } } } } } return false; }
sf::Vector2<T> rect_size(const sf::Rect<T>& r) { return sf::Vector2<T>(r.GetWidth(),r.GetHeight()); }
void BulletHandler::CheckCollision(std::vector<sf::Rect<float>> &platforms, std::vector<Player> &players, sf::Rect<float> levelBounds, sf::Uint32 stateIterator) { //Loop through the bullets for(size_t i = 0; i < projectileList.size(); i++) { bool playerHasCollided = false; //Deals with player collisions for(size_t k = 0; k < players.size(); k++) { if(playerNetworkData->playersActive[k]) { if(players[k].GetBounds().Intersects(projectileList[i]->GetBounds())) { ProjectileDeathPacket deathPacket; deathPacket.PackData(sharedConstants.GAME_STATE, projectileList[i]->GetBulletID(), projectileList[i]->GetPosition(), stateIterator, deathPacket); if(projectileList[i] != NULL) { deathsToSend.push_back(deathPacket); } playerHasCollided = true; players[k].SetHealth(players[k].GetHealth() - 10); //Check if the player is dead so the score can be incremented if(players[k].GetHealth() <= 0) { players[projectileList[i]->GetBulletOwner()].IncreaseScore(1); } //THIS ERASE IS SUPER INNEFICIANT AND SHOULD PROBABLY BE DONE WITH A SET TO NULL WITH SMARTER BULLET SPAWNING projectileList.erase(projectileList.begin() + i); break; } } } //This is done so you dont get a vector exception if you kill the bullet with a player above. Should probly be seperate like, checkLevelCollision, CheckPlayerCollision, etc if(playerHasCollided == true) { continue; } //Deals with destroying bullets if they goe outside the level if(levelBounds.Intersects(projectileList[i]->GetBounds()) == false) { ProjectileDeathPacket deathPacket; deathPacket.PackData(sharedConstants.GAME_STATE, projectileList[i]->GetBulletID(), projectileList[i]->GetPosition(), stateIterator, deathPacket); if(projectileList[i] != NULL) { deathsToSend.push_back(deathPacket); } //THIS ERASE IS SUPER INNEFICIANT AND SHOULD PROBABLY BE DONE WITH A SET TO NULL WITH SMARTER BULLET SPAWNING projectileList.erase(projectileList.begin() + i); continue; } //Loop through the platforms for(size_t j = 0; j < platforms.size(); j++) { if(platforms[j].Intersects(projectileList[i]->GetBounds())) { ProjectileDeathPacket deathPacket; deathPacket.PackData(sharedConstants.GAME_STATE, projectileList[i]->GetBulletID(), projectileList[i]->GetPosition(), stateIterator, deathPacket); if(projectileList[i] != NULL) { deathsToSend.push_back(deathPacket); } // THIS ERASE IS SUPER INNEFICIANT AND SHOULD PROBABLY BE DONE WITH A SET TO NULL WITH SMARTER BULLET SPAWNING projectileList.erase(projectileList.begin() + i); break; } } } }