From 1e7a924874050674a103f6927f4a3c5ef6459222 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 13 Jun 2025 21:53:32 +0100 Subject: Create a list of snap points for the existing network These form key positions in the world to ease neat creation. --- game/network/network.h | 14 ++++++++++++++ game/network/rail.cpp | 16 ++++++++++++++++ game/network/rail.h | 1 + test/test-network.cpp | 6 ++++++ ui/editNetwork.cpp | 15 ++++++++++----- ui/editNetwork.h | 1 + 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/game/network/network.h b/game/network/network.h index d3370b3..7bfd2ce 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -35,6 +35,19 @@ struct CreationDefinition { CreationDefinitionEnd toEnd; }; +struct SnapPoint : CreationDefinitionEnd { + template + requires std::is_constructible_v + explicit SnapPoint(GlobalPosition3D snapPosition, CDE &&... cde) : + CreationDefinitionEnd {std::forward(cde)...}, snapPosition(snapPosition) + { + } + + GlobalPosition3D snapPosition; +}; + +using SnapPoints = std::vector; + class Network { public: using LinkEnd = std::pair; @@ -55,6 +68,7 @@ public: [[nodiscard]] static Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &); [[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0; + [[nodiscard]] virtual SnapPoints getSnapPoints() const = 0; [[nodiscard]] Link::Collection create(const GeoData *, const CreationDefinition &); [[nodiscard]] Link::Collection createChain(const GeoData *, std::span); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 342a2ad..45d55d6 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -137,3 +137,19 @@ RailLinks::getBaseWidth() const static constexpr auto BASE_WIDTH = 5'700; return BASE_WIDTH; } + +SnapPoints +RailLinks::getSnapPoints() const +{ + static constexpr auto EXTENSION_SNAP_DIST = 1'200.F; + SnapPoints out; + for (const auto & link : links) { + for (const auto & end : link->ends) { + // Link end node; directionless, suitable crossings + out.emplace_back(end.node->pos, end.node->pos, std::nullopt); + // Link end; with direction, suitable for continuing/joining + out.emplace_back(end.node->pos - ((sincos(end.dir) * EXTENSION_SNAP_DIST) || 0.F), end.node->pos, end.dir); + } + } + return out; +} diff --git a/game/network/rail.h b/game/network/rail.h index 15b9ae4..48b1410 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -78,6 +78,7 @@ public: [[nodiscard]] const Surface * getBaseSurface() const override; [[nodiscard]] RelativeDistance getBaseWidth() const override; + [[nodiscard]] SnapPoints getSnapPoints() const override; private: void tick(TickDuration elapsed) override; diff --git a/test/test-network.cpp b/test/test-network.cpp index 9a9ceb3..c3617b2 100644 --- a/test/test-network.cpp +++ b/test/test-network.cpp @@ -97,6 +97,12 @@ namespace { { return 5'700; } + + SnapPoints + getSnapPoints() const override + { + return {}; + } }; struct TestNetwork : public EmptyNetwork { diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index 98a60d3..20ec877 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -5,7 +5,7 @@ #include #include -EditNetwork::EditNetwork(Network * n) : network {n} { } +EditNetwork::EditNetwork(Network * n) : network {n}, snapPoints {network->getSnapPoints()} { } bool EditNetwork::click(const SDL_MouseButtonEvent & event, const Ray & ray) @@ -34,6 +34,7 @@ EditNetwork::click(const SDL_MouseButtonEvent & event, const RaygetSnapPoints(); } } return true; @@ -63,16 +64,20 @@ EditNetwork::handleInput(const SDL_Event &) void EditNetwork::render(const SceneShader &, const Frustum &) const { + // TODO render snapPoints } std::optional EditNetwork::resolveRay(const Ray & ray) const { if (const auto position = gameState->terrain->intersectRay(ray)) { - const auto node = network->intersectRayNodes(ray); - if (node) { - const auto direction = network->findNodeDirection(node); - return CreationDefinitionEnd {.position = node->pos, .direction = direction}; + if (const auto closestSnapPoint = std::ranges::min_element(snapPoints, {}, + [position](auto && snapPoint) { + return distance(position->first, snapPoint.snapPosition); + }); + closestSnapPoint != snapPoints.end() + && distance(position->first, closestSnapPoint->snapPosition) < 1'200.F) { + return *closestSnapPoint; } return CreationDefinitionEnd {.position = position->first, .direction = std::nullopt}; } diff --git a/ui/editNetwork.h b/ui/editNetwork.h index e155105..8011c1e 100644 --- a/ui/editNetwork.h +++ b/ui/editNetwork.h @@ -23,6 +23,7 @@ private: Network * network; bool continuousMode {false}; + SnapPoints snapPoints; std::optional currentStart; Link::Collection candidates; }; -- cgit v1.2.3