From e16fb5be90c889c371cbb0ca2444735c2e12073c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 18 Feb 2018 12:35:45 -0500 Subject: Implemented map adjacency This brings along with it the ability to move to different maps, for which the PlayingSystem and PlayableComponent were introduced. The PlayingSystem is a general overseer system that handles big picture stuff like initializing the player and changing maps. The PlayableComponent represents the player. While the ControllableComponent is also likely to always only be on the player entity, the two are distinct by separation of concerns. This also required a refactoring of how collisions are processed, because of a bug where the player can move to a new map when horizontal collisions are checked, and vertical collisions are skipped, causing the player to clip through the ground because the normal force was never handled. --- src/collision.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/collision.cpp (limited to 'src/collision.cpp') diff --git a/src/collision.cpp b/src/collision.cpp new file mode 100644 index 0000000..b747a90 --- /dev/null +++ b/src/collision.cpp @@ -0,0 +1,97 @@ +#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); + } + } +} -- cgit 1.4.1