63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include "connection.h"
|
|
#include "raylib.h"
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
class ConnectionManager{
|
|
|
|
public:
|
|
|
|
ConnectionManager() {}
|
|
|
|
std::vector<Connection> connections;
|
|
|
|
std::map<Node*, int> counter;
|
|
|
|
Node* next(Node* current, Node* last){
|
|
std::vector<Connection*> paths;
|
|
std::vector<Node*> nodes;
|
|
|
|
for(auto& p : connections){
|
|
if((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) {
|
|
p.active = false;
|
|
paths.push_back(&p);
|
|
nodes.push_back( p.v1 == current ? p.v2 : p.v1 );
|
|
}
|
|
}
|
|
|
|
if(paths.size() == 2) {
|
|
Node* next = nodes[0] == last ? nodes[1] : nodes[0];
|
|
if(paths[0]->v1 == next || paths[0]->v2 == next) paths[0]->active = true;
|
|
if(paths[1]->v1 == next || paths[1]->v2 == next) paths[1]->active = true;
|
|
return next;
|
|
}
|
|
|
|
if(paths.empty()) return nullptr;
|
|
|
|
int return_path = 0;
|
|
|
|
if(current->mode == Distribution::ROUNDROBIN){
|
|
if(counter.find(current) == counter.end()) {
|
|
counter[current] = 0;
|
|
} else {
|
|
counter[current] = (counter[current] + 1) % paths.size();
|
|
return_path = counter[current];
|
|
}
|
|
}
|
|
if(current->mode == Distribution::RANDOM){
|
|
return_path = rand() % paths.size();
|
|
}
|
|
paths.at(return_path)->active = true;
|
|
return paths.at(return_path)->v1 == current ? paths.at(return_path)->v2 : paths.at(return_path)->v1;
|
|
}
|
|
|
|
void draw(Vec2D<float> *mouse_pos, Camera2D *cam){
|
|
std::erase_if(connections, [](Connection c){ return c.remove == true;});
|
|
for(auto& c : connections){
|
|
c.draw(mouse_pos, cam);
|
|
}
|
|
}
|
|
};
|