89 lines
2.5 KiB
C++
89 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "connection.h"
|
|
#include "node-manager.h"
|
|
#include "raylib.h"
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
class ConnectionManager{
|
|
|
|
public:
|
|
|
|
ConnectionManager(NodeManager *nm) : nm(nm) {}
|
|
|
|
NodeManager *nm;
|
|
|
|
std::vector<Connection> connections;
|
|
|
|
std::map<int, int> counter;
|
|
|
|
void reset(){
|
|
counter.clear();
|
|
}
|
|
|
|
int next(int current, int last){
|
|
std::vector<Connection*> possible_connections;
|
|
std::vector<int> next_node_ids;
|
|
|
|
for(auto& c : connections){
|
|
if((c.node_id_start == current && c.v1_v2) || (c.node_id_end == current && c.v2_v1)) {
|
|
c.active = false;
|
|
possible_connections.push_back(&c);
|
|
next_node_ids.push_back( c.node_id_start == current ? c.node_id_end : c.node_id_start );
|
|
}
|
|
}
|
|
|
|
if(possible_connections.empty()) return -1;
|
|
|
|
int return_path = 0;
|
|
float angle = -1;
|
|
if(nm->nodes.at(current).distributionMode == Distribution::GEOMETRIC){
|
|
Vec2D<float> coming_from = (nm->nodes.at(current).position - nm->nodes.at(last).position).norm();
|
|
int i = 0;
|
|
for(auto n : possible_connections){
|
|
int next_position = n->node_id_start == current ? n->node_id_end : n->node_id_start;
|
|
float angle_v1_v2 = coming_from.norm().dot((nm->nodes.at(next_position).position - nm->nodes.at(current).position).norm());
|
|
if(angle_v1_v2 > angle) {
|
|
return_path = i;
|
|
angle = angle_v1_v2;
|
|
}
|
|
++i;
|
|
}
|
|
}
|
|
|
|
if(nm->nodes.at(current).distributionMode == Distribution::ROUNDROBIN){
|
|
if(counter.find(current) == counter.end()) {
|
|
counter[current] = 0;
|
|
} else {
|
|
counter[current] = (counter[current] + 1) % possible_connections.size();
|
|
return_path = counter[current];
|
|
}
|
|
}
|
|
|
|
if(nm->nodes.at(current).distributionMode == Distribution::RANDOM){
|
|
return_path = rand() % possible_connections.size();
|
|
}
|
|
|
|
possible_connections.at(return_path)->active = true;
|
|
return possible_connections.at(return_path)->node_id_start == current ? possible_connections.at(return_path)->node_id_end : possible_connections.at(return_path)->node_id_start;
|
|
}
|
|
|
|
void addConnection(int from, int to){
|
|
connections.emplace_back(nm, from, to);
|
|
}
|
|
|
|
void splitConnection(Connection *c){
|
|
Vec2D<float> center = c->node_start->position.center(c->node_end->position);
|
|
|
|
}
|
|
|
|
void drawConnections(Vec2D<float> *mouse_pos, Camera2D *cam){
|
|
std::erase_if(connections, [](Connection c){ return c.remove == true;});
|
|
for(auto& c : connections){
|
|
if(c.split) splitConnection(&c);
|
|
c.draw(mouse_pos, cam);
|
|
}
|
|
}
|
|
};
|