From 789a16e4e97d4050bb8db9cc3aebb01e807ac8c7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Mar 2021 01:54:40 +0000 Subject: Push more RailLinks logic down into NetworkOf --- game/network/network.cpp | 17 +++++++++++++++++ game/network/network.h | 13 +++++++++++++ game/network/network.impl.h | 9 +++++++++ game/network/rail.cpp | 18 ------------------ game/network/rail.h | 13 ------------- 5 files changed, 39 insertions(+), 31 deletions(-) (limited to 'game') diff --git a/game/network/network.cpp b/game/network/network.cpp index 52d88ec..fd61764 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -1,7 +1,9 @@ #include "network.h" +#include #include #include #include +#include #include Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } @@ -27,3 +29,18 @@ Network::findNodeAt(glm::vec3 pos) const } return {}; } + +void +Network::joinLinks(const LinkPtr & l, const LinkPtr & ol) +{ + if (l != ol) { + for (const auto oe : {0, 1}) { + for (const auto te : {0, 1}) { + if (l->ends[te].node == ol->ends[oe].node) { + l->ends[te].nexts.emplace_back(ol, oe); + ol->ends[oe].nexts.emplace_back(l, te); + } + } + } + } +} diff --git a/game/network/network.h b/game/network/network.h index c42f056..c6d4dfa 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -23,6 +23,8 @@ public: [[nodiscard]] std::pair newNodeAt(glm::vec3); protected: + static void joinLinks(const LinkPtr & l, const LinkPtr & ol); + using Nodes = std::set>; Nodes nodes; std::shared_ptr texture; @@ -33,8 +35,19 @@ protected: using Network::Network; Collection links; + void joinLinks(const LinkPtr &) const; public: + template + std::shared_ptr + addLink(glm::vec3 a, glm::vec3 b, Params &&... params) requires std::is_base_of_v + { + const auto node1 = nodeAt(a), node2 = nodeAt(b); + auto l {links.template create(node1, node2, std::forward(params)...)}; + joinLinks(l); + return l; + } + void render(const Shader &) const override; }; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index ddb1ae5..699cb94 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -10,3 +10,12 @@ NetworkOf::render(const Shader & shader) const texture->Bind(); links.apply(&T::render, shader); } + +template +void +NetworkOf::joinLinks(const LinkPtr & l) const +{ + for (const auto & ol : links.objects) { + Network::joinLinks(l, ol); + } +} diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 7cc6284..0ef58d7 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -7,7 +7,6 @@ #include // IWYU pragma: keep #include #include -#include #include #include #include @@ -22,23 +21,6 @@ constexpr glm::vec3 RAIL_HEIGHT {0, .25F, 0}; RailLinks::RailLinks() : NetworkOf {"rails.jpg"} { } void RailLinks::tick(TickDuration) { } -void -RailLinks::joinLinks(const LinkPtr & l) const -{ - for (const auto & ol : links.objects) { - if (l != ol) { - for (const auto oe : {0, 1}) { - for (const auto te : {0, 1}) { - if (l->ends[te].node == ol->ends[oe].node) { - l->ends[te].nexts.emplace_back(ol, oe); - ol->ends[oe].nexts.emplace_back(l, te); - } - } - } - } - } -} - std::shared_ptr RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { diff --git a/game/network/rail.h b/game/network/rail.h index 3b3da42..3ecc59f 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -11,7 +11,6 @@ #include #include #include -#include class Shader; class Vertex; @@ -50,26 +49,14 @@ private: Arc arc; }; -template concept RailLinkConcept = std::is_base_of_v; - class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); - template - std::shared_ptr - addLink(glm::vec3 a, glm::vec3 b, Params &&... params) - { - const auto node1 = nodeAt(a), node2 = nodeAt(b); - auto l {links.create(node1, node2, std::forward(params)...)}; - joinLinks(l); - return l; - } std::shared_ptr addLinksBetween(glm::vec3 start, glm::vec3 end); private: void tick(TickDuration elapsed) override; - void joinLinks(const LinkPtr &) const; }; #endif -- cgit v1.2.3