mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-04 02:36:31 +00:00
collision system is now updating only movable entities
This commit is contained in:
@@ -173,6 +173,11 @@ namespace BBM
|
||||
return Vector3<T>(std::round(this->x), std::round(this->y), std::round(this->z));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool isNull() const
|
||||
{
|
||||
return this->x == 0 && this->y == 0 && this->z == 0;
|
||||
}
|
||||
|
||||
operator RAY::Vector3() const requires(std::is_same_v<T, float>)
|
||||
{
|
||||
return RAY::Vector3(this->x, this->y, this->z);
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace BBM
|
||||
return (overlapX && overlapY && overlapZ);
|
||||
}
|
||||
|
||||
void CollisionSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, CollisionComponent> &entity)
|
||||
void CollisionSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, CollisionComponent, MovableComponent> &entity)
|
||||
{
|
||||
unsigned int entityUid = entity->getUid();
|
||||
auto &posA = entity.get<PositionComponent>();
|
||||
@@ -33,8 +33,10 @@ namespace BBM
|
||||
Vector3f pointAy = pointA;
|
||||
Vector3f pointAz = pointA;
|
||||
|
||||
if (auto *movable = entity->tryGetComponent<MovableComponent>()) {
|
||||
auto vel = movable->getVelocity();
|
||||
auto &movable = entity.get<MovableComponent>();
|
||||
const auto &vel = movable.getVelocity();
|
||||
|
||||
if (!vel.isNull()) {
|
||||
pointAx.x += vel.x;
|
||||
pointAy.y += vel.y;
|
||||
pointAz.z += vel.z;
|
||||
@@ -43,13 +45,21 @@ namespace BBM
|
||||
Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound);
|
||||
Vector3f maxAx = Vector3f::max(pointAx, pointAx + colA.bound);
|
||||
|
||||
Vector3f minAy = Vector3f::min(pointAy, pointAy + colA.bound);
|
||||
Vector3f maxAy = Vector3f::max(pointAy, pointAy + colA.bound);
|
||||
Vector3f minAy;
|
||||
Vector3f maxAy;
|
||||
|
||||
Vector3f minAz = Vector3f::min(pointAz, pointAz + colA.bound);
|
||||
Vector3f maxAz = Vector3f::max(pointAz, pointAz + colA.bound);
|
||||
Vector3f minAz;
|
||||
Vector3f maxAz;
|
||||
|
||||
for (auto &[other, posB, colB] : this->getView()) {
|
||||
if (!vel.isNull()) {
|
||||
minAy = Vector3f::min(pointAy, pointAy + colA.bound);
|
||||
maxAy = Vector3f::max(pointAy, pointAy + colA.bound);
|
||||
|
||||
minAz = Vector3f::min(pointAz, pointAz + colA.bound);
|
||||
maxAz = Vector3f::max(pointAz, pointAz + colA.bound);
|
||||
}
|
||||
|
||||
for (auto &[other, posB, colB] : this->_wal.getScene()->view<PositionComponent, CollisionComponent>()) {
|
||||
if (other.getUid() == entityUid)
|
||||
continue;
|
||||
|
||||
@@ -60,13 +70,15 @@ namespace BBM
|
||||
Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound);
|
||||
|
||||
if (boxesCollide(minAx, maxAx, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::X;
|
||||
collidedAxis += vel.isNull() ? 7 : CollisionComponent::CollidedAxis::X;
|
||||
}
|
||||
if (boxesCollide(minAy, maxAy, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Y;
|
||||
}
|
||||
if (boxesCollide(minAz, maxAz, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Z;
|
||||
if (!vel.isNull()) {
|
||||
if (boxesCollide(minAy, maxAy, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Y;
|
||||
}
|
||||
if (boxesCollide(minAz, maxAz, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Z;
|
||||
}
|
||||
}
|
||||
if (collidedAxis) {
|
||||
colA.onCollide(entity, other, static_cast<CollisionComponent::CollidedAxis>(collidedAxis));
|
||||
|
||||
@@ -10,16 +10,17 @@
|
||||
#include "System/System.hpp"
|
||||
#include "Models/Vector3.hpp"
|
||||
#include "Component/Collision/CollisionComponent.hpp"
|
||||
#include "Component/Movable/MovableComponent.hpp"
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
//! @brief A system to handle collisions.
|
||||
class CollisionSystem : public WAL::System<PositionComponent, CollisionComponent>
|
||||
class CollisionSystem : public WAL::System<PositionComponent, CollisionComponent, MovableComponent>
|
||||
{
|
||||
public:
|
||||
//! @inherit
|
||||
void onFixedUpdate(WAL::ViewEntity<PositionComponent, CollisionComponent> &entity) override;
|
||||
void onFixedUpdate(WAL::ViewEntity<PositionComponent, CollisionComponent, MovableComponent> &entity) override;
|
||||
|
||||
//! @brief A default constructor
|
||||
explicit CollisionSystem(WAL::Wal &wal);
|
||||
|
||||
Reference in New Issue
Block a user