From 08f6e874a8930899714e2c0d313a9ed9f4eec35c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 14 Feb 2021 14:52:04 +0000 Subject: Link weak_ptrs --- game/network/link.h | 3 ++- game/network/rail.cpp | 4 ++-- game/vehicles/railloco.cpp | 16 +++++++++------- game/vehicles/vehicle.h | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) (limited to 'game') diff --git a/game/network/link.h b/game/network/link.h index d4ae4c6..207ecd0 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -28,6 +28,7 @@ using NodePtr = std::shared_ptr; // it joins 2 nodes class Link; using LinkPtr = std::shared_ptr; +using LinkWPtr = std::weak_ptr; class Link { public: using End = std::pair; @@ -41,7 +42,7 @@ public: std::array ends; float length; - using Next = std::pair; + using Next = std::pair; std::array, 2> nexts; }; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 1f3cc01..10dfb88 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -24,8 +24,8 @@ RailLinks::joinLinks(const LinkPtr & l) const for (const auto oe : {0, 1}) { for (const auto te : {0, 1}) { if (l->ends[te].first == ol->ends[oe].first) { - l->nexts[te].emplace_back(ol.get(), oe); - ol->nexts[oe].emplace_back(l.get(), te); + l->nexts[te].emplace_back(ol, oe); + ol->nexts[oe].emplace_back(l, te); } } } diff --git a/game/vehicles/railloco.cpp b/game/vehicles/railloco.cpp index 54582c2..d06474c 100644 --- a/game/vehicles/railloco.cpp +++ b/game/vehicles/railloco.cpp @@ -12,23 +12,25 @@ void RailLoco::tick(TickDuration dur) { linkDist += dur.count() * speed; - while (linkDist > link->length) { - location = link->positionAt(link->length, linkDir); - const auto & nexts {link->nexts[1 - linkDir]}; + auto curLink {link.lock()}; + while (linkDist > curLink->length) { + location = curLink->positionAt(curLink->length, linkDir); + const auto & nexts {curLink->nexts[1 - linkDir]}; const auto next = std::find_if(nexts.begin(), nexts.end(), [ang = location.GetRot().y](const Link::Next & n) { - return std::abs(normalize(n.first->ends[n.second].second - ang)) < 0.1F; + return std::abs(normalize(n.first.lock()->ends[n.second].second - ang)) < 0.1F; }); if (next != nexts.end()) { - linkDist -= link->length; + linkDist -= curLink->length; link = next->first; + curLink = link.lock(); linkDir = next->second; } else { - linkDist = link->length; + linkDist = curLink->length; speed = 0; } } - location = link->positionAt(linkDist, linkDir); + location = curLink->positionAt(linkDist, linkDir); } Brush47::Brush47(const LinkPtr & l) : RailLoco(l, "brush47.obj", "brush47.png") { } diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index cd2249d..d59588c 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -9,7 +9,7 @@ class Vehicle : public WorldObject, public Physical { public: Vehicle(const LinkPtr & link, const std::string & obj, const std::string & tex); - LinkPtr link; // Which link are we travelling along + LinkWPtr link; // Which link are we travelling along unsigned char linkDir {0}; // Starting end e0->e1 or e1->e0 float linkDist {0}; // distance long current link float speed {50}; // speed in m/s (~75 km/h) -- cgit v1.2.3