From c08e33649931b679b17488ba6f7dab1d628213fd Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 14 Feb 2021 14:19:21 +0000 Subject: Add our first vehicle --- game/vehicles/railloco.cpp | 30 ++++++++++++++++++++++++++++++ game/vehicles/railloco.h | 14 ++++++++++++++ game/vehicles/vehicle.cpp | 6 ++++++ game/vehicles/vehicle.h | 19 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 game/vehicles/railloco.cpp create mode 100644 game/vehicles/railloco.h create mode 100644 game/vehicles/vehicle.cpp create mode 100644 game/vehicles/vehicle.h (limited to 'game/vehicles') diff --git a/game/vehicles/railloco.cpp b/game/vehicles/railloco.cpp new file mode 100644 index 0000000..baa9b81 --- /dev/null +++ b/game/vehicles/railloco.cpp @@ -0,0 +1,30 @@ +#include "railloco.h" +#include "game/vehicles/vehicle.h" +#include "gfx/gl/transform.h" +#include +#include + +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]}; + 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; + }); + if (next != nexts.end()) { + linkDist -= link->length; + link = next->first; + linkDir = next->second; + } + else { + linkDist = link->length; + speed = 0; + } + } + location = link->positionAt(linkDist, linkDir); +} + +Brush47::Brush47(LinkPtr l) : RailLoco(l, "brush47.obj", "brush47.png") { } diff --git a/game/vehicles/railloco.h b/game/vehicles/railloco.h new file mode 100644 index 0000000..05e3e48 --- /dev/null +++ b/game/vehicles/railloco.h @@ -0,0 +1,14 @@ +#include "game/worldobject.h" +#include "vehicle.h" +#include + +class RailLoco : public Vehicle { +public: + using Vehicle::Vehicle; + void tick(TickDuration elapsed) override; +}; + +class Brush47 : public RailLoco { +public: + Brush47(LinkPtr p); +}; diff --git a/game/vehicles/vehicle.cpp b/game/vehicles/vehicle.cpp new file mode 100644 index 0000000..4c4ac71 --- /dev/null +++ b/game/vehicles/vehicle.cpp @@ -0,0 +1,6 @@ +#include "vehicle.h" + +Vehicle::Vehicle(LinkPtr l, const std::string & obj, const std::string & tex) : + Physical(l->ends.front().first->pos, obj, tex), link(l) +{ +} diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h new file mode 100644 index 0000000..01be6a6 --- /dev/null +++ b/game/vehicles/vehicle.h @@ -0,0 +1,19 @@ +#ifndef VEHICLE_H +#define VEHICLE_H + +#include "game/physical.h" +#include +#include +#include +#include + +class Vehicle : public WorldObject, public Physical { +public: + Vehicle(LinkPtr link, const std::string & obj, const std::string & tex); + LinkPtr 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) +}; + +#endif -- cgit v1.2.3