summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-03-06 13:43:05 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-03-06 13:43:16 +0000
commite255ef23c762b0d0588e274b910850abd9a7d2c8 (patch)
tree2f623e19b0729776c4e104d200ea3e89aea735ee /game
parentFixing manual camera scrolling, now just a matrix multiplication (diff)
downloadilt-e255ef23c762b0d0588e274b910850abd9a7d2c8.tar.bz2
ilt-e255ef23c762b0d0588e274b910850abd9a7d2c8.tar.xz
ilt-e255ef23c762b0d0588e274b910850abd9a7d2c8.zip
Introduce the rail vehicle class for the definition of the vehicle
Not an instance of the vehicle.
Diffstat (limited to 'game')
-rw-r--r--game/vehicles/railloco.cpp66
-rw-r--r--game/vehicles/railloco.h29
2 files changed, 53 insertions, 42 deletions
diff --git a/game/vehicles/railloco.cpp b/game/vehicles/railloco.cpp
index de47ebb..7763147 100644
--- a/game/vehicles/railloco.cpp
+++ b/game/vehicles/railloco.cpp
@@ -19,18 +19,24 @@
#include <vector>
void
-RailVehicle::render(const Shader & shader) const
+RailVehicleClass::render(const Shader & shader, const Location & location, const std::array<Location, 2> & bl) const
{
texture->Bind();
- for (const auto & bogie : bogies) {
- shader.setModel(bogie.location);
- bogie.mesh->Draw();
+ for (auto b = 0U; b < bogies.size(); ++b) {
+ shader.setModel(bl[b]);
+ bogies[b]->Draw();
}
shader.setModel(location);
bodyMesh->Draw();
}
void
+RailVehicle::render(const Shader & shader) const
+{
+ rvClass->render(shader, location, bogies);
+}
+
+void
RailLoco::move(TickDuration dur)
{
static std::mt19937 gen(std::random_device {}());
@@ -65,9 +71,9 @@ RailLoco::getBogiePosition(float linkDist, float dist) const
void
RailLoco::updateRailVehiclePosition(RailVehicle * w, float trailBy) const
{
- const auto overhang {(w->length - w->wheelBase) / 2};
- const auto & b1Pos = w->bogies[0].location = getBogiePosition(linkDist, trailBy += overhang);
- const auto & b2Pos = w->bogies[1].location = getBogiePosition(linkDist, trailBy + wheelBase);
+ const auto overhang {(w->rvClass->length - w->rvClass->wheelBase) / 2};
+ const auto & b1Pos = w->bogies[0] = getBogiePosition(linkDist, trailBy += overhang);
+ const auto & b2Pos = w->bogies[1] = getBogiePosition(linkDist, trailBy + rvClass->wheelBase);
const auto diff = glm::normalize(b2Pos.pos - b1Pos.pos);
w->location.pos = (b1Pos.pos + b2Pos.pos) / 2.F;
w->location.rot = {-vector_pitch(diff), vector_yaw(diff), 0};
@@ -85,11 +91,11 @@ void
RailLoco::updateWagons() const
{
// Drag wagons
- float trailBy {length};
+ float trailBy {rvClass->length};
for (const auto & wagon : wagons) {
const auto w {wagon.lock()};
updateRailVehiclePosition(w.get(), trailBy);
- trailBy += w->length;
+ trailBy += w->rvClass->length;
}
}
@@ -113,31 +119,29 @@ bogieOffset(ObjParser & o)
}
}
-Brush47::Brush47(const LinkPtr & l) : RailLoco(l, 0)
+class Brush47Class : public RailVehicleClass {
+public:
+ Brush47Class()
+ {
+ ObjParser o {Resource::mapPath("brush47.obj")};
+ bogieOffset(o);
+ const auto m = o.createMeshes();
+ bodyMesh = m.at("Body");
+ bogies[0] = m.at("Bogie1");
+ bogies[1] = m.at("Bogie2");
+ texture = Texture::cachedTexture.get(Resource::mapPath("brush47.png"));
+ wheelBase = 12.F;
+ length = 20.F;
+ }
+};
+
+Brush47::Brush47(const LinkPtr & l) : RailLoco(std::make_shared<Brush47Class>(), l, 0)
{
- ObjParser o {Resource::mapPath("brush47.obj")};
- bogieOffset(o);
- const auto m = o.createMeshes();
- bodyMesh = m.at("Body");
- bogies[0].mesh = m.at("Bogie1");
- bogies[1].mesh = m.at("Bogie2");
- texture = Texture::cachedTexture.get(Resource::mapPath("brush47.png"));
- wheelBase = 12.F;
- length = 20.F;
speed = 33.6F;
- linkDist = wheelBase;
+ linkDist = rvClass->wheelBase;
}
-Brush47Wagon::Brush47Wagon(const LinkPtr & l) : RailWagon(l, 0)
+Brush47Wagon::Brush47Wagon(const LinkPtr & l) : RailWagon(std::make_shared<Brush47Class>(), l, 0)
{
- ObjParser o {Resource::mapPath("brush47.obj")};
- bogieOffset(o);
- const auto m = o.createMeshes();
- bodyMesh = m.at("Body");
- bogies[0].mesh = m.at("Bogie1");
- bogies[1].mesh = m.at("Bogie2");
- texture = Texture::cachedTexture.get(Resource::mapPath("brush47.png"));
- wheelBase = 12.F;
- length = 20.F;
- linkDist = wheelBase;
+ linkDist = rvClass->wheelBase;
}
diff --git a/game/vehicles/railloco.h b/game/vehicles/railloco.h
index f08de05..88f9503 100644
--- a/game/vehicles/railloco.h
+++ b/game/vehicles/railloco.h
@@ -5,26 +5,33 @@
#include <array>
#include <location.hpp>
#include <memory>
+#include <utility>
#include <vector>
class Shader;
-
class Texture;
-class RailVehicle : public Vehicle {
-public:
- struct Bogie {
- Location location;
- MeshPtr mesh;
- };
- using Vehicle::Vehicle;
- void render(const Shader & shader) const override;
-
- std::array<Bogie, 2> bogies;
+class RailVehicleClass {
+public:
+ void render(const Shader &, const Location &, const std::array<Location, 2> &) const;
+ std::array<MeshPtr, 2> bogies;
MeshPtr bodyMesh;
std::shared_ptr<Texture> texture;
float wheelBase;
float length;
+};
+using RailVehicleClassPtr = std::shared_ptr<RailVehicleClass>;
+
+class RailVehicle : public Vehicle {
+public:
+ explicit RailVehicle(RailVehicleClassPtr rvc, const LinkPtr & link, float linkDist = 0) :
+ Vehicle {link, linkDist}, rvClass {std::move(rvc)}
+ {
+ }
+ void render(const Shader & shader) const override;
+
+ RailVehicleClassPtr rvClass;
+ std::array<Location, 2> bogies;
friend class RailLoco;
};