Skip to content
Snippets Groups Projects
Commit 1cf8f72e authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

shield the grid against out of bounds

parent 5deaeaaf
No related branches found
No related tags found
No related merge requests found
......@@ -91,7 +91,11 @@ namespace demo {
} else if (cell.direction == 3) {
moveDir.x = -1;
}
cell.pos += moveDir;
if ( m_grid->canMove(cell.pos, moveDir) ) {
cell.pos += moveDir;
}
}
inline void rotate(bool clockwise) {
......
......@@ -98,6 +98,11 @@ namespace fggl::entity::grid {
clear();
}
inline bool inBounds(GridPos pos) const {
return 0 <= pos.x && pos.x <= width
&& 0 <= pos.y && pos.y <= height;
}
void clear() {
WallState noWall;
......@@ -135,7 +140,10 @@ namespace fggl::entity::grid {
}
inline bool canMove(GridPos pos) const {
return m_tiles.m_floors[m_floors.getCell(pos)] != 0;
if ( !inBounds(pos) ) {
return false;
}
return m_tiles.m_floors[m_floors.get(pos)].moveCost != FloorTile::IMPOSSIBLE;
}
inline bool canMove(GridPos pos, math::vec2i dir) const {
......@@ -143,11 +151,25 @@ namespace fggl::entity::grid {
}
inline bool blocked(GridPos pos, math::vec2i dir) const {
if ( dir.x == -1 || dir.y == -1 ) {
return m_walls.getCell(pos) != 0;
} else {
m_walls.getCell(pos + dir) != 0;
auto targetPos = pos;
if ( dir.x == 1 || dir.y == 1 ) {
targetPos = pos + dir;
}
if ( !inBounds(targetPos) ) {
return true;
}
auto& wallObj = m_walls.get(targetPos);
if ( dir.y != 0 ) {
return wallObj.north != 0;
}
if (dir.x != 0) {
return wallObj.west != 0;
}
return true;
}
EntityManager& entities() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment