diff options
author | cflip <cflip@cflip.net> | 2022-02-28 19:10:09 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-02-28 19:10:09 -0700 |
commit | fe44274be041e125cd1be9bbfba26b2894ed51fd (patch) | |
tree | 83623b112f6166737a42a42779e74a7e1cac864e | |
parent | ed68168a1cd42bd593895eed055bf129217142d3 (diff) |
Quick and simple multi-car train implementation
-rw-r--r-- | src/main.cpp | 14 | ||||
-rw-r--r-- | src/train.cpp | 32 | ||||
-rw-r--r-- | src/train.h | 5 |
3 files changed, 43 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index c19d4e3..0dd2dea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,10 @@ int main(int argc, char** argv) Level level(32, 32, tiles); Bitmap bitmap(Width, Height); - Train car(level); + Train engine(level); + Train wagon(level); + + engine.addVehicle(&wagon); int xOffs = 0, yOffs = 0; int xDrag, yDrag; @@ -37,7 +40,7 @@ int main(int argc, char** argv) int mx = x / Scale + xOffs; int my = y / Scale + yOffs; auto pos = ScreenToTile({ mx, my }); - car.setPosition(pos.x, pos.y); + engine.setPosition(pos.x, pos.y); } }); @@ -63,8 +66,11 @@ int main(int argc, char** argv) bitmap.clear(0xff224466); level.draw(bitmap, xOffs, yOffs); - car.update(level); - car.draw(bitmap, xOffs, yOffs); + engine.update(level); + engine.draw(bitmap, xOffs, yOffs); + + wagon.update(level); + wagon.draw(bitmap, xOffs, yOffs); int xx = (hoveredTile.x - hoveredTile.y) * (TileSize / 2) - xOffs; int yy = (hoveredTile.x + hoveredTile.y) * (TileSize / 4) - yOffs; diff --git a/src/train.cpp b/src/train.cpp index 98a493f..2604898 100644 --- a/src/train.cpp +++ b/src/train.cpp @@ -5,15 +5,29 @@ void Train::update(Level& level) { - auto tile = level.get(x, y); - if (tile == 0) - return; + if (m_next) { + m_speed = m_next->m_speed; + m_progress = m_next->m_progress; + } else { + auto tile = level.get(x, y); + if (tile == 0) + return; + } if (m_progress < 1.f) { m_progress += m_speed; return; } + if (m_next) { + x = m_nextX; + y = m_nextY; + m_nextX = m_next->x; + m_nextY = m_next->y; + m_progress = 0.f; + findDirection(); + } + setPosition(m_nextX, m_nextY); } @@ -40,6 +54,16 @@ void Train::setPosition(int tx, int ty) findNextTile(); } +void Train::addVehicle(Train* newTrain) +{ + if (!m_prev) { + m_prev = newTrain; + newTrain->m_next = this; + } else { + m_prev->addVehicle(newTrain); + } +} + void Train::findDirection() { auto tile = m_level.get(x, y); @@ -81,4 +105,4 @@ void Train::findNextTile() m_nextX = x - 1; m_nextY = y; } -}
\ No newline at end of file +} diff --git a/src/train.h b/src/train.h index dc09889..9663150 100644 --- a/src/train.h +++ b/src/train.h @@ -14,11 +14,16 @@ public: void draw(Bitmap&, int, int); void setPosition(int x, int y); + // Add a vehicle to the end of this train + void addVehicle(Train*); private: void findDirection(); void findNextTile(); + Train* m_next { nullptr }; + Train* m_prev { nullptr }; + Bitmap m_sprite; Level& m_level; |