From fe44274be041e125cd1be9bbfba26b2894ed51fd Mon Sep 17 00:00:00 2001 From: cflip Date: Mon, 28 Feb 2022 19:10:09 -0700 Subject: Quick and simple multi-car train implementation --- src/train.cpp | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/train.cpp') 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 +} -- cgit v1.2.3