0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

minor improvements

This commit is contained in:
Benedikt Galbavy 2024-01-28 18:18:27 +01:00
parent 4755076c34
commit 28af8c9389
7 changed files with 39 additions and 19 deletions

View File

@ -1,13 +1,17 @@
#pragma once #pragma once
#include "Constants.h"
#include "Entity.h" #include "Entity.h"
#include "SDL_rect.h" #include "SDL_rect.h"
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "Vector2D.h" #include "Vector2D.h"
#include "Manager.h"
#include <bitset>
#include <initializer_list>
#include <vector> #include <vector>
class ColliderComponent; class ColliderComponent;
class Manager;
class Entity; class Entity;
constexpr uint8_t DIRECTION_C = 4; 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);// will fail if speed high enough to switch from no collision to full overlap in one tick
static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod); static IntersectionBitSet getIntersectionWithBounds(Entity* entity, Vector2D posMod);
std::vector<ColliderComponent*> getColliders(GroupLabel groupLabel); // temporary function, remove once game.cpp cleaned up std::vector<ColliderComponent*> getColliders(std::initializer_list<GroupLabel> const& groupLabels); // temporary function, remove once game.cpp cleaned up
void update(); void update();
}; };

View File

@ -47,6 +47,7 @@ public:
bool hasGroup(Group mGroup); bool hasGroup(Group mGroup);
void addGroup(Group mGroup); void addGroup(Group mGroup);
void delGroup(Group mGroup); void delGroup(Group mGroup);
std::bitset<MAX_GROUPS> getGroupBitSet();
void setTeam(TeamLabel teamLabel); void setTeam(TeamLabel teamLabel);
TeamLabel getTeam(); TeamLabel getTeam();

View File

@ -20,6 +20,8 @@ public:
void addToTeam(Entity* mEntity, Team mTeam); void addToTeam(Entity* mEntity, Team mTeam);
std::vector<Entity*>& getTeam(Team mTeam); std::vector<Entity*>& getTeam(Team mTeam);
std::vector<Entity*> getAll();
Entity& addEntity(); Entity& addEntity();
private: private:

View File

@ -94,11 +94,18 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V
return intersections; return intersections;
} }
std::vector<ColliderComponent*> CollisionHandler::getColliders(GroupLabel groupLabel) std::vector<ColliderComponent*> CollisionHandler::getColliders(std::initializer_list<GroupLabel> const& groupLabels)
{ {
std::vector<ColliderComponent*> colliders; std::vector<ColliderComponent*> colliders;
for (auto& entity : manager.getGroup((size_t) groupLabel)) { std::bitset<MAX_GROUPS> groupBitSet;
for (auto& groupLabel : groupLabels) {
groupBitSet.set((size_t) groupLabel);
}
for (auto& entity : manager.getAll()) {
if ((groupBitSet & entity->getGroupBitSet()).none())
continue;
if (!entity->hasComponent<ColliderComponent>()) if (!entity->hasComponent<ColliderComponent>())
continue; continue;
colliders.emplace_back(&entity->getComponent<ColliderComponent>()); colliders.emplace_back(&entity->getComponent<ColliderComponent>());

View File

@ -30,6 +30,11 @@ void Entity::delGroup(Group mGroup)
groupBitSet[mGroup] = false; groupBitSet[mGroup] = false;
} }
std::bitset<MAX_GROUPS> Entity::getGroupBitSet()
{
return groupBitSet;
}
void Entity::setTeam(TeamLabel teamLabel) void Entity::setTeam(TeamLabel teamLabel)
{ {
teamLabel = teamLabel; teamLabel = teamLabel;

View File

@ -1,8 +1,10 @@
#include "Manager.h" #include "Manager.h"
#include <algorithm> #include <algorithm>
#include <vector>
#include "Constants.h" #include "Constants.h"
#include "Entity.h"
void Manager::draw() void Manager::draw()
{ {
@ -66,6 +68,15 @@ std::vector<Entity*>& Manager::getTeam(Team mTeam)
return entitiesByTeam[mTeam]; return entitiesByTeam[mTeam];
} }
std::vector<Entity*> Manager::getAll()
{
std::vector<Entity*> entity_vec;
for (auto& entity_ptr : entities) {
entity_vec.emplace_back(entity_ptr.get());
}
return entity_vec;
}
Entity& Manager::addEntity() Entity& Manager::addEntity()
{ {
Entity* e = new Entity(*this); Entity* e = new Entity(*this);

View File

@ -60,31 +60,21 @@ void TransformComponent::update()
if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) { if (this->entity->hasGroup((size_t) GroupLabel::PLAYERS)) {
IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange); IntersectionBitSet intersectionsX = CollisionHandler::getIntersectionWithBounds(entity, positionChange);
for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) {
intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0));
}
for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) {
intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0)); intersectionsX |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(positionChange.x, 0), Vector2D(0, 0));
} }
IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange); IntersectionBitSet intersectionsY = CollisionHandler::getIntersectionWithBounds(entity, positionChange);
for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::MAPTILES)) { for (auto& collider : Game::collisionHandler->getColliders({GroupLabel::MAPTILES, GroupLabel::COLLIDERS})) {
intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0));
}
for (auto& collider : Game::collisionHandler->getColliders(GroupLabel::COLLIDERS)) {
intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0)); intersectionsY |= CollisionHandler::getIntersection(entity, collider->entity, Vector2D(0, positionChange.y), Vector2D(0, 0));
} }
if (intersectionsX.test((size_t) direction::LEFT) && positionChange.x < 0) IntersectionBitSet intersections = (intersectionsX & IntersectionBitSet("0011")) | (intersectionsY & IntersectionBitSet("1100"));
if (intersections.test((size_t) direction::LEFT) || intersections.test((size_t) direction::RIGHT))
positionChange.x = 0; positionChange.x = 0;
if (intersectionsX.test((size_t) direction::RIGHT) && positionChange.x > 0) if (intersections.test((size_t) direction::UP) || intersections.test((size_t) direction::DOWN))
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)
positionChange.y = 0; positionChange.y = 0;
} }