#include "CollisionHandler.h" #include "ColliderComponent.h" #include "Constants.h" #include "Entity.h" #include "Manager.h" #include "Vector2D.h" #include #include #include #include IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB) { if (!entityA->hasComponent() || !entityB->hasComponent()) return std::bitset(); SDL_Rect colliderA = entityA->getComponent().collider; SDL_Rect colliderB = entityB->getComponent().collider; colliderA.x += posModA.x; colliderA.y += posModA.y; colliderB.x += posModB.x; colliderB.y += posModB.y; if (!SDL_HasIntersection( &colliderA, &colliderB)) return std::bitset(); std::bitset intersections; // checks all 4 directions to allow checking full overlap if (colliderA.x < colliderB.x + colliderB.w && colliderA.x > colliderB.x) { intersections.set((size_t) Direction::LEFT); } if (colliderA.x + colliderA.w < colliderB.x + colliderB.w && colliderA.x + colliderA.w > colliderB.x) { intersections.set((size_t) Direction::RIGHT); } if (colliderA.y < colliderB.y + colliderB.h && colliderA.y > colliderB.y) intersections.set((size_t) Direction::UP); if (colliderA.y + colliderA.h < colliderB.y + colliderB.h && colliderA.y + colliderA.h > colliderB.y) intersections.set((size_t) Direction::DOWN); return intersections; } IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, Vector2D posMod) { if (!entity->hasComponent()) return std::bitset(); SDL_Rect* collider = &entity->getComponent().collider; std::bitset intersections; // all 4 directions and both sides to allow checking for fully out of bounds if (collider->x + posMod.x < 0 || collider->x + posMod.x > SCREEN_SIZE_WIDTH) { intersections.set((size_t) Direction::LEFT); } if (collider->x + collider->w + posMod.x < 0 || collider->x + collider->w + posMod.x > SCREEN_SIZE_WIDTH) intersections.set((size_t) Direction::RIGHT); if (collider->y + posMod.y < 0 || collider->y + posMod.y > SCREEN_SIZE_HEIGHT) intersections.set((size_t) Direction::UP); if (collider->y + collider->h + posMod.y < 0 || collider->y + collider->h + posMod.y > SCREEN_SIZE_HEIGHT) intersections.set((size_t) Direction::DOWN); return intersections; } std::vector CollisionHandler::getColliders( std::initializer_list const& groupLabels, std::initializer_list const& teamLabels, bool negateTeam) { std::vector colliders; std::bitset groupBitSet; for (auto& groupLabel : groupLabels) { groupBitSet.set((size_t) groupLabel); } std::bitset teamBitSet; for (auto& teamLabel : teamLabels) { teamBitSet.set((size_t) teamLabel); } for (auto& entity : manager.getAll()) { if ((groupBitSet & entity->getGroupBitSet()).none()) continue; if (teamBitSet.any() && negateTeam != (teamBitSet.test((size_t) entity->getTeam()))) continue; if (!entity->hasComponent()) continue; colliders.emplace_back(&entity->getComponent()); } return colliders; } template<> IntersectionBitSet CollisionHandler::getAnyIntersection( Entity* entity, Vector2D posMod, std::initializer_list const& groupLabels, std::initializer_list const& teamLabels, bool negateTeam) { IntersectionBitSet intersections; for (auto& collider : getColliders(groupLabels, teamLabels)) { intersections |= getIntersection(entity, collider->entity, posMod); } return intersections; }; template<> Entity* CollisionHandler::getAnyIntersection( Entity* entity, Vector2D posMod, std::initializer_list const& groupLabels, std::initializer_list const& teamLabels, bool negateTeam) { for (auto& collider : getColliders(groupLabels, teamLabels)) { SDL_Rect rect = entity->getComponent().collider + posMod; if (SDL_HasIntersection(&rect, &collider->collider)) { return collider->entity; } } return nullptr; };