diff options
-rw-r--r-- | game/network/network.cpp | 17 | ||||
-rw-r--r-- | game/network/network.h | 13 | ||||
-rw-r--r-- | game/network/network.impl.h | 9 | ||||
-rw-r--r-- | game/network/rail.cpp | 18 | ||||
-rw-r--r-- | game/network/rail.h | 13 |
5 files changed, 39 insertions, 31 deletions
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 <array> #include <cache.h> #include <game/network/link.h> #include <gfx/models/texture.h> +#include <initializer_list> #include <utility> 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<NodePtr, bool> newNodeAt(glm::vec3); protected: + static void joinLinks(const LinkPtr & l, const LinkPtr & ol); + using Nodes = std::set<NodePtr, PtrSorter<NodePtr>>; Nodes nodes; std::shared_ptr<Texture> texture; @@ -33,8 +35,19 @@ protected: using Network::Network; Collection<T> links; + void joinLinks(const LinkPtr &) const; public: + template<typename L, typename... Params> + std::shared_ptr<L> + addLink(glm::vec3 a, glm::vec3 b, Params &&... params) requires std::is_base_of_v<T, L> + { + const auto node1 = nodeAt(a), node2 = nodeAt(b); + auto l {links.template create<L>(node1, node2, std::forward<Params>(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<T>::render(const Shader & shader) const texture->Bind(); links.apply(&T::render, shader); } + +template<typename T> +void +NetworkOf<T>::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 <game/network/network.impl.h> // IWYU pragma: keep #include <gfx/models/vertex.hpp> #include <glm/gtx/transform.hpp> -#include <initializer_list> #include <location.hpp> #include <maths.h> #include <stdexcept> @@ -22,23 +21,6 @@ constexpr glm::vec3 RAIL_HEIGHT {0, .25F, 0}; RailLinks::RailLinks() : NetworkOf<RailLink> {"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<RailLink> 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 <maths.h> #include <memory> #include <span> -#include <utility> class Shader; class Vertex; @@ -50,26 +49,14 @@ private: Arc arc; }; -template<typename T> concept RailLinkConcept = std::is_base_of_v<RailLink, T>; - class RailLinks : public NetworkOf<RailLink>, public WorldObject { public: RailLinks(); - template<RailLinkConcept T, typename... Params> - std::shared_ptr<T> - addLink(glm::vec3 a, glm::vec3 b, Params &&... params) - { - const auto node1 = nodeAt(a), node2 = nodeAt(b); - auto l {links.create<T>(node1, node2, std::forward<Params>(params)...)}; - joinLinks(l); - return l; - } std::shared_ptr<RailLink> addLinksBetween(glm::vec3 start, glm::vec3 end); private: void tick(TickDuration elapsed) override; - void joinLinks(const LinkPtr &) const; }; #endif |