Facebook
From valva-ro, 4 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 56
  1. #include <cstdint>
  2. #include <SFML/Graphics.hpp>
  3.  
  4. const uint8_t ALL_WALLS = 0b1111;
  5. const uint8_t LEFT_WALL = 0b1000;
  6. const uint8_t DOWN_WALL = 0b0100;
  7. const uint8_t RIGHT_WALL = 0b0010;
  8. const uint8_t UP_WALL = 0b0001;
  9.  
  10. class Cell {
  11.  
  12. private:
  13.     int column;
  14.     int row;
  15.     sf::RectangleShape cell;
  16.     sf::RectangleShape walls[4];
  17.     sf::Texture background;
  18.     uint8_t activeWalls;
  19.  
  20. public:
  21.     Cell(sf::Vector2f position, sf::Vector2f size, int column, int row, float wallWidth,
  22.          sf::Color backgroundColor, sf::Color wallColor);
  23.     void draw(sf::RenderWindow *window);
  24.     uint8_t getActiveWalls();
  25.     int getColumn();
  26.     int getRow();
  27.     void setActiveWalls(uint8_t walls);
  28.     void setBackgroundColor(sf::Color backgroundColor);
  29.     void setTexture(const std::string &texturePath);
  30.     void removeWalls(uint8_t walls);
  31.     bool isVisited = false;
  32. };
  33.  
  34. Cell::Cell(sf::Vector2f position, sf::Vector2f size, int column, int row, float wallWidth, sf::Color backgroundColor, sf::Color wallColor) {
  35.     this->cell = sf::RectangleShape(size);
  36.     this->cell.setPosition(position);
  37.     this->cell.setFillColor(backgroundColor);
  38.     this->column = column;
  39.     this->row = row;
  40.     this->activeWalls = ALL_WALLS;
  41.  
  42.     this->walls[0] = sf::RectangleShape(sf::Vector2f(size.x, wallWidth));
  43.     this->walls[0].setPosition(sf::Vector2f(position.x, position.y));
  44.     this->walls[0].setFillColor(wallColor);
  45.  
  46.     this->walls[1] = sf::RectangleShape(sf::Vector2f(wallWidth, size.y));
  47.     this->walls[1].setPosition(sf::Vector2f(position.x + size.x - wallWidth, position.y));
  48.     this->walls[1].setFillColor(wallColor);
  49.  
  50.     this->walls[2] = sf::RectangleShape(sf::Vector2f(size.x, wallWidth));
  51.     this->walls[2].setPosition(sf::Vector2f(position.x, position.y + size.y - wallWidth));
  52.     this->walls[2].setFillColor(wallColor);
  53.  
  54.     this->walls[3] = sf::RectangleShape(sf::Vector2f(wallWidth, size.y));
  55.     this->walls[3].setPosition(sf::Vector2f(position.x, position.y));
  56.     this->walls[3].setFillColor(wallColor);
  57. }
  58.  
  59. void Cell::draw(sf::RenderWindow *window) {
  60.     if(this->isVisited)
  61.         window->draw(this->cell);
  62.     if(this->activeWalls & 0b1)
  63.         window->draw(this->walls[0]);
  64.     if((this->activeWalls >> 1) & 0b1)
  65.         window->draw(this->walls[1]);
  66.     if((this->activeWalls >> 2) & 0b1)
  67.         window->draw(this->walls[2]);
  68.     if((this->activeWalls >> 3) & 0b1)
  69.         window->draw(this->walls[3]);
  70. }
  71.  
  72. uint8_t Cell::getActiveWalls() {
  73.     if (this->activeWalls)
  74.         return this->activeWalls;
  75. }
  76.  
  77. void Cell::setActiveWalls(uint8_t walls) {
  78.     this->activeWalls = walls;
  79. }
  80.  
  81. void Cell::setBackgroundColor(sf::Color backgroundColor) {
  82.     this->cell.setFillColor(backgroundColor);
  83. }
  84.  
  85. void Cell::setTexture(const std::string &texturePath) {
  86.     if (this->background.loadFromFile(texturePath)) {
  87.         this->cell.setTexture(&background, true);
  88.     }
  89. }
  90.  
  91. void Cell::removeWalls(uint8_t walls) {
  92.     this->activeWalls &= (walls ^ 0b1111);
  93. }
  94.  
  95. int Cell::getColumn() {
  96.     return this->column;
  97. }
  98.  
  99. int Cell::getRow() {
  100.     return this->row;
  101. }