From 8142a9c87a13cecc7a3698e877f24d89f128c074 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 21 Apr 2018 14:50:52 -0400 Subject: Started working on prototype objects --- src/systems/pondering.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 5 deletions(-) (limited to 'src/systems/pondering.cpp') diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 02d5cfc..9115988 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -39,7 +39,7 @@ void PonderingSystem::tick(double dt) ponderable.setVelocityY( ponderable.getVelocityY() + ponderable.getAccelY() * dt); - if ((ponderable.getType() == PonderableComponent::Type::freefalling) + if ((ponderable.getBodyType() == PonderableComponent::BodyType::freefalling) && (ponderable.getVelocityY() > TERMINAL_VELOCITY)) { ponderable.setVelocityY(TERMINAL_VELOCITY); @@ -58,7 +58,7 @@ void PonderingSystem::tick(double dt) std::priority_queue collisions; - // Find collisions + // Find map collisions for (id_type mapEntity : maps) { auto& mappable = game_.getEntityManager(). @@ -366,6 +366,51 @@ void PonderingSystem::tick(double dt) } } + // Find body collisions + for (id_type body : entities) + { + // Can't collide with self + if (body == entity) + { + continue; + } + + // Make sure the body is collidable + auto& colliderPonderable = + game_.getEntityManager().getComponent(body); + + if (!colliderPonderable.isCollidable()) + { + continue; + } + + // Test if the body was already colliding + auto& colliderTransformable = + game_.getEntityManager().getComponent(body); + + if ((oldRight > colliderTransformable.getX()) + && (oldX < colliderTransformable.getX() + colliderTransformable.getW()) + && (oldBottom > colliderTransformable.getY()) + && (oldY < colliderTransformable.getY() + colliderTransformable.getH())) + { + continue; + } + + // Test if there is a new collision + if (!((newX + transformable.getW() > colliderTransformable.getX()) + && (newX < colliderTransformable.getX() + colliderTransformable.getW()) + && (newY + transformable.getH() > colliderTransformable.getY()) + && (newY < + colliderTransformable.getY() + colliderTransformable.getH()))) + { + continue; + } + + // Process the collision + processBodyCollision(entity, body); + processBodyCollision(body, entity); + } + // Move transformable.setX(newX); transformable.setY(newY); @@ -399,13 +444,46 @@ void PonderingSystem::tick(double dt) void PonderingSystem::initializeBody( id_type entity, - PonderableComponent::Type type) + PonderableComponent::BodyType bodyType, + PonderableComponent::ColliderType colliderType) { auto& ponderable = game_.getEntityManager(). - emplaceComponent(entity, type); + emplaceComponent(entity, bodyType, colliderType); - if (type == PonderableComponent::Type::freefalling) + if (bodyType == PonderableComponent::BodyType::freefalling) { ponderable.setAccelY(NORMAL_GRAVITY); } } + +void PonderingSystem::processBodyCollision(id_type body, id_type collider) +{ + auto& bodyPonderable = game_.getEntityManager(). + getComponent(body); + + auto& colliderPonderable = game_.getEntityManager(). + getComponent(collider); + + switch (colliderPonderable.getColliderType()) + { + case PonderableComponent::ColliderType::event: + { + auto& callback = colliderPonderable. + getEventCallback(bodyPonderable.getColliderType()); + + if (callback) + { + callback(game_); + } + + break; + } + + default: + { + // Do nothing. + + break; + } + } +} -- cgit 1.4.1