Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include "LightingSystem.h"
- #include "Map.h"
- #include "ECS.h"
- #include "PositionSystem.h"
- //use this for edges, defined by two vertices
- struct Edge {
- Edge(sf::Vector2f FIRST, sf::Vector2f SECOND): first(FIRST), second(SECOND){}
- Edge(){}
- sf::Vector2f first;
- sf::Vector2f second;
- };
- struct Vertex {
- Vertex(sf::Vector2f POS, float ANGLE) : pos(POS), angle(ANGLE){}
- Vertex() {}
- sf::Vector2f pos;
- float angle;
- };
- namespace station {
- LightingSystem::LightingSystem(GameData* DATA) : System(DATA) {}
- LightingSystem::~LightingSystem() {}
- void LightingSystem::init() {
- chunks = &data->map->mapChunks;
- generateLightMap();
- }
- void LightingSystem::update(float dt) {
- generateLightMap();
- }
- void LightingSystem::draw(sf::RenderTarget &target, sf::RenderStates states) const {
- }
- void LightingSystem::generateLightMap() {
- for (LightingComponent* l : components) {
- PositionComponent* pos = data->ecs->positionSystem->getComp(l->getCompID());
- //get all edges in a vector first
- std::vector<Edge> edges(l->range * l->range);
- std::vector<Vertex> vertices(l->range * l->range);
- std::vector<MapChunk*>* mapChunks = &data->map->mapChunks;
- MapData* activeMap = data->map->activeMap;
- //tile x loop
- for (int x = std::max((int)(pos->position.x - l->range), 0);
- x < std::min((int)(pos->position.x + l->range), data->map->activeMap->mapWidth); x++) {
- //tile y loop
- for (int y = std::max((int)(pos->position.y - l->range), 0);
- y < std::min((int)(pos->position.y + l->range), activeMap->mapHeight); y++) {
- int index = (y * activeMap->mapWidth + x);
- bool isSolid =
- (bool)mapChunks->at(data->map->globalToChunkPos(index))->chunkSolidityMask->mapDataArray
- [data->map->globalToPosInChunk(index)];
- //emplace edges and vertices
- if (isSolid) {
- edges.emplace_back(Edge(sf::Vector2f(x , y ), sf::Vector2f(x + 1 , y )));
- edges.emplace_back(Edge(sf::Vector2f(x + 1 , y ), sf::Vector2f(x + 1 , y + 1 )));
- edges.emplace_back(Edge(sf::Vector2f(x + 1 , y + 1 ), sf::Vector2f(x , y + 1 )));
- edges.emplace_back(Edge(sf::Vector2f(x , y + 1 ), sf::Vector2f(x , y )));
- vertices.emplace_back(Vertex(sf::Vector2f(x, y), atan2(y, x) * 180 / PI));
- vertices.emplace_back(Vertex(sf::Vector2f(x + 1, y), atan2(y, x + 1) * 180 / PI));
- vertices.emplace_back(Vertex(sf::Vector2f(x + 1, y + 1), atan2(y + 1, x + 1) * 180 / PI));
- vertices.emplace_back(Vertex(sf::Vector2f(x, y + 1), atan2(y + 1, x) * 180 / PI));
- }
- //sort vertices by angle
- std::sort(vertices.begin(), vertices.begin() + vertices.size(),
- [](const Vertex &a, const Vertex &b) {
- return a.angle < b.angle;
- }
- );
- //sweeping angle raycasting and all that stuff
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment