mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
first draft collision handler get any
This commit is contained in:
parent
11a206fe0a
commit
b2a001e24d
@ -55,6 +55,28 @@ public:
|
|||||||
std::initializer_list<TeamLabel> const& teamLabels = {},
|
std::initializer_list<TeamLabel> const& teamLabels = {},
|
||||||
bool negateTeam = false);
|
bool negateTeam = false);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
*
|
||||||
|
* \brief Tests entity against all entities with the specified labels for a collision
|
||||||
|
* \details Tests the given entity against every other entity with the specified labels for intersections between their collison boxes.
|
||||||
|
* If the primary entity has no ColliderComponent, the equivalent of no collision is returned immediately, other entities are skipped
|
||||||
|
* if they don't have a ColliderComponent
|
||||||
|
* \param entity The primary entity to check against. Return values will be relative to this entity
|
||||||
|
* \param posMod Modifier to apply toposition before checking collisions. Example: (TODO: link player collision)
|
||||||
|
* \param groupLabels Entities need to have at least one listed GroupLabels to get checked against
|
||||||
|
* \param teamLabels Entities need to have one of the specified TeamLabels to get checked against
|
||||||
|
* \param negateTeam If set to true, entities will only be checked against if they **don't** have one of the specified TeamLabels
|
||||||
|
* \return `bool` true if any collision was found, otherwise false
|
||||||
|
* \return `Entity*` returns first entity with collision found
|
||||||
|
* \return `IntersectionBitSet` bitset of intersection, position `Direction` true if any part in direction collides
|
||||||
|
* \see GroupLabel
|
||||||
|
* \see TeamLabel
|
||||||
|
* \see Entity
|
||||||
|
* \see ColliderComponent
|
||||||
|
* \see IntersectionBitSet
|
||||||
|
* \see Direction
|
||||||
|
* \see Entity::getTeam()
|
||||||
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T getAnyIntersection(
|
T getAnyIntersection(
|
||||||
Entity* entity,
|
Entity* entity,
|
||||||
|
|||||||
@ -123,6 +123,7 @@ IntersectionBitSet CollisionHandler::getAnyIntersection<IntersectionBitSet>(
|
|||||||
std::initializer_list<TeamLabel> const& teamLabels,
|
std::initializer_list<TeamLabel> const& teamLabels,
|
||||||
bool negateTeam)
|
bool negateTeam)
|
||||||
{
|
{
|
||||||
|
if (!entity->hasComponent<ColliderComponent>()) return std::bitset<DIRECTION_C>();
|
||||||
IntersectionBitSet intersections;
|
IntersectionBitSet intersections;
|
||||||
for (auto& collider : getColliders(groupLabels, teamLabels)) {
|
for (auto& collider : getColliders(groupLabels, teamLabels)) {
|
||||||
intersections |= getIntersection(entity, collider->entity, posMod);
|
intersections |= getIntersection(entity, collider->entity, posMod);
|
||||||
@ -138,6 +139,7 @@ Entity* CollisionHandler::getAnyIntersection<Entity*>(
|
|||||||
std::initializer_list<TeamLabel> const& teamLabels,
|
std::initializer_list<TeamLabel> const& teamLabels,
|
||||||
bool negateTeam)
|
bool negateTeam)
|
||||||
{
|
{
|
||||||
|
if (!entity->hasComponent<ColliderComponent>()) return nullptr;
|
||||||
for (auto& collider : getColliders(groupLabels, teamLabels)) {
|
for (auto& collider : getColliders(groupLabels, teamLabels)) {
|
||||||
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
|
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
|
||||||
if (SDL_HasIntersection(&rect, &collider->collider)) {
|
if (SDL_HasIntersection(&rect, &collider->collider)) {
|
||||||
@ -146,3 +148,21 @@ Entity* CollisionHandler::getAnyIntersection<Entity*>(
|
|||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
bool CollisionHandler::getAnyIntersection<bool>(
|
||||||
|
Entity* entity,
|
||||||
|
Vector2D posMod,
|
||||||
|
std::initializer_list<GroupLabel> const& groupLabels,
|
||||||
|
std::initializer_list<TeamLabel> const& teamLabels,
|
||||||
|
bool negateTeam)
|
||||||
|
{
|
||||||
|
if (!entity->hasComponent<ColliderComponent>()) return false;
|
||||||
|
for (auto& collider : getColliders(groupLabels, teamLabels)) {
|
||||||
|
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
|
||||||
|
if (SDL_HasIntersection(&rect, &collider->collider)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user