From 0d99c20d0fafcf2b86d9d33430659de76b030395 Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 24 Mar 2022 08:45:00 -0600 Subject: Add acceleration/deceleration to the train --- src/main.cpp | 1 + src/train.cpp | 3 ++- src/train.h | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 436660c..28b2281 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,7 @@ int main(int argc, char** argv) int my = y / Scale + yOffs; auto pos = ScreenToTile({ mx, my }); engine.setPosition(pos.x, pos.y); + engine.setSpeed(0.2f); } }); diff --git a/src/train.cpp b/src/train.cpp index 7ce1c2d..d3ea7fb 100644 --- a/src/train.cpp +++ b/src/train.cpp @@ -14,6 +14,7 @@ void Train::update() return; } + m_speed *= m_acceleration; if (m_progress < 1.f) { m_progress += m_speed; return; @@ -48,8 +49,8 @@ void Train::setPosition(int tx, int ty) { x = tx; y = ty; - m_progress = 0.f; + findDirection(); findNextTile(); } diff --git a/src/train.h b/src/train.h index 3cd23e4..178583f 100644 --- a/src/train.h +++ b/src/train.h @@ -14,6 +14,7 @@ public: void draw(Bitmap&, int, int); void setPosition(int x, int y); + void setSpeed(float speed) { m_speed = speed; }; // Add a vehicle to the end of this train void addVehicle(Train*); @@ -28,7 +29,8 @@ private: Level& m_level; int x { 0 }, y { 0 }; - float m_speed { 0.05f }; + float m_speed { 0.f }; + float m_acceleration { 0.98f }; float m_progress { 0.f }; Direction m_dir { North }; -- cgit v1.2.3