From d240fc840ebe35499e09c48960d4d89ce25c2824 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Mar 2021 01:20:08 +0000 Subject: Add some more helpers to Network --- game/network/network.cpp | 14 ++++++++++++++ game/network/network.h | 3 +++ game/network/rail.cpp | 7 +++---- game/network/rail.h | 4 +--- 4 files changed, 21 insertions(+), 7 deletions(-) (limited to 'game/network') diff --git a/game/network/network.cpp b/game/network/network.cpp index adda8fc..52d88ec 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -2,9 +2,23 @@ #include #include #include +#include Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } +NodePtr +Network::nodeAt(glm::vec3 pos) +{ + return *nodes.insert(std::make_shared(pos)).first; +} + +std::pair +Network::newNodeAt(glm::vec3 pos) +{ + const auto i = nodes.insert(std::make_shared(pos)); + return {*i.first, i.second}; +} + NodePtr Network::findNodeAt(glm::vec3 pos) const { diff --git a/game/network/network.h b/game/network/network.h index 2c24916..c42f056 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -9,6 +9,7 @@ #include #include #include +#include class Texture; class Shader; @@ -18,6 +19,8 @@ public: explicit Network(const std::string & textureName); [[nodiscard]] NodePtr findNodeAt(glm::vec3) const; + [[nodiscard]] NodePtr nodeAt(glm::vec3); + [[nodiscard]] std::pair newNodeAt(glm::vec3); protected: using Nodes = std::set>; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index c84f97f..7cc6284 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -42,8 +42,7 @@ RailLinks::joinLinks(const LinkPtr & l) const std::shared_ptr RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { - auto node1ins = nodes.insert(std::make_shared(start)); - auto node2ins = nodes.insert(std::make_shared(end)); + auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); if (node1ins.second && node2ins.second) { // Both nodes are new, direct link, easy return addLink(start, end); @@ -65,14 +64,14 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) } throw std::runtime_error("Node exists but couldn't find it"); }; - float dir = pi + findDir(*node1ins.first); + float dir = pi + findDir(node1ins.first); const glm::vec2 flatStart {!start}, flatEnd {!end}; if (!node2ins.second) { auto midheight = [&](auto mid) { const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); return start.y + ((end.y - start.y) * (sm / (sm + em))); }; - float dir2 = pi + findDir(*node2ins.first); + float dir2 = pi + findDir(node2ins.first); if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { const auto radius {radii.first}; const auto c1 = flatStart + sincosf(dir + half_pi) * radius; diff --git a/game/network/rail.h b/game/network/rail.h index c67dc67..3b3da42 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -60,8 +59,7 @@ public: std::shared_ptr addLink(glm::vec3 a, glm::vec3 b, Params &&... params) { - const auto node1 = *nodes.insert(std::make_shared(a)).first; - const auto node2 = *nodes.insert(std::make_shared(b)).first; + const auto node1 = nodeAt(a), node2 = nodeAt(b); auto l {links.create(node1, node2, std::forward(params)...)}; joinLinks(l); return l; -- cgit v1.2.3