Advertisement
Zenn_

scrapped lighting system code, first attempt

May 16th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. const PositionComponent* pos = data->ecs->positionSystem->getComp(l->getCompID());
  2.  
  3. //get all edges in a vector first
  4. std::vector<Edge> edges(l->range * l->range);
  5. std::vector<Vertex> vertices(l->range * l->range);
  6.  
  7. std::vector<MapChunk*>* mapChunks = &data->map->mapChunks;
  8. const MapData* activeMap = data->map->activeMap;
  9.  
  10. //tile x loop
  11. for (int x = std::max((int)(pos->position.x - l->range), 0);
  12. x < std::min((int)(pos->position.x + l->range), data->map->activeMap->mapWidth); x++) {
  13. //tile y loop
  14. for (int y = std::max((int)(pos->position.y - l->range), 0);
  15. y < std::min((int)(pos->position.y + l->range), activeMap->mapHeight); y++) {
  16. int index = (y * activeMap->mapWidth + x);
  17.  
  18. bool isSolid =
  19. (bool)mapChunks->at(data->map->globalToChunkPos(index))->chunkSolidityMask->mapDataArray
  20. [data->map->globalToPosInChunk(index)];
  21. if (isSolid) {
  22. if (debug) {
  23. std::cout << "-x: " << x << std::endl;
  24. std::cout << "y: " << y << std::endl;
  25. }
  26. //emplace edges and vertices
  27. edges.emplace_back(Edge(sf::Vector2f(x , y ), sf::Vector2f(x + 1 , y )));
  28. edges.emplace_back(Edge(sf::Vector2f(x + 1 , y ), sf::Vector2f(x + 1 , y + 1 )));
  29. edges.emplace_back(Edge(sf::Vector2f(x + 1 , y + 1 ), sf::Vector2f(x , y + 1 )));
  30. edges.emplace_back(Edge(sf::Vector2f(x , y + 1 ), sf::Vector2f(x , y )));
  31.  
  32. const float posAngle = atan2(pos->position.y, pos->position.x);
  33. // POSITION ANGLE
  34. vertices.emplace_back(Vertex(sf::Vector2f(x , y ), (posAngle - atan2(y , x )) * PIRAD));
  35. if (debug) std::cout << "1: " << vertices.end()->angle << '\n';
  36. vertices.emplace_back(Vertex(sf::Vector2f(x + 1 , y ), (posAngle - atan2(y , x + 1.0f)) * PIRAD));
  37. if (debug) std::cout << "2: " << vertices.end()->angle << '\n';
  38. vertices.emplace_back(Vertex(sf::Vector2f(x + 1 , y + 1 ), (posAngle - atan2(y + 1.0f, x + 1.0f)) * PIRAD));
  39. if (debug) std::cout << "3: " << vertices.end()->angle << '\n';
  40. vertices.emplace_back(Vertex(sf::Vector2f(x , y + 1 ), (posAngle - atan2(y + 1.0f, x )) * PIRAD));
  41. if (debug) std::cout << "4: " << vertices.end()->angle << '\n';
  42. }
  43. }
  44. }
  45. //sort vertices by angle
  46. std::sort(vertices.begin(), vertices.end(),
  47. [](const Vertex &a, const Vertex &b) {
  48. return a.angle < b.angle;
  49. }
  50. );
  51. //sweeping angle raycasting and all that stuff
  52. if (debug) {
  53. for (int i = 0; i < vertices.size(); i++) {
  54. std::cout << "vertex angle " << i << ": " << std::to_string(vertices.at(i).angle) << "\n";
  55. }
  56. std::cout << "vertex amount: " + std::to_string(vertices.size()) << "\n";
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement