public onCollide and onCollided

This commit is contained in:
Bluub
2021-06-01 17:45:09 +02:00
parent 2bd387195f
commit d0205b68e6
4 changed files with 18 additions and 36 deletions
@@ -17,37 +17,26 @@ namespace BBM
}
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, Vector3f bound)
: WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound)
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, float boundSize)
: WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize})
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize})
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided, Vector3f bound)
: WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound)
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided, float boundSize)
: WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize})
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize})
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound)
: WAL::Component(entity), _onCollide(), _onCollided(), bound(bound)
: WAL::Component(entity), onCollide(), onCollided(), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize)
: WAL::Component(entity), _onCollide(), _onCollided(), bound({boundSize, boundSize, boundSize})
: WAL::Component(entity), onCollide(), onCollided(), bound({boundSize, boundSize, boundSize})
{ }
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &CollisionComponent::getOnCollide(void) const
{
return _onCollide;
}
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &CollisionComponent::getOnCollided(void) const
{
return _onCollided;
}
}
@@ -14,11 +14,11 @@ namespace BBM
class CollisionComponent : public WAL::Component
{
private:
//! @brief onCollide functions to be called
WAL::Callback<WAL::Entity &, const WAL::Entity &> _onCollide;
//! @brief onCollided functions to be called
WAL::Callback<WAL::Entity &, const WAL::Entity &> _onCollided;
public:
//! @brief onCollide functions to be called
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide;
//! @brief onCollided functions to be called
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided;
//! @brief Bound size on all axis
Vector3f bound;
//! @inherit
@@ -53,11 +53,5 @@ namespace BBM
//! @brief A component can't be assigned
CollisionComponent &operator=(const CollisionComponent &) = delete;
//! @brief Get reference of the onCollide callback
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &getOnCollide(void) const;
//! @brief Get reference of the onCollided callback
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &getOnCollided(void) const;
};
}
+4 -5
View File
@@ -67,13 +67,12 @@ namespace BBM
{
auto scene = std::make_shared<WAL::Scene>();
RAY3D::Cube cube(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED);
RAY3D::Cube cubePlayer(Vector3f(0, 0, 0), Vector3f(3, 3, 3), GREEN);
scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent<RAY3D::Cube>>(cubePlayer)
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<KeyboardComponent>()
.addComponent<CollisionComponent>(3)
.addComponent<CollisionComponent>(2)
.addComponent<MovableComponent>();
scene->addEntity("cube")
.addComponent<PositionComponent>(-5, 0, -5)
@@ -83,8 +82,8 @@ namespace BBM
.addComponent<CollisionComponent>([](WAL::Entity &, const WAL::Entity &){},
[](WAL::Entity &actual, const WAL::Entity &) {
try {
auto &mov = actual.getComponent<MovableComponent>();
mov.resetVelocity();
auto &mov = actual.getComponent<MovableComponent>();
mov.resetVelocity();
} catch (std::exception &e) { };
}, 3);
+4 -4
View File
@@ -36,16 +36,16 @@ namespace BBM
for (auto &other : _wal.scene->getEntities()) {
if (&other == &entity)
continue;
if (!other.hasComponent(typeid(CollisionComponent)) ||
!other.hasComponent(typeid(PositionComponent)))
if (!other.hasComponent<CollisionComponent>() ||
!other.hasComponent<PositionComponent>())
continue;
auto colB = other.getComponent<CollisionComponent>();
auto posB = other.getComponent<PositionComponent>().position;
Vector3f minB = Vector3f::min(posB, posB + colB.bound);
Vector3f maxB = Vector3f::max(posB, posB + colB.bound);
if (collide(minA, maxA, minB, maxB)) {
col.getOnCollide()(entity, other);
colB.getOnCollided()(entity, other);
col.onCollide(entity, other);
colB.onCollided(entity, other);
}
}
}