diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/network/link.h | 1 | ||||
-rw-r--r-- | game/vehicles/railloco.cpp | 18 | ||||
-rw-r--r-- | game/vehicles/vehicle.cpp | 30 | ||||
-rw-r--r-- | game/vehicles/vehicle.h | 17 |
4 files changed, 53 insertions, 13 deletions
diff --git a/game/network/link.h b/game/network/link.h index 207ecd0..4968eba 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -28,6 +28,7 @@ using NodePtr = std::shared_ptr<Node>; // it joins 2 nodes class Link; using LinkPtr = std::shared_ptr<Link>; +using LinkCPtr = std::shared_ptr<const Link>; using LinkWPtr = std::weak_ptr<const Link>; class Link { public: diff --git a/game/vehicles/railloco.cpp b/game/vehicles/railloco.cpp index d06474c..146d2e0 100644 --- a/game/vehicles/railloco.cpp +++ b/game/vehicles/railloco.cpp @@ -12,25 +12,23 @@ void RailLoco::tick(TickDuration dur) { linkDist += dur.count() * speed; - auto curLink {link.lock()}; - while (linkDist > curLink->length) { - location = curLink->positionAt(curLink->length, linkDir); - const auto & nexts {curLink->nexts[1 - linkDir]}; + auto curLink {linkHist.getAt(0.F)}; + while (linkDist > curLink.first->length) { + location = curLink.first->positionAt(curLink.first->length, curLink.second); + const auto & nexts {curLink.first->nexts[1 - curLink.second]}; const auto next = std::find_if(nexts.begin(), nexts.end(), [ang = location.GetRot().y](const Link::Next & n) { return std::abs(normalize(n.first.lock()->ends[n.second].second - ang)) < 0.1F; }); if (next != nexts.end()) { - linkDist -= curLink->length; - link = next->first; - curLink = link.lock(); - linkDir = next->second; + linkDist -= curLink.first->length; + curLink = linkHist.add(next->first, next->second); } else { - linkDist = curLink->length; + linkDist = curLink.first->length; speed = 0; } } - location = curLink->positionAt(linkDist, linkDir); + location = curLink.first->positionAt(linkDist, curLink.second); } Brush47::Brush47(const LinkPtr & l) : RailLoco(l, "brush47.obj", "brush47.png") { } diff --git a/game/vehicles/vehicle.cpp b/game/vehicles/vehicle.cpp index c12181f..569063d 100644 --- a/game/vehicles/vehicle.cpp +++ b/game/vehicles/vehicle.cpp @@ -5,6 +5,34 @@ #include <utility> Vehicle::Vehicle(const LinkPtr & l, const std::string & obj, const std::string & tex) : - Physical(l->ends.front().first->pos, obj, tex), link(l) + Physical(l->ends.front().first->pos, obj, tex) { + linkHist.add(l, 0); +} + +LinkHistory::Return +LinkHistory::add(const LinkWPtr & l, unsigned char d) +{ + links.insert(links.begin(), {l, d}); + const auto lp = l.lock(); + totalLen += lp->length; + while (totalLen >= 1000.F && !links.empty()) { + totalLen -= links.back().first.lock()->length; + links.pop_back(); + } + return {lp, d}; +} + +LinkHistory::Return +LinkHistory::getAt(float len) const +{ + auto litr = links.begin(); + while (len > 0.F && litr != links.end()) { + len -= litr->first.lock()->length; + litr++; + } + if (litr == links.end()) { + litr--; + } + return {litr->first.lock(), litr->second}; } diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index d59588c..f7defb3 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -6,13 +6,26 @@ #include <game/worldobject.h> #include <string> +class LinkHistory { +public: + using Entry = std::pair<LinkWPtr, unsigned char /*dir*/>; + using Return = std::pair<LinkCPtr, unsigned char /*dir*/>; + Return add(const LinkWPtr &, unsigned char); + Return getAt(float) const; + +private: + std::vector<Entry> links; + float totalLen; +}; + class Vehicle : public WorldObject, public Physical { public: Vehicle(const LinkPtr & link, const std::string & obj, const std::string & tex); - 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) + +protected: + LinkHistory linkHist; }; #endif |