diff options
-rw-r--r-- | game/network/network.cpp | 15 | ||||
-rw-r--r-- | game/network/network.h | 38 | ||||
-rw-r--r-- | game/network/network.impl.h | 12 | ||||
-rw-r--r-- | game/network/rail.cpp | 28 | ||||
-rw-r--r-- | game/network/rail.h | 11 |
5 files changed, 73 insertions, 31 deletions
diff --git a/game/network/network.cpp b/game/network/network.cpp new file mode 100644 index 0000000..adda8fc --- /dev/null +++ b/game/network/network.cpp @@ -0,0 +1,15 @@ +#include "network.h" +#include <cache.h> +#include <game/network/link.h> +#include <gfx/models/texture.h> + +Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { } + +NodePtr +Network::findNodeAt(glm::vec3 pos) const +{ + if (const auto n = nodes.find(std::make_shared<Node>(pos)); n != nodes.end()) { + return *n; + } + return {}; +} diff --git a/game/network/network.h b/game/network/network.h new file mode 100644 index 0000000..2c24916 --- /dev/null +++ b/game/network/network.h @@ -0,0 +1,38 @@ +#ifndef NETWORK_H +#define NETWORK_H + +#include "link.h" +#include <collection.hpp> +#include <gfx/renderable.h> +#include <glm/glm.hpp> +#include <memory> +#include <set> +#include <sorting.hpp> +#include <string> + +class Texture; +class Shader; + +class Network { +public: + explicit Network(const std::string & textureName); + + [[nodiscard]] NodePtr findNodeAt(glm::vec3) const; + +protected: + using Nodes = std::set<NodePtr, PtrSorter<NodePtr>>; + Nodes nodes; + std::shared_ptr<Texture> texture; +}; + +template<typename T> class NetworkOf : public Network, public Renderable { +protected: + using Network::Network; + + Collection<T> links; + +public: + void render(const Shader &) const override; +}; + +#endif diff --git a/game/network/network.impl.h b/game/network/network.impl.h new file mode 100644 index 0000000..ddb1ae5 --- /dev/null +++ b/game/network/network.impl.h @@ -0,0 +1,12 @@ +#include "network.h" +#include <gfx/gl/shader.h> +#include <gfx/models/texture.h> + +template<typename T> +void +NetworkOf<T>::render(const Shader & shader) const +{ + shader.setModel(Location {}, Shader::Program::StaticPos); + texture->Bind(); + links.apply(&T::render, shader); +} diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 8ec6893..133b80c 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -1,11 +1,10 @@ #include "rail.h" -#include "game/network/link.h" +#include "network.h" #include <GL/glew.h> #include <array> -#include <cache.h> #include <cassert> -#include <gfx/gl/shader.h> -#include <gfx/models/texture.h> +#include <game/network/link.h> +#include <game/network/network.impl.h> // IWYU pragma: keep #include <gfx/models/vertex.hpp> #include <glm/gtx/transform.hpp> #include <initializer_list> @@ -14,10 +13,12 @@ #include <stdexcept> #include <utility> +template class NetworkOf<RailLink>; + constexpr auto RAIL_CROSSSECTION_VERTICES {5U}; constexpr glm::vec3 RAIL_HEIGHT {0, .25F, 0}; -RailLinks::RailLinks() : texture {Texture::cachedTexture.get("rails.jpg")} { } +RailLinks::RailLinks() : NetworkOf<RailLink> {"rails.jpg"} { } void RailLinks::tick(TickDuration) { } void @@ -37,15 +38,6 @@ RailLinks::joinLinks(const LinkPtr & l) const } } -NodePtr -RailLinks::findNodeAt(glm::vec3 pos) const -{ - if (const auto n = nodes.find(std::make_shared<Node>(pos)); n != nodes.end()) { - return *n; - } - return {}; -} - std::shared_ptr<RailLink> RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) { @@ -111,14 +103,6 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) } void -RailLinks::render(const Shader & shader) const -{ - shader.setModel(Location {}, Shader::Program::StaticPos); - texture->Bind(); - links.apply(&RailLink::render, shader); -} - -void RailLink::defaultMesh() { for (auto n = RAIL_CROSSSECTION_VERTICES; n < vertices.size(); n += 1) { diff --git a/game/network/rail.h b/game/network/rail.h index 4460fcd..1d0ef68 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -7,17 +7,16 @@ #include "gfx/models/vertex.hpp" #include "gfx/renderable.h" #include "link.h" +#include "network.h" #include <glm/glm.hpp> #include <location.hpp> #include <maths.h> #include <memory> #include <set> -#include <sorting.hpp> #include <utility> #include <vector> class Shader; -class Texture; // A piece of rail track class RailLink : public Link, public Renderable { @@ -57,7 +56,7 @@ private: template<typename T> concept RailLinkConcept = std::is_base_of_v<RailLink, T>; -class RailLinks : public Renderable, public WorldObject { +class RailLinks : public NetworkOf<RailLink>, public WorldObject { public: RailLinks(); template<RailLinkConcept T, typename... Params> @@ -72,16 +71,10 @@ public: } std::shared_ptr<RailLink> addLinksBetween(glm::vec3 start, glm::vec3 end); - [[nodiscard]] NodePtr findNodeAt(glm::vec3) const; private: - using Nodes = std::set<NodePtr, PtrSorter<NodePtr>>; - Collection<RailLink> links; - Nodes nodes; - void render(const Shader &) const override; void tick(TickDuration elapsed) override; void joinLinks(const LinkPtr &) const; - std::shared_ptr<Texture> texture; }; #endif |