Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const 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;
- const 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)];
- if (isSolid) {
- if (debug) {
- std::cout << "-x: " << x << std::endl;
- std::cout << "y: " << y << std::endl;
- }
- //emplace edges and vertices
- 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 )));
- const float posAngle = atan2(pos->position.y, pos->position.x);
- // POSITION ANGLE
- vertices.emplace_back(Vertex(sf::Vector2f(x , y ), (posAngle - atan2(y , x )) * PIRAD));
- if (debug) std::cout << "1: " << vertices.end()->angle << '\n';
- vertices.emplace_back(Vertex(sf::Vector2f(x + 1 , y ), (posAngle - atan2(y , x + 1.0f)) * PIRAD));
- if (debug) std::cout << "2: " << vertices.end()->angle << '\n';
- vertices.emplace_back(Vertex(sf::Vector2f(x + 1 , y + 1 ), (posAngle - atan2(y + 1.0f, x + 1.0f)) * PIRAD));
- if (debug) std::cout << "3: " << vertices.end()->angle << '\n';
- vertices.emplace_back(Vertex(sf::Vector2f(x , y + 1 ), (posAngle - atan2(y + 1.0f, x )) * PIRAD));
- if (debug) std::cout << "4: " << vertices.end()->angle << '\n';
- }
- }
- }
- //sort vertices by angle
- std::sort(vertices.begin(), vertices.end(),
- [](const Vertex &a, const Vertex &b) {
- return a.angle < b.angle;
- }
- );
- //sweeping angle raycasting and all that stuff
- if (debug) {
- for (int i = 0; i < vertices.size(); i++) {
- std::cout << "vertex angle " << i << ": " << std::to_string(vertices.at(i).angle) << "\n";
- }
- std::cout << "vertex amount: " + std::to_string(vertices.size()) << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement