From cffe1bdc316c5b13e4615b0843a94012324fdce3 Mon Sep 17 00:00:00 2001 From: Nimac0 Date: Mon, 29 Apr 2024 23:46:25 +0200 Subject: [PATCH] ref(TransformComponent): extracted entity move collision function --- include/TransformComponent.h | 1 + src/TransformComponent.cpp | 38 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/TransformComponent.h b/include/TransformComponent.h index e57d747..c88a81d 100644 --- a/include/TransformComponent.h +++ b/include/TransformComponent.h @@ -24,5 +24,6 @@ public: void init() override; void update() override; + void setPositionAfterCollision(Vector2D& positionChange); void modifySpeed(int8_t modifier); }; diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index c97b4fd..3175968 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -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(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(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; @@ -84,4 +70,24 @@ void TransformComponent::update() 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(entity, Vector2D(positionChange.x, 0), colliders)) & + IntersectionBitSet("0011")) | + (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(0, positionChange.y)) | + (this->entity->getManager() + .getGame()->collisionHandler->getAnyIntersection(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; } \ No newline at end of file