Zenn_

Lighting System WIP

May 13th, 2019
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include "LightingSystem.h"
  4. #include "Map.h"
  5. #include "ECS.h"
  6. #include "PositionSystem.h"
  7.  
  8. //use this for edges, defined by two vertices
  9. struct Edge {
  10.     Edge(sf::Vector2f FIRST, sf::Vector2f SECOND): first(FIRST), second(SECOND){}
  11.     Edge(){}
  12.  
  13.     sf::Vector2f first;
  14.     sf::Vector2f second;
  15. };
  16.  
  17. struct Vertex {
  18.     Vertex(sf::Vector2f POS, float ANGLE) : pos(POS), angle(ANGLE){}
  19.     Vertex() {}
  20.  
  21.     sf::Vector2f pos;
  22.     float angle;
  23. };
  24.  
  25. namespace station {
  26.     LightingSystem::LightingSystem(GameData* DATA) : System(DATA) {}
  27.     LightingSystem::~LightingSystem() {}
  28.  
  29.     void LightingSystem::init() {
  30.         chunks = &data->map->mapChunks;
  31.         generateLightMap();
  32.     }
  33.     void LightingSystem::update(float dt) {
  34.         generateLightMap();
  35.     }
  36.     void LightingSystem::draw(sf::RenderTarget &target, sf::RenderStates states) const {
  37.    
  38.     }
  39.  
  40.     void LightingSystem::generateLightMap() {
  41.         for (LightingComponent* l : components) {
  42.             PositionComponent* pos = data->ecs->positionSystem->getComp(l->getCompID());
  43.  
  44.             //get all edges in a vector first
  45.             std::vector<Edge> edges(l->range * l->range);
  46.             std::vector<Vertex> vertices(l->range * l->range);
  47.  
  48.             std::vector<MapChunk*>* mapChunks = &data->map->mapChunks;
  49.             MapData* activeMap = data->map->activeMap;
  50.  
  51.             //tile x loop
  52.             for (int x = std::max((int)(pos->position.x - l->range), 0);
  53.                 x < std::min((int)(pos->position.x + l->range), data->map->activeMap->mapWidth); x++) {
  54.                 //tile y loop
  55.                 for (int y = std::max((int)(pos->position.y - l->range), 0);
  56.                     y < std::min((int)(pos->position.y + l->range), activeMap->mapHeight); y++) {
  57.                     int index = (y * activeMap->mapWidth + x);
  58.                     bool isSolid =
  59.                         (bool)mapChunks->at(data->map->globalToChunkPos(index))->chunkSolidityMask->mapDataArray
  60.                             [data->map->globalToPosInChunk(index)];
  61.  
  62.                     //emplace edges and vertices
  63.                     if (isSolid) {
  64.                         edges.emplace_back(Edge(sf::Vector2f(x      , y     ), sf::Vector2f(x + 1   , y     )));
  65.                         edges.emplace_back(Edge(sf::Vector2f(x + 1  , y     ), sf::Vector2f(x + 1   , y + 1 )));
  66.                         edges.emplace_back(Edge(sf::Vector2f(x + 1  , y + 1 ), sf::Vector2f(x       , y + 1 )));
  67.                         edges.emplace_back(Edge(sf::Vector2f(x      , y + 1 ), sf::Vector2f(x       , y     )));
  68.                    
  69.                         vertices.emplace_back(Vertex(sf::Vector2f(x, y), atan2(y, x) * 180 / PI));
  70.                         vertices.emplace_back(Vertex(sf::Vector2f(x + 1, y), atan2(y, x + 1) * 180 / PI));
  71.                         vertices.emplace_back(Vertex(sf::Vector2f(x + 1, y + 1), atan2(y + 1, x + 1) * 180 / PI));
  72.                         vertices.emplace_back(Vertex(sf::Vector2f(x, y + 1), atan2(y + 1, x) * 180 / PI));
  73.                     }
  74.  
  75.                     //sort vertices by angle
  76.                     std::sort(vertices.begin(), vertices.begin() + vertices.size(),
  77.                         [](const Vertex &a, const Vertex &b) {
  78.                             return a.angle < b.angle;
  79.                         }
  80.                     );
  81.  
  82.                     //sweeping angle raycasting and all that stuff
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment