diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-10 01:20:08 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-10 01:20:08 +0000 |
commit | d240fc840ebe35499e09c48960d4d89ce25c2824 (patch) | |
tree | 5a0423cd488e38bb8dc3e9ab4a81f8959ad95c73 | |
parent | Vertices and indices vectors not required once mesh is created (diff) | |
download | ilt-d240fc840ebe35499e09c48960d4d89ce25c2824.tar.bz2 ilt-d240fc840ebe35499e09c48960d4d89ce25c2824.tar.xz ilt-d240fc840ebe35499e09c48960d4d89ce25c2824.zip |
Add some more helpers to Network
-rw-r--r-- | game/network/network.cpp | 14 | ||||
-rw-r--r-- | game/network/network.h | 3 | ||||
-rw-r--r-- | game/network/rail.cpp | 7 | ||||
-rw-r--r-- | game/network/rail.h | 4 |
4 files changed, 21 insertions, 7 deletions
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,10 +2,24 @@ #include <cache.h> #include <game/network/link.h> #include <gfx/models/texture.h> +#include <utility> Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } NodePtr +Network::nodeAt(glm::vec3 pos) +{ + return *nodes.insert(std::make_shared<Node>(pos)).first; +} + +std::pair<NodePtr, bool> +Network::newNodeAt(glm::vec3 pos) +{ + const auto i = nodes.insert(std::make_shared<Node>(pos)); + return {*i.first, i.second}; +} + +NodePtr Network::findNodeAt(glm::vec3 pos) const { if (const auto n = nodes.find(std::make_shared<Node>(pos)); n != nodes.end()) { 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 <set> #include <sorting.hpp> #include <string> +#include <utility> 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<NodePtr, bool> newNodeAt(glm::vec3); protected: using Nodes = std::set<NodePtr, PtrSorter<NodePtr>>; 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<RailLink> RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { - auto node1ins = nodes.insert(std::make_shared<Node>(start)); - auto node2ins = nodes.insert(std::make_shared<Node>(end)); + auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); if (node1ins.second && node2ins.second) { // Both nodes are new, direct link, easy return addLink<RailLinkStraight>(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 <location.hpp> #include <maths.h> #include <memory> -#include <set> #include <span> #include <utility> @@ -60,8 +59,7 @@ public: std::shared_ptr<T> addLink(glm::vec3 a, glm::vec3 b, Params &&... params) { - const auto node1 = *nodes.insert(std::make_shared<Node>(a)).first; - const auto node2 = *nodes.insert(std::make_shared<Node>(b)).first; + const auto node1 = nodeAt(a), node2 = nodeAt(b); auto l {links.create<T>(node1, node2, std::forward<Params>(params)...)}; joinLinks(l); return l; |