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/transform_system.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/transform_system.cpp') 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; +} -- cgit 1.4.1