diff --git a/include/CollisionHandler.h b/include/CollisionHandler.h index cf90905..b786c12 100644 --- a/include/CollisionHandler.h +++ b/include/CollisionHandler.h @@ -1,13 +1,17 @@ #pragma once +#include "Constants.h" #include "Entity.h" #include "SDL_rect.h" #include "SpriteComponent.h" #include "Vector2D.h" +#include "Manager.h" + +#include +#include #include class ColliderComponent; -class Manager; class Entity; constexpr uint8_t DIRECTION_C = 4; @@ -39,7 +43,7 @@ public: static IntersectionBitSet getIntersectionWithBounds(Entity* entity);// will fail if speed high enough to switch from no collision to full overlap in one tick static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod); - std::vector getColliders(GroupLabel groupLabel); // temporary function, remove once game.cpp cleaned up + std::vector getColliders(std::initializer_list const& groupLabels); // temporary function, remove once game.cpp cleaned up void update(); }; \ No newline at end of file diff --git a/include/Entity.h b/include/Entity.h index 630e2fe..afb871a 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -47,6 +47,7 @@ public: bool hasGroup(Group mGroup); void addGroup(Group mGroup); void delGroup(Group mGroup); + std::bitset getGroupBitSet(); void setTeam(TeamLabel teamLabel); TeamLabel getTeam(); diff --git a/include/Manager.h b/include/Manager.h index 0046c33..61a1c45 100644 --- a/include/Manager.h +++ b/include/Manager.h @@ -20,6 +20,8 @@ public: void addToTeam(Entity* mEntity, Team mTeam); std::vector& getTeam(Team mTeam); + std::vector getAll(); + Entity& addEntity(); private: diff --git a/src/CollisionHandler.cpp b/src/CollisionHandler.cpp index f8975d1..98173e2 100644 --- a/src/CollisionHandler.cpp +++ b/src/CollisionHandler.cpp @@ -94,11 +94,18 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V return intersections; } -std::vector CollisionHandler::getColliders(GroupLabel groupLabel) +std::vector CollisionHandler::getColliders(std::initializer_list const& groupLabels) { std::vector colliders; - for (auto& entity : manager.getGroup((size_t) groupLabel)) { + std::bitset groupBitSet; + for (auto& groupLabel : groupLabels) { + groupBitSet.set((size_t) groupLabel); + } + + for (auto& entity : manager.getAll()) { + if ((groupBitSet & entity->getGroupBitSet()).none()) + continue; if (!entity->hasComponent()) continue; colliders.emplace_back(&entity->getComponent()); diff --git a/src/Entity.cpp b/src/Entity.cpp index 6817071..bd9b79a 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -30,6 +30,11 @@ void Entity::delGroup(Group mGroup) groupBitSet[mGroup] = false; } +std::bitset Entity::getGroupBitSet() +{ + return groupBitSet; +} + void Entity::setTeam(TeamLabel teamLabel) { teamLabel = teamLabel; diff --git a/src/Manager.cpp b/src/Manager.cpp index 3759704..f3aa133 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -1,8 +1,10 @@ #include "Manager.h" #include +#include #include "Constants.h" +#include "Entity.h" void Manager::draw() { @@ -66,6 +68,15 @@ std::vector& Manager::getTeam(Team mTeam) return entitiesByTeam[mTeam]; } +std::vector Manager::getAll() +{ + std::vector entity_vec; + for (auto& entity_ptr : entities) { + entity_vec.emplace_back(entity_ptr.get()); + } + return entity_vec; +} + Entity& Manager::addEntity() { Entity* e = new Entity(*this); diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 12609dd..b14ecb9 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -60,31 +60,21 @@ void TransformComponent::update() if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) { IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); - } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); } IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { - intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); - } - for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) { + for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) { intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); } - if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) - positionChange.x = 0; + IntersectionBitSet intersections = (intersectionsX & IntersectionBitSet("0011")) | (intersectionsY & IntersectionBitSet("1100")); - if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) + if (intersections.test((size_t) direction::LEFT) || intersections.test((size_t) direction::RIGHT)) positionChange.x = 0; - if (intersectionsY.test((size_t) direction::UP) && positionChange.y < 0) - positionChange.y = 0; - - if (intersectionsY.test((size_t) direction::DOWN) && positionChange.y > 0) + if (intersections.test((size_t) direction::UP) || intersections.test((size_t) direction::DOWN)) positionChange.y = 0; }