diff options
author | cflip <cflip@cflip.net> | 2022-02-27 10:08:02 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-02-27 10:11:55 -0700 |
commit | 5aba1f0f337679467c18cddbcbd2ed3f378a1229 (patch) | |
tree | 924e389fbe3fa683563168bf98adab33e8c5b77b | |
parent | 000fb3cf7d5df09f9c89f619a870aecf28b82ce2 (diff) |
Make train movement smoother
-rw-r--r-- | src/main.cpp | 5 | ||||
-rw-r--r-- | src/train.cpp | 33 | ||||
-rw-r--r-- | src/train.h | 15 |
3 files changed, 39 insertions, 14 deletions
diff --git a/src/main.cpp b/src/main.cpp index b1942c9..4fa5e47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,7 +85,7 @@ int main(int argc, char **argv) Level level(32, 32); Bitmap bitmap(Width, Height); - TrainCar car(0, 0); + TrainCar car; int xOffs = 0, yOffs = 0; int xDrag, yDrag; @@ -123,8 +123,7 @@ int main(int argc, char **argv) float mx = x / Scale + xOffs; float my = y / Scale + yOffs; auto pos = screenToTile({ mx, my }); - car.x = pos.x; - car.y = pos.y; + car.setPosition(pos.x, pos.y); } }); diff --git a/src/train.cpp b/src/train.cpp index e369887..046c580 100644 --- a/src/train.cpp +++ b/src/train.cpp @@ -9,13 +9,12 @@ void TrainCar::update(Level& level) if (tile == 0) return; - if (m_progress < 12) { - m_progress++; + if (m_progress < 1.f) { + m_progress += m_speed; return; } - else { - m_progress = 0; - } + + m_progress = 0.f; auto n = level.get(x, y - 1); auto e = level.get(x + 1, y); @@ -42,11 +41,31 @@ void TrainCar::update(Level& level) if (w == SouthEast) m_dir = South; x--; } + + nextTile(); } void TrainCar::draw(Bitmap& bitmap, int xo, int yo) { - int xx = (x - y) * (24 / 2) - xo; - int yy = (x + y) * (24 / 4) - yo; + float xi = ((float)x + (float)(m_nextX - x) * m_progress) * 24; + float yi = ((float)y + (float)(m_nextY - y) * m_progress) * 24; + int xx = (int)((xi - yi) / 2.f - (float)xo); + int yy = (int)((xi + yi) / 4.f - (float)yo); bitmap.blit(m_sprite, xx, yy, 0, 0, 24, 24); +} + +void TrainCar::setPosition(int tx, int ty) +{ + x = tx; + y = ty; + m_progress = 0.f; + nextTile(); +} + +void TrainCar::nextTile() +{ + if (m_dir == North) { m_nextX = x; m_nextY = y - 1; } + if (m_dir == East) { m_nextX = x + 1; m_nextY = y; } + if (m_dir == South) { m_nextX = x; m_nextY = y + 1; } + if (m_dir == West) { m_nextX = x - 1; m_nextY = y; } }
\ No newline at end of file diff --git a/src/train.h b/src/train.h index 342c787..05b0da5 100644 --- a/src/train.h +++ b/src/train.h @@ -13,15 +13,22 @@ class Level; class TrainCar { public: - TrainCar(int x, int y) - : x(x), y(y), m_sprite("../res/car.png") {} + TrainCar() + : m_sprite("../res/car.png") {} void update(Level&); void draw(Bitmap&, int, int); - int x, y; + void setPosition(int x, int y); private: + void nextTile(); + Bitmap m_sprite; - int m_progress{ 0 }; + + int x{0}, y{0}; + float m_speed{0.05f}; + float m_progress{ 0.f }; + CarDirection m_dir{ North }; + int m_nextX{0}, m_nextY{0}; }; |