mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 10:13:42 +00:00
ref(TransformComponent): extracted entity move collision function
This commit is contained in:
parent
6744f519c5
commit
cffe1bdc31
@ -24,5 +24,6 @@ public:
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void setPositionAfterCollision(Vector2D& positionChange);
|
||||
void modifySpeed(int8_t modifier);
|
||||
};
|
||||
|
||||
@ -60,22 +60,8 @@ void TransformComponent::update()
|
||||
direction.y * speed * multiplier
|
||||
);
|
||||
|
||||
// TODO: move to separate functions
|
||||
|
||||
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)) {
|
||||
IntersectionBitSet intersections =
|
||||
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
|
||||
(this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(positionChange.x, 0), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) &
|
||||
IntersectionBitSet("0011")) |
|
||||
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) |
|
||||
(this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(0, positionChange.y), { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS })) &
|
||||
IntersectionBitSet("1100"));
|
||||
|
||||
if (intersections.test((size_t)Direction::LEFT) || intersections.test((size_t)Direction::RIGHT))
|
||||
positionChange.x = 0;
|
||||
|
||||
if (intersections.test((size_t)Direction::UP) || intersections.test((size_t)Direction::DOWN))
|
||||
positionChange.y = 0;
|
||||
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
|
||||
this->setPositionAfterCollision(positionChange);
|
||||
}
|
||||
|
||||
position += positionChange;
|
||||
@ -85,3 +71,23 @@ void TransformComponent::modifySpeed(int8_t modifier)
|
||||
{
|
||||
this->speed += modifier;
|
||||
}
|
||||
|
||||
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)
|
||||
{
|
||||
std::initializer_list colliders = { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS };
|
||||
IntersectionBitSet intersections =
|
||||
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
|
||||
(this->entity->getManager()
|
||||
.getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(positionChange.x, 0), colliders)) &
|
||||
IntersectionBitSet("0011")) |
|
||||
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) |
|
||||
(this->entity->getManager()
|
||||
.getGame()->collisionHandler->getAnyIntersection<IntersectionBitSet>(entity, Vector2D(0, positionChange.y), colliders)) &
|
||||
IntersectionBitSet("1100"));
|
||||
|
||||
if (intersections.test((size_t)Direction::LEFT) || intersections.test((size_t)Direction::RIGHT))
|
||||
positionChange.x = 0;
|
||||
|
||||
if (intersections.test((size_t)Direction::UP) || intersections.test((size_t)Direction::DOWN))
|
||||
positionChange.y = 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user