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

extended collision handler docs

This commit is contained in:
Benedikt Galbavy 2024-02-02 15:12:51 +01:00
parent 680f66270f
commit cebe343e4c
6 changed files with 42 additions and 7 deletions

View File

@ -26,6 +26,8 @@ DOT_TRANSPARENT = YES
# Source files
INPUT = ./include ./src
FILE_PATTERNS = *.cpp *.h *.hpp
EXAMPLE_PATH = ./include ./src
EXAMPLE_PATTERNS = *.cpp *.h *.hpp
# Output formats
GENERATE_XML = YES
@ -57,4 +59,5 @@ DISABLE_INDEX = YES
GENERATE_TREEVIEW = YES
HTML_COLLABORATION = YES
# idk everything else
# idk everything else
TAB_SIZE = 4

View File

@ -1,5 +1,9 @@
#pragma once
//! \file CollisionHandler.h
//! \file CollisionHandler.cpp
//! \file TransformComponent.cpp
#include "ColliderComponent.h"
#include "Constants.h"
#include "Entity.h"
@ -17,9 +21,11 @@
class ColliderComponent;
class Entity;
// [IntersectionBitSet]
constexpr uint8_t DIRECTION_C = 4;
using IntersectionBitSet = std::bitset<DIRECTION_C>;
// [IntersectionBitSet]
class CollisionHandler
{
@ -54,20 +60,18 @@ public:
* 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 posMod Modifier to apply toposition before checking collisions.
* \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()
* \details Example usage for IntersectionBitSet (TransformComponent::update()):
* \snippet{trimleft} TransformComponent.cpp getAnyIntersection example code
*
*/
template<typename T>
T getAnyIntersection(

View File

@ -16,6 +16,7 @@ using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
using GroupBitSet = std::bitset<MAX_GROUPS>;
using ComponentArray = std::array<Component*, MAX_COMPONENTS>;
/*! TODO */
enum class GroupLabel
{
MAPTILES,
@ -57,6 +58,7 @@ public:
std::bitset<MAX_GROUPS> getGroupBitSet();
void setTeam(TeamLabel teamLabel);
/*! TODO */
TeamLabel getTeam();
Manager& getManager() { return manager; };

View File

@ -23,6 +23,7 @@ public:
TransformComponent(float x, float y, int w, int h, int scale);
void init() override;
/*! TODO: document usage of collision handler */
void update() override;
void modifySpeed(int8_t modifier);
};

View File

@ -115,6 +115,15 @@ std::vector<ColliderComponent*> CollisionHandler::getColliders(
return colliders;
}
/*!
*
* \details Refer to getAnyIntersection() for more details
* \return A bitset of intersections, describing the directions of intersection. Position `Direction` in bitset true if edge in that direction collides
* \see Direction
* \see IntersectionBitSet
* \snippet CollisionHandler.h IntersectionBitSet
*
*/
template<>
IntersectionBitSet CollisionHandler::getAnyIntersection<IntersectionBitSet>(
Entity* entity,
@ -131,6 +140,13 @@ IntersectionBitSet CollisionHandler::getAnyIntersection<IntersectionBitSet>(
return intersections;
};
/*!
*
* \details Refer to getAnyIntersection() for more details
* \return The first entity with collision found
* \see Entity
*
*/
template<>
Entity* CollisionHandler::getAnyIntersection<Entity*>(
Entity* entity,
@ -149,6 +165,12 @@ Entity* CollisionHandler::getAnyIntersection<Entity*>(
return nullptr;
};
/*!
*
* \details Refer to getAnyIntersection() for more details
* \return True if any collision was found, otherwise false
*
*/
template<>
bool CollisionHandler::getAnyIntersection<bool>(
Entity* entity,

View File

@ -63,6 +63,8 @@ void TransformComponent::update()
// TODO: move to separate functions
if (this->entity->hasGroup((size_t)GroupLabel::PLAYERS)) {
// [getAnyIntersection example code]
IntersectionBitSet intersections =
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
(Game::collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(positionChange.x, 0), { GroupLabel::MAPTILES, GroupLabel::COLLIDERS })) &
@ -76,6 +78,7 @@ void TransformComponent::update()
if (intersections.test((size_t)Direction::UP) || intersections.test((size_t)Direction::DOWN))
positionChange.y = 0;
// [getAnyIntersection example code]
}
position += positionChange;