halexandru11

Node.hpp

Dec 7th, 2021
571
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include "Constants.hpp"
  4.  
  5. struct Node {
  6. public:
  7.     Node(Constants::NodeType nodeType_) {
  8.         nodeType = nodeType_;
  9.     }
  10.  
  11.     void setCoordonates(sf::Vector2f coord) {
  12.         m_coord = coord;
  13.     }
  14.  
  15.     sf::Vector2f getCoordonates() {
  16.         return m_coord;
  17.     }
  18.  
  19.     sf::ConvexShape getShape() {
  20.         if(m_coord == sf::Vector2f{-1, -1}) {
  21.             throw std::invalid_argument("You did not set the coordinates for this shape");
  22.         }
  23.         if(!m_shapeAssigned) {
  24.             m_shapeAssigned = true;
  25.             m_shape = setShape();
  26.         }
  27.         return m_shape;
  28.     }
  29.  
  30. public:
  31.     Constants::NodeType nodeType;
  32.     int height;
  33.     int width;
  34.     sf::RectangleShape hitbox;
  35.  
  36. private:
  37.     sf::ConvexShape setShape() {
  38.         hitbox = sf::RectangleShape(sf::Vector2f{100, 30});
  39.         hitbox.setPosition(m_coord - sf::Vector2f{50, 15});
  40.         hitbox.setFillColor(sf::Color::Transparent);
  41.         hitbox.setOutlineColor(sf::Color::Green);
  42.         hitbox.setOutlineThickness(2);
  43.         switch(nodeType) {
  44.         case Constants::StartNode:
  45.             return startNodeShape();
  46.         case Constants::AssignNode:
  47.             return assignNodeShape();
  48.         case Constants::ConditionalNode:
  49.             return conditionalNodeShape();
  50.         case Constants::StopNode:
  51.             return stopNodeShape();
  52.         default:
  53.             return sf::ConvexShape(0);
  54.         }
  55.     }
  56.  
  57.     sf::ConvexShape startNodeShape() {
  58.         width = 100;
  59.         height = 30;
  60.  
  61.         sf::ConvexShape convexShape;
  62.         convexShape.setPointCount(4);
  63.  
  64.         convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  65.         convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  66.         convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2,  height/2});
  67.         convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2,  height/2});
  68.  
  69.         return convexShape;
  70.     }
  71.  
  72.     sf::ConvexShape assignNodeShape() {
  73.         width = 100;
  74.         height = 30;
  75.  
  76.         sf::ConvexShape convexShape;
  77.         convexShape.setPointCount(4);
  78.  
  79.         convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  80.         convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  81.         convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2-20,  height/2});
  82.         convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2+20,  height/2});
  83.  
  84.         return convexShape;
  85.     }
  86.  
  87.     sf::ConvexShape conditionalNodeShape() {
  88.         width = 100;
  89.         height = 30;
  90.  
  91.         sf::ConvexShape convexShape;
  92.         convexShape.setPointCount(4);
  93.  
  94.         convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2+20, -height/2});
  95.         convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2-20, -height/2});
  96.         convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2,  height/2});
  97.         convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2,  height/2});
  98.  
  99.         return convexShape;
  100.     }
  101.  
  102.     sf::ConvexShape stopNodeShape() {
  103.         width = 100;
  104.         height = 30;
  105.  
  106.         sf::ConvexShape convexShape;
  107.         convexShape.setPointCount(4);
  108.  
  109.         convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  110.         convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  111.         convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2,  height/2});
  112.         convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2,  height/2});
  113.  
  114.         return convexShape;
  115.     }
  116.  
  117. private:
  118.     sf::Vector2f m_coord = sf::Vector2f{-1, -1};
  119.     sf::ConvexShape m_shape;
  120.     bool m_shapeAssigned = false;
  121. };
  122.  
RAW Paste Data