From 69c04dfb6c49e7b2d34a6699c071f037880fbde5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 4 May 2018 00:08:35 -0400 Subject: Added ferrying (buggy) A freefalling body is considered to be "ferried" if it is grounded by another body (not a map). Its location is then dependent on the ferry's location; in this way, the ferry carries the passenger around. This implementation is kind of buggy currently: first of all, ferrying does not work vertically upward, because the ferry will consider the passenger to be a wall and will not continue moving upward. Second, ferries are not processed before passengers, so passengers can move in a physics tick using the knowledge of a ferry's location before it moves in that tick. Third, if the transform coordinates are set by any system other than the PonderingSystem, the relative coordinates to the ferry will not be updated and the body will not be unferried if necessary. This is still a cool commit because, three years later, we have finally overcome the issue that stopped development on the original branch in 2015. --- src/components/ponderable.h | 33 ++++++++++++++++++++++++++ src/systems/pondering.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++-- src/systems/pondering.h | 1 + 3 files changed, 89 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/components/ponderable.h b/src/components/ponderable.h index 5354f87..45150a0 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -1,11 +1,15 @@ #ifndef TANGIBLE_H_746DB3EE #define TANGIBLE_H_746DB3EE +#include #include "component.h" +#include "entity_manager.h" class PonderableComponent : public Component { public: + using id_type = EntityManager::id_type; + /** * List of different types of physical bodies. * @@ -66,6 +70,35 @@ public: */ bool grounded = false; + /** + * Whether or not a freefalling body is being ferried by another body. + * + * @managed_by PonderingSystem + */ + bool ferried = false; + + /** + * The entity that is ferrying this body, if there is one. + * + * @managed_by PonderingSystem + */ + id_type ferry; + + /** + * The location of the body relative to the location of its ferry. + * + * @managed_by PonderingSystem + */ + double relX; + double relY; + + /** + * The bodies that are being ferried by this body. + * + * @managed_by PonderingSystem + */ + std::set passengers; + /** * If enabled, this will prevent the body from moving. */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 5143f8e..ccfd66f 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -57,8 +57,21 @@ void PonderingSystem::tick(double dt) const double oldBottom = oldY + transformable.h; CollisionResult result; - result.newX = oldX + ponderable.velX * dt; - result.newY = oldY + ponderable.velY * dt; + + if (ponderable.ferried) + { + auto& ferryTrans = game_.getEntityManager(). + getComponent(ponderable.ferry); + + result.newX = ferryTrans.x + ponderable.relX; + result.newY = ferryTrans.y + ponderable.relY; + } else { + result.newX = transformable.x; + result.newY = transformable.y; + } + + result.newX += ponderable.velX * dt; + result.newY += ponderable.velY * dt; bool oldGrounded = ponderable.grounded; ponderable.grounded = false; @@ -642,6 +655,43 @@ void PonderingSystem::tick(double dt) } } + // Handle ferry passengers + if ((ponderable.type == PonderableComponent::Type::freefalling) && + (ponderable.grounded != oldGrounded)) + { + if (ponderable.grounded && + game_.getEntityManager(). + hasComponent(result.groundEntity)) + { + // The body is now being ferried + auto& ferryPonder = game_.getEntityManager(). + getComponent(result.groundEntity); + + ponderable.ferried = true; + ponderable.ferry = result.groundEntity; + + ferryPonder.passengers.insert(entity); + } else if (ponderable.ferried) + { + // The body is no longer being ferried + ponderable.ferried = false; + + auto& ferryPonder = game_.getEntityManager(). + getComponent(ponderable.ferry); + + ferryPonder.passengers.erase(entity); + } + } + + if (ponderable.ferried) + { + auto& ferryTrans = game_.getEntityManager(). + getComponent(ponderable.ferry); + + ponderable.relX = transformable.x - ferryTrans.x; + ponderable.relY = transformable.y - ferryTrans.y; + } + // Move to an adjacent map, if necessary if (result.adjacentlyWarping) { @@ -714,6 +764,8 @@ void PonderingSystem::initPrototype(id_type prototype) ponderable.grounded = false; ponderable.frozen = false; ponderable.collidable = true; + ponderable.ferried = false; + ponderable.passengers.clear(); } void PonderingSystem::processCollision( @@ -900,6 +952,7 @@ void PonderingSystem::processCollision( case Direction::down: { result.newY = axis - transformable.h; + result.groundEntity = collider; ponderable.velY = 0.0; ponderable.grounded = true; diff --git a/src/systems/pondering.h b/src/systems/pondering.h index aa430db..b195814 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -29,6 +29,7 @@ private: bool adjacentlyWarping = false; Direction adjWarpDir; size_t adjWarpMapId; + id_type groundEntity; }; void processCollision( -- cgit 1.4.1