bool Agent::collideWithLevel(const std::vector<std::string>& levelData) { std::vector<glm::vec2> collideTilePositions; // Check the four corners // First corner checkTilePosition(levelData, collideTilePositions, _position.x, _position.y); // Second Corner checkTilePosition(levelData, collideTilePositions, _position.x + AGENT_WIDTH, _position.y); // Third Corner checkTilePosition(levelData, collideTilePositions, _position.x, _position.y + AGENT_WIDTH); // Third Corner checkTilePosition(levelData, collideTilePositions, _position.x + AGENT_WIDTH, _position.y + AGENT_WIDTH); // Check if there was no collision if (collideTilePositions.size() == 0) { return false; } // Do the collision for (int i = 0; i < collideTilePositions.size(); i++) { collideWithTile(collideTilePositions[i]); } return true; }
// Choose which tiles to check based on movement void Player::detectTileCollisions(World *ptr) { sf::Vector2i currField = ptr->getNField(position); int width = ptr->mapDimensions.x; /* Fig.1: Tile checking pattern. * * % % % . . % . . . % . . * . ^ . . > % . v . % < . * . . . . . % % % % % . . * * */ if(goDown) { collideWithTile(ptr, (currField.y + 1) * width + (currField.x - 1) ); collideWithTile(ptr, (currField.y + 1) * width + (currField.x ) ); collideWithTile(ptr, (currField.y + 1) * width + (currField.x + 1) ); } if(goUp) { collideWithTile(ptr, (currField.y - 1) * width + (currField.x - 1) ); collideWithTile(ptr, (currField.y - 1) * width + (currField.x ) ); collideWithTile(ptr, (currField.y - 1) * width + (currField.x + 1) ); } if(goRight){ collideWithTile(ptr, (currField.y + 1) * width + (currField.x + 1) ); collideWithTile(ptr, (currField.y ) * width + (currField.x + 1) ); collideWithTile(ptr, (currField.y - 1) * width + (currField.x + 1) ); } if(goLeft) { collideWithTile(ptr, (currField.y + 1) * width + (currField.x - 1) ); collideWithTile(ptr, (currField.y ) * width + (currField.x - 1) ); collideWithTile(ptr, (currField.y - 1) * width + (currField.x - 1) ); } }