From 478bc11eec70e6127161ff360cd77d6893a81c42 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 6 Feb 2021 11:40:48 -0500 Subject: Moved some collision stuff into the TransformSystem --- src/character_system.cpp | 57 ++++++---------------------------------- src/transform_system.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ src/transform_system.h | 13 ++++++++++ 3 files changed, 88 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/character_system.cpp b/src/character_system.cpp index 4d2d338..3df4f9a 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp @@ -67,58 +67,15 @@ void CharacterSystem::tick(double dt) { pLoc += (unitVecInDirection(sprite.dir) * speed); // Check collision. - const Map& map = game_.getMap(); - bool blocked = false; - - const vec2i UL_COL_BOX = { 8, 8 }; - const vec2i DR_COL_BOX = { 4, 0 }; - vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize(); - vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize(); - vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize(); - vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize(); - - if (dirHasDir(sprite.dir, Direction::right) && - newColPosDR.x() > oldColPosDR.x()) { - for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { - if (map.isBlocked(newColPosDR.x(), y)) { - blocked = true; - pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; - break; - } - } - } - - if (dirHasDir(sprite.dir, Direction::left) && - newColPosUL.x() < oldColPosUL.x()) { - for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { - if (map.isBlocked(newColPosUL.x(), y)) { - blocked = true; - pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; - break; - } - } - } + CollisionResult collision = game_.getSystem().checkCollision(spriteId, pLoc, sprite.dir); + bool blocked = collision.horiz.blocked || collision.vert.blocked; - if (dirHasDir(sprite.dir, Direction::down) && - newColPosDR.y() > oldColPosDR.y()) { - for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { - if (map.isBlocked(x, newColPosDR.y())) { - blocked = true; - pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; - break; - } - } + if (collision.horiz.blocked) { + pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; } - if (dirHasDir(sprite.dir, Direction::up) && - newColPosUL.y() < oldColPosUL.y()) { - for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { - if (map.isBlocked(x, newColPosUL.y())) { - blocked = true; - pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; - break; - } - } + if (collision.vert.blocked) { + pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; } if (blocked && sprite.characterState == CharacterState::Running) { @@ -131,6 +88,8 @@ void CharacterSystem::tick(double dt) { game_.getSystem().moveSprite(spriteId, pLoc); if (sprite.characterState == CharacterState::Running) { + const Map& map = game_.getMap(); + vec2i newMapTileLoc = pLoc / map.getTileSize(); StepType newTileStep = map.getStepType(newMapTileLoc.x(), newMapTileLoc.y()); if (sprite.stepType != newTileStep) { diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 0e602e0..ad7947f 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp @@ -1,5 +1,6 @@ #include "transform_system.h" #include "game.h" +#include "map.h" void TransformSystem::initSprite(int spriteId, vec2i loc) { Sprite& sprite = game_.getSprite(spriteId); @@ -18,3 +19,69 @@ void TransformSystem::moveSprite(int spriteId, vec2i newLoc) { spritesByY_.emplace(newLoc.y(), spriteId); } } + +CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Direction dir) { + CollisionResult result; + + Sprite& sprite = game_.getSprite(spriteId); + + const Map& map = game_.getMap(); + bool blocked = false; + + const vec2i UL_COL_BOX = { 8, 8 }; + const vec2i DR_COL_BOX = { 4, 0 }; + vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize(); + vec2i newColPosUL = (newLoc - UL_COL_BOX) / map.getTileSize(); + vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize(); + vec2i newColPosDR = (newLoc + DR_COL_BOX) / map.getTileSize(); + + if (dirHasDir(sprite.dir, Direction::right) && + newColPosDR.x() > oldColPosDR.x()) { + for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { + if (map.isBlocked(newColPosDR.x(), y)) { + result.horiz.blocked = true; + result.horiz.dir = Direction::right; + + break; + } + } + } + + if (dirHasDir(sprite.dir, Direction::left) && + newColPosUL.x() < oldColPosUL.x()) { + for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { + if (map.isBlocked(newColPosUL.x(), y)) { + result.horiz.blocked = true; + result.horiz.dir = Direction::left; + + break; + } + } + } + + if (dirHasDir(sprite.dir, Direction::down) && + newColPosDR.y() > oldColPosDR.y()) { + for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { + if (map.isBlocked(x, newColPosDR.y())) { + result.vert.blocked = true; + result.vert.dir = Direction::down; + + break; + } + } + } + + if (dirHasDir(sprite.dir, Direction::up) && + newColPosUL.y() < oldColPosUL.y()) { + for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { + if (map.isBlocked(x, newColPosUL.y())) { + result.vert.blocked = true; + result.vert.dir = Direction::up; + + break; + } + } + } + + return result; +} diff --git a/src/transform_system.h b/src/transform_system.h index 290d518..eb1a95b 100644 --- a/src/transform_system.h +++ b/src/transform_system.h @@ -4,11 +4,22 @@ #include #include #include +#include "direction.h" #include "system.h" #include "vector.h" class Game; +struct AxisResult { + Direction dir; + bool blocked = false; +}; + +struct CollisionResult { + AxisResult horiz; + AxisResult vert; +}; + class TransformSystem : public System { public: @@ -26,6 +37,8 @@ public: }); } + CollisionResult checkCollision(int spriteId, vec2i newLoc, Direction dir); + private: Game& game_; -- cgit 1.4.1