From c00668c58b26325203cb6815bc3dedf1e7d7ac5e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 29 Apr 2018 16:45:55 -0400 Subject: Added map object collision Collision checking in PonderingSystem was rewritten to work as follows: horizontal movement is step first, then vertical. In each step, the closest environmental boundary to the body is found on the axis of movement in the space traversed by the body. Then, if any map objects fall in the region between the body's old position and the environmental boundary (or body new position if no boundary was found), process collision with those bodies in increasing distance order, stopping if a collision stops movement short of where the next collision would take place. After this, process collision with all of the environmental boundaries at the axis distance found earlier, as long as movement hasn't stopped short. This is not the most optimal implementation, and there is a lot of code repetition, but it is a start and it works. All map objects currently function as walls. This fixes the bug where you could, with pixel-perfect precision, jump into the corner of a wall tile. The top of the hitbox for the spike tile was lowered by one pixel. This fixes a problem where if the player is halfway on a floor tile and halfway over a spike tile, the floor tile would not stop the spike tile from being processed, and the player would die. --- src/collision.cpp | 97 ----- src/collision.h | 81 ---- src/components/mappable.h | 4 +- src/components/ponderable.h | 16 + src/systems/mapping.cpp | 2 +- src/systems/pondering.cpp | 882 +++++++++++++++++++++++++++++++++----------- src/systems/pondering.h | 23 ++ src/systems/realizing.cpp | 4 + 8 files changed, 714 insertions(+), 395 deletions(-) delete mode 100644 src/collision.cpp delete mode 100644 src/collision.h (limited to 'src') diff --git a/src/collision.cpp b/src/collision.cpp deleted file mode 100644 index b747a90..0000000 --- a/src/collision.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include "collision.h" - -bool Collision::operator<(const Collision& other) const -{ - // Most important is the type of collision - if (type_ != other.type_) - { - return (static_cast(type_) > static_cast(other.type_)); - } - - // Next, categorize the collisions arbitrarily based on direction - if (dir_ != other.dir_) - { - return (static_cast(dir_) < static_cast(other.dir_)); - } - - // We want to process closer collisions first - if (axis_ != other.axis_) - { - switch (dir_) - { - case Direction::left: - case Direction::up: - { - return (axis_ < other.axis_); - } - - case Direction::right: - case Direction::down: - { - return (axis_ > other.axis_); - } - } - } - - // Order the remaining attributes arbitrarily - return std::tie(collider_, lower_, upper_) < - std::tie(other.collider_, other.lower_, other.upper_); -} - -bool Collision::isColliding( - double x, - double y, - int w, - int h) const -{ - int right = x + w; - int bottom = y + h; - - switch (dir_) - { - case Direction::left: - case Direction::right: - { - if (!((bottom > lower_) && (y < upper_))) - { - return false; - } - - break; - } - - case Direction::up: - case Direction::down: - { - if (!((right > lower_) && (x < upper_))) - { - return false; - } - - break; - } - } - - switch (dir_) - { - case Direction::left: - { - return (axis_ >= x); - } - - case Direction::right: - { - return (axis_ <= right); - } - - case Direction::up: - { - return (axis_ >= y); - } - - case Direction::down: - { - return (axis_ <= bottom); - } - } -} diff --git a/src/collision.h b/src/collision.h deleted file mode 100644 index e5371f8..0000000 --- a/src/collision.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef COLLISION_H_53D84877 -#define COLLISION_H_53D84877 - -#include "entity_manager.h" -#include "direction.h" - -class Collision { -public: - - using id_type = EntityManager::id_type; - - // Types are defined in descending priority order - enum class Type { - wall, - platform, - adjacency, - warp, - danger - }; - - Collision( - id_type collider, - Direction dir, - Type type, - int axis, - double lower, - double upper) : - collider_(collider), - dir_(dir), - type_(type), - axis_(axis), - lower_(lower), - upper_(upper) - { - } - - inline id_type getCollider() const - { - return collider_; - } - - inline Direction getDirection() const - { - return dir_; - } - - inline Type getType() const - { - return type_; - } - - inline int getAxis() const - { - return axis_; - } - - inline double getLower() const - { - return lower_; - } - - inline double getUpper() const - { - return upper_; - } - - bool operator<(const Collision& other) const; - - bool isColliding(double x, double y, int w, int h) const; - -private: - - id_type collider_; - Direction dir_; - Type type_; - int axis_; - double lower_; - double upper_; -}; - -#endif /* end of include guard: COLLISION_H_53D84877 */ diff --git a/src/components/mappable.h b/src/components/mappable.h index 6f3d38e..e92074e 100644 --- a/src/components/mappable.h +++ b/src/components/mappable.h @@ -7,7 +7,7 @@ #include #include "component.h" #include "renderer/texture.h" -#include "collision.h" +#include "components/ponderable.h" #include "entity_manager.h" class MappableComponent : public Component { @@ -46,7 +46,7 @@ public: class Boundary { public: - using Type = Collision::Type; + using Type = PonderableComponent::Collision; Boundary( double axis, diff --git a/src/components/ponderable.h b/src/components/ponderable.h index fd7e775..5354f87 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -20,6 +20,17 @@ public: freefalling }; + /** + * List of different types of collidable surfaces. + */ + enum class Collision { + wall, + platform, + adjacency, + warp, + danger + }; + /** * Constructor for initializing the body type, which is a constant. */ @@ -66,6 +77,11 @@ public: */ bool collidable = true; + /** + * The effect that colliding with this body has. + */ + Collision colliderType = Collision::wall; + /** * If this flag is disabled, the entity will be ignored by the pondering * system. diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index af67aed..d78c8fe 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -175,7 +175,7 @@ void MappingSystem::generateBoundaries(id_type mapEntity) addBoundary( mappable.downBoundaries, - y * TILE_HEIGHT, + y * TILE_HEIGHT + 1, x * TILE_WIDTH, (x+1) * TILE_WIDTH, MappableComponent::Boundary::Type::danger); diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 4ae6176..04b45a1 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -11,7 +11,6 @@ #include "systems/playing.h" #include "systems/realizing.h" #include "consts.h" -#include "collision.h" void PonderingSystem::tick(double dt) { @@ -56,304 +55,566 @@ void PonderingSystem::tick(double dt) const double oldRight = oldX + transformable.w; const double oldBottom = oldY + transformable.h; - double newX = oldX + ponderable.velX * dt; - double newY = oldY + ponderable.velY * dt; + CollisionResult result; + result.newX = oldX + ponderable.velX * dt; + result.newY = oldY + ponderable.velY * dt; bool oldGrounded = ponderable.grounded; ponderable.grounded = false; - std::priority_queue collisions; - - // Find collisions - if (newX < oldX) + // Find horizontal collisions. + if (result.newX < oldX) { - for (auto it = mappable.leftBoundaries.lower_bound(oldX); - (it != std::end(mappable.leftBoundaries)) && (it->first >= newX); - it++) + bool boundaryCollision = false; + auto it = mappable.leftBoundaries.lower_bound(oldX); + + // Find the axis distance of the closest environmental boundary. + for (; + (it != std::end(mappable.leftBoundaries)) && + (it->first >= result.newX); + it++) { - if ((oldBottom > it->second.lower) - && (oldY < it->second.upper)) + // Check that the boundary is in range for the other axis. + if ((oldBottom > it->second.lower) && (oldY < it->second.upper)) { // We have a collision! - collisions.emplace( - mapEntity, - Direction::left, - it->second.type, - it->first, - it->second.lower, - it->second.upper); + boundaryCollision = true; + + break; } } - } else if (newX > oldX) - { - for (auto it = mappable.rightBoundaries.lower_bound(oldRight); - (it != std::end(mappable.rightBoundaries)) - && (it->first <= (newX + transformable.w)); - it++) + + // Find a list of potential colliders, sorted so that the closest is + // first. + std::vector colliders; + + for (id_type collider : entities) { - if ((oldBottom > it->second.lower) - && (oldY < it->second.upper)) + // Can't collide with self. + if (collider == entity) { - // We have a collision! - collisions.emplace( - mapEntity, - Direction::right, - it->second.type, - it->first, - it->second.lower, - it->second.upper); + continue; } - } - } - if (newY < oldY) - { - for (auto it = mappable.upBoundaries.lower_bound(oldY); - (it != std::end(mappable.upBoundaries)) && (it->first >= newY); - it++) - { - if ((oldRight > it->second.lower) - && (oldX < it->second.upper)) + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); + + // Only check objects that are active. + if (!colliderPonder.active) { - // We have a collision! - collisions.emplace( - mapEntity, - Direction::up, - it->second.type, - it->first, - it->second.lower, - it->second.upper); + continue; } - } - } else if (newY > oldY) - { - for (auto it = mappable.downBoundaries.lower_bound(oldBottom); - (it != std::end(mappable.downBoundaries)) - && (it->first <= (newY + transformable.h)); - it++) - { - if ((oldRight > it->second.lower) - && (oldX < it->second.upper)) + + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would move into the potential collider, + if ((colliderTrans.x + colliderTrans.w > result.newX) && + // that it wasn't already colliding, + (colliderTrans.x + colliderTrans.w <= oldX) && + // that the position on the other axis is in range, + (colliderTrans.y + colliderTrans.h > oldY) && + (colliderTrans.y < oldBottom) && + // and that the collider is not farther away than the environmental + // boundary. + (!boundaryCollision || + (colliderTrans.x + colliderTrans.w >= it->first))) { - // We have a collision! - collisions.emplace( - mapEntity, - Direction::down, - it->second.type, - it->first, - it->second.lower, - it->second.upper); + colliders.push_back(collider); } } - } - // Process collisions in order of priority - bool adjacentlyWarping = false; - Direction adjWarpDir; - size_t adjWarpMapId; + std::sort( + std::begin(colliders), + std::end(colliders), + [&] (id_type left, id_type right) { + auto& leftTrans = game_.getEntityManager(). + getComponent(left); - while (!collisions.empty()) - { - Collision collision = collisions.top(); - collisions.pop(); - - // Make sure that they are still colliding - if (!collision.isColliding( - newX, - newY, - transformable.w, - transformable.h)) - { - continue; - } + auto& rightTrans = game_.getEntityManager(). + getComponent(right); - bool touchedWall = false; - bool stopProcessing = false; + return (rightTrans.x < leftTrans.x); + }); - switch (collision.getType()) + for (id_type collider : colliders) { - case Collision::Type::wall: + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would still move into the potential collider. + if (colliderTrans.x + colliderTrans.w <= result.newX) { - touchedWall = true; + break; + } + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); + + processCollision( + entity, + collider, + Direction::left, + colliderPonder.colliderType, + colliderTrans.x + colliderTrans.w, + colliderTrans.y, + colliderTrans.y + colliderTrans.h, + result); + + if (result.stopProcessing) + { break; } + } - case Collision::Type::platform: + // If movement hasn't been stopped by an intermediary object, and + // collision checking hasn't been stopped, process the environmental + // boundaries closest to the entity. + if (!result.stopProcessing && !result.touchedWall && boundaryCollision) + { + double boundaryAxis = it->first; + + for (; + (it != std::end(mappable.leftBoundaries)) && + (it->first == boundaryAxis); + it++) { - if (game_.getEntityManager(). - hasComponent(entity)) + if ((oldBottom > it->second.lower) && (oldY < it->second.upper)) { - auto& orientable = game_.getEntityManager(). - getComponent(entity); - - if (orientable.getDropState() != - OrientableComponent::DropState::none) + processCollision( + entity, + mapEntity, + Direction::left, + it->second.type, + it->first, + it->second.lower, + it->second.upper, + result); + + if (result.stopProcessing) { - orientable.setDropState(OrientableComponent::DropState::active); - } else { - touchedWall = true; + break; } - } else { - touchedWall = true; } + } + } + } else if (result.newX > oldX) + { + bool boundaryCollision = false; + auto it = mappable.rightBoundaries.lower_bound(oldRight); + + // Find the axis distance of the closest environmental boundary. + for (; + (it != std::end(mappable.rightBoundaries)) + && (it->first <= (result.newX + transformable.w)); + it++) + { + // Check that the boundary is in range for the other axis. + if ((oldBottom > it->second.lower) && (oldY < it->second.upper)) + { + // We have a collision! + boundaryCollision = true; break; } + } + + // Find a list of potential colliders, sorted so that the closest is + // first. + std::vector colliders; - case Collision::Type::adjacency: + for (id_type collider : entities) + { + // Can't collide with self. + if (collider == entity) { - auto& mappable = game_.getEntityManager(). - getComponent(collision.getCollider()); + continue; + } - auto& adj = [&] () -> const MappableComponent::Adjacent& { - switch (collision.getDirection()) - { - case Direction::left: return mappable.leftAdjacent; - case Direction::right: return mappable.rightAdjacent; - case Direction::up: return mappable.upAdjacent; - case Direction::down: return mappable.downAdjacent; - } - }(); + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); - switch (adj.type) - { - case MappableComponent::Adjacent::Type::wall: - { - touchedWall = true; + // Only check objects that are active. + if (!colliderPonder.active) + { + continue; + } - break; - } + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would move into the potential collider, + if ((colliderTrans.x < result.newX + transformable.w) && + // that it wasn't already colliding, + (colliderTrans.x >= oldRight) && + // that the position on the other axis is in range, + (colliderTrans.y + colliderTrans.h > oldY) && + (colliderTrans.y < oldBottom) && + // and that the collider is not farther away than the environmental + // boundary. + (!boundaryCollision || (colliderTrans.x <= it->first))) + { + colliders.push_back(collider); + } + } - case MappableComponent::Adjacent::Type::wrap: - { - switch (collision.getDirection()) - { - case Direction::left: - { - newX = GAME_WIDTH + WALL_GAP - transformable.w; + std::sort( + std::begin(colliders), + std::end(colliders), + [&] (id_type left, id_type right) { + auto& leftTrans = game_.getEntityManager(). + getComponent(left); - break; - } + auto& rightTrans = game_.getEntityManager(). + getComponent(right); - case Direction::right: - { - newX = -WALL_GAP; + return (leftTrans.x < rightTrans.x); + }); - break; - } + for (id_type collider : colliders) + { + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); - case Direction::up: - { - newY = MAP_HEIGHT * TILE_HEIGHT + WALL_GAP - transformable.h; + // Check if the entity would still move into the potential collider. + if (colliderTrans.x >= result.newX + transformable.w) + { + break; + } - break; - } + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); - case Direction::down: - { - newY = -WALL_GAP; + processCollision( + entity, + collider, + Direction::right, + colliderPonder.colliderType, + colliderTrans.x, + colliderTrans.y, + colliderTrans.y + colliderTrans.h, + result); + + if (result.stopProcessing) + { + break; + } + } - break; - } - } - } + // If movement hasn't been stopped by an intermediary object, and + // collision checking hasn't been stopped, process the environmental + // boundaries closest to the entity. + if (!result.stopProcessing && !result.touchedWall && boundaryCollision) + { + double boundaryAxis = it->first; - case MappableComponent::Adjacent::Type::warp: + for (; + (it != std::end(mappable.rightBoundaries)) && + (it->first == boundaryAxis); + it++) + { + if ((oldBottom > it->second.lower) && (oldY < it->second.upper)) + { + processCollision( + entity, + mapEntity, + Direction::right, + it->second.type, + it->first, + it->second.lower, + it->second.upper, + result); + + if (result.stopProcessing) { - if (game_.getEntityManager(). - hasComponent(entity)) - { - adjacentlyWarping = true; - adjWarpDir = collision.getDirection(); - adjWarpMapId = adj.mapId; - } - break; } + } + } + } + } - case MappableComponent::Adjacent::Type::reverse: - { - // TODO: not yet implemented. + // Find vertical collisions + result.touchedWall = false; - break; - } - } + if ((!result.stopProcessing) && (result.newY < oldY)) + { + bool boundaryCollision = false; + auto it = mappable.upBoundaries.lower_bound(oldY); + + // Find the axis distance of the closest environmental boundary. + for (; + (it != std::end(mappable.upBoundaries)) && + (it->first >= result.newY); + it++) + { + // Check that the boundary is in range for the other axis. + if ((result.newX + transformable.h > it->second.lower) && + (result.newX < it->second.upper)) + { + // We have a collision! + boundaryCollision = true; break; } + } - case Collision::Type::danger: + // Find a list of potential colliders, sorted so that the closest is + // first. + std::vector colliders; + + for (id_type collider : entities) + { + // Can't collide with self. + if (collider == entity) { - if (game_.getEntityManager(). - hasComponent(entity)) - { - game_.getSystemManager().getSystem().die(entity); + continue; + } - adjacentlyWarping = false; - } + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); - stopProcessing = true; + // Only check objects that are active. + if (!colliderPonder.active) + { + continue; + } - break; + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would move into the potential collider, + if ((colliderTrans.y + colliderTrans.h > result.newY) && + // that it wasn't already colliding, + (colliderTrans.y + colliderTrans.h <= oldY) && + // that the position on the other axis is in range, + (colliderTrans.x + colliderTrans.w > result.newX) && + (colliderTrans.x < result.newX + transformable.w) && + // and that the collider is not farther away than the environmental + // boundary. + (!boundaryCollision || + (colliderTrans.y + colliderTrans.h >= it->first))) + { + colliders.push_back(collider); } + } + + std::sort( + std::begin(colliders), + std::end(colliders), + [&] (id_type left, id_type right) { + auto& leftTrans = game_.getEntityManager(). + getComponent(left); + + auto& rightTrans = game_.getEntityManager(). + getComponent(right); - default: + return (rightTrans.y < leftTrans.y); + }); + + for (id_type collider : colliders) + { + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would still move into the potential collider. + if (colliderTrans.y + colliderTrans.h <= result.newY) { - // Not yet implemented. + break; + } + + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); + processCollision( + entity, + collider, + Direction::up, + colliderPonder.colliderType, + colliderTrans.y + colliderTrans.h, + colliderTrans.x, + colliderTrans.x + colliderTrans.w, + result); + + if (result.stopProcessing) + { break; } } - if (stopProcessing) + // If movement hasn't been stopped by an intermediary object, and + // collision checking hasn't been stopped, process the environmental + // boundaries closest to the entity. + if (!result.stopProcessing && !result.touchedWall && boundaryCollision) { - break; + double boundaryAxis = it->first; + + for (; + (it != std::end(mappable.upBoundaries)) && + (it->first == boundaryAxis); + it++) + { + if ((result.newX + transformable.w > it->second.lower) && + (result.newX < it->second.upper)) + { + processCollision( + entity, + mapEntity, + Direction::up, + it->second.type, + it->first, + it->second.lower, + it->second.upper, + result); + + if (result.stopProcessing) + { + break; + } + } + } } + } else if ((!result.stopProcessing) && (result.newY > oldY)) + { + bool boundaryCollision = false; + auto it = mappable.downBoundaries.lower_bound(oldBottom); + + // Find the axis distance of the closest environmental boundary. + for (; + (it != std::end(mappable.downBoundaries)) + && (it->first <= (result.newY + transformable.h)); + it++) + { + // Check that the boundary is in range for the other axis. + if ((result.newX + transformable.w > it->second.lower) && + (result.newX < it->second.upper)) + { + // We have a collision! + boundaryCollision = true; - if (touchedWall) + break; + } + } + + // Find a list of potential colliders, sorted so that the closest is + // first. + std::vector colliders; + + for (id_type collider : entities) { - switch (collision.getDirection()) + // Can't collide with self. + if (collider == entity) { - case Direction::left: - { - newX = collision.getAxis(); - ponderable.velX = 0.0; + continue; + } - break; - } + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); - case Direction::right: - { - newX = collision.getAxis() - transformable.w; - ponderable.velX = 0.0; + // Only check objects that are active. + if (!colliderPonder.active) + { + continue; + } - break; - } + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would move into the potential collider, + if ((colliderTrans.y < result.newY + transformable.h) && + // that it wasn't already colliding, + (colliderTrans.y >= oldBottom) && + // that the position on the other axis is in range, + (colliderTrans.x + colliderTrans.w > result.newX) && + (colliderTrans.x < result.newX + transformable.w) && + // and that the collider is not farther away than the environmental + // boundary. + (!boundaryCollision || (colliderTrans.y <= it->first))) + { + colliders.push_back(collider); + } + } - case Direction::up: - { - newY = collision.getAxis(); - ponderable.velY = 0.0; + std::sort( + std::begin(colliders), + std::end(colliders), + [&] (id_type left, id_type right) { + auto& leftTrans = game_.getEntityManager(). + getComponent(left); - break; - } + auto& rightTrans = game_.getEntityManager(). + getComponent(right); - case Direction::down: - { - newY = collision.getAxis() - transformable.h; - ponderable.velY = 0.0; - ponderable.grounded = true; + return (leftTrans.y < rightTrans.y); + }); + + for (id_type collider : colliders) + { + auto& colliderTrans = game_.getEntityManager(). + getComponent(collider); + + // Check if the entity would still move into the potential collider. + if (colliderTrans.y >= result.newY + transformable.h) + { + break; + } + + auto& colliderPonder = game_.getEntityManager(). + getComponent(collider); + + processCollision( + entity, + collider, + Direction::down, + colliderPonder.colliderType, + colliderTrans.y, + colliderTrans.x, + colliderTrans.x + colliderTrans.w, + result); + + if (result.stopProcessing) + { + break; + } + } + + // If movement hasn't been stopped by an intermediary object, and + // collision checking hasn't been stopped, process the environmental + // boundaries closest to the entity. + if (!result.stopProcessing && !result.touchedWall && boundaryCollision) + { + double boundaryAxis = it->first; - break; + for (; + (it != std::end(mappable.downBoundaries)) && + (it->first == boundaryAxis); + it++) + { + if ((result.newX + transformable.w > it->second.lower) && + (result.newX < it->second.upper)) + { + processCollision( + entity, + mapEntity, + Direction::down, + it->second.type, + it->first, + it->second.lower, + it->second.upper, + result); + + if (result.stopProcessing) + { + break; + } } } } } // Move - transformable.x = newX; - transformable.y = newY; + transformable.x = result.newX; + transformable.y = result.newY; // Perform cleanup for orientable entites if (game_.getEntityManager().hasComponent(entity)) @@ -381,12 +642,12 @@ void PonderingSystem::tick(double dt) } // Move to an adjacent map, if necessary - if (adjacentlyWarping) + if (result.adjacentlyWarping) { - double warpX = newX; - double warpY = newY; + double warpX = result.newX; + double warpY = result.newY; - switch (adjWarpDir) + switch (result.adjWarpDir) { case Direction::left: { @@ -420,7 +681,7 @@ void PonderingSystem::tick(double dt) game_.getSystemManager().getSystem(). changeMap( entity, - adjWarpMapId, + result.adjWarpMapId, warpX, warpY); } @@ -453,3 +714,196 @@ void PonderingSystem::initPrototype(id_type prototype) ponderable.frozen = false; ponderable.collidable = true; } + +void PonderingSystem::processCollision( + id_type entity, + id_type collider, + Direction dir, + PonderableComponent::Collision type, + double axis, + double lower, + double upper, + CollisionResult& result) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& transformable = game_.getEntityManager(). + getComponent(entity); + + switch (type) + { + case PonderableComponent::Collision::wall: + { + result.touchedWall = true; + + break; + } + + case PonderableComponent::Collision::platform: + { + if (game_.getEntityManager(). + hasComponent(entity)) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.getDropState() != + OrientableComponent::DropState::none) + { + orientable.setDropState(OrientableComponent::DropState::active); + } else { + result.touchedWall = true; + } + } else { + result.touchedWall = true; + } + + break; + } + + case PonderableComponent::Collision::adjacency: + { + auto& mappable = game_.getEntityManager(). + getComponent(collider); + + auto& adj = [&] () -> const MappableComponent::Adjacent& { + switch (dir) + { + case Direction::left: return mappable.leftAdjacent; + case Direction::right: return mappable.rightAdjacent; + case Direction::up: return mappable.upAdjacent; + case Direction::down: return mappable.downAdjacent; + } + }(); + + switch (adj.type) + { + case MappableComponent::Adjacent::Type::wall: + { + result.touchedWall = true; + + break; + } + + case MappableComponent::Adjacent::Type::wrap: + { + switch (dir) + { + case Direction::left: + { + result.newX = GAME_WIDTH + WALL_GAP - transformable.w; + + break; + } + + case Direction::right: + { + result.newX = -WALL_GAP; + + break; + } + + case Direction::up: + { + result.newY = + MAP_HEIGHT * TILE_HEIGHT + WALL_GAP - transformable.h; + + break; + } + + case Direction::down: + { + result.newY = -WALL_GAP; + + break; + } + } + } + + case MappableComponent::Adjacent::Type::warp: + { + if (game_.getEntityManager(). + hasComponent(entity)) + { + result.adjacentlyWarping = true; + result.adjWarpDir = dir; + result.adjWarpMapId = adj.mapId; + } + + break; + } + + case MappableComponent::Adjacent::Type::reverse: + { + // TODO: not yet implemented. + + break; + } + } + + break; + } + + case PonderableComponent::Collision::danger: + { + if (game_.getEntityManager(). + hasComponent(entity)) + { + game_.getSystemManager().getSystem().die(entity); + + result.adjacentlyWarping = false; + } + + result.stopProcessing = true; + + break; + } + + default: + { + // Not yet implemented. + + break; + } + } + + if (!result.stopProcessing && result.touchedWall) + { + switch (dir) + { + case Direction::left: + { + result.newX = axis; + ponderable.velX = 0.0; + + break; + } + + case Direction::right: + { + result.newX = axis - transformable.w; + ponderable.velX = 0.0; + + break; + } + + case Direction::up: + { + result.newY = axis; + ponderable.velY = 0.0; + + break; + } + + case Direction::down: + { + result.newY = axis - transformable.h; + ponderable.velY = 0.0; + ponderable.grounded = true; + + break; + } + } + } +} diff --git a/src/systems/pondering.h b/src/systems/pondering.h index 58e6496..aa430db 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -18,6 +18,29 @@ public: void initPrototype(id_type prototype); +private: + + struct CollisionResult + { + double newX; + double newY; + bool stopProcessing = false; + bool touchedWall = false; + bool adjacentlyWarping = false; + Direction adjWarpDir; + size_t adjWarpMapId; + }; + + void processCollision( + id_type entity, + id_type collider, + Direction dir, + PonderableComponent::Collision type, + double axis, + double lower, + double upper, + CollisionResult& result); + }; #endif /* end of include guard: PONDERING_H_F2530E0E */ diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index c86dd5e..3656acb 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp @@ -207,6 +207,10 @@ EntityManager::id_type RealizingSystem::initSingleton( animatable.origAnimation = "static"; + // Create a physics body. + game_.getSystemManager().getSystem(). + initializeBody(mapObject, PonderableComponent::Type::vacuumed); + mappable.objects.push_back(mapObject); } else if (!xmlStrcmp( mapNode->name, -- cgit 1.4.1