diff options
| -rw-r--r-- | car.png | bin | 0 -> 259 bytes | |||
| -rw-r--r-- | main.cpp | 11 | ||||
| -rw-r--r-- | train.cpp | 52 | ||||
| -rw-r--r-- | train.h | 27 | 
4 files changed, 90 insertions, 0 deletions
| Binary files differ @@ -4,6 +4,7 @@  #include "window.h"  #include "bitmap.h"  #include "level.h" +#include "train.h"  struct Point2D {  	float x, y; @@ -89,6 +90,8 @@ int main(int argc, char **argv)  	int xDrag, yDrag;  	bool isDragging = false; +	TrainCar car(0, 0); +  	window.onMouseDown([&](int button, int x, int y) {  		if (button == 1) {  			static const auto update_direction = [&](int xt, int yt) { @@ -117,6 +120,12 @@ int main(int argc, char **argv)  			xDrag = x;  			yDrag = y;  			isDragging = true; +		} else { +			float mx = x / Scale + xOffs; +			float my = y / Scale + yOffs; +			auto pos = screenToTile({ mx, my }); +			car.x = pos.x; +			car.y = pos.y;  		}  	}); @@ -137,6 +146,8 @@ int main(int argc, char **argv)  	while (!window.shouldClose()) {  		window.update();  		DrawLevel(bitmap, tiles, level, xOffs, yOffs); +		car.update(level); +		car.draw(bitmap, xOffs, yOffs);  		window.draw(bitmap);  	} diff --git a/train.cpp b/train.cpp new file mode 100644 index 0000000..e369887 --- /dev/null +++ b/train.cpp @@ -0,0 +1,52 @@ +#include "train.h" + +#include "level.h" +#include "bitmap.h" + +void TrainCar::update(Level& level) +{ +	auto tile = level.get(x, y); +	if (tile == 0) +		return; + +	if (m_progress < 12) { +		m_progress++; +		return; +	} +	else { +		m_progress = 0; +	} + +	auto n = level.get(x, y - 1); +	auto e = level.get(x + 1, y); +	auto s = level.get(x, y + 1); +	auto w = level.get(x - 1, y); + +	if (m_dir == North) { +		if (n == SouthEast) m_dir = East; +		if (n == SouthWest) m_dir = West; +		y--; +	} +	else if (m_dir == East) { +		if (e == NorthWest) m_dir = North; +		if (e == SouthWest) m_dir = South; +		x++; +	} +	else if (m_dir == South) { +		if (s == NorthEast) m_dir = East; +		if (s == NorthWest) m_dir = West; +		y++; +	} +	else if (m_dir == West) { +		if (w == NorthEast) m_dir = North; +		if (w == SouthEast) m_dir = South; +		x--; +	} +} + +void TrainCar::draw(Bitmap& bitmap, int xo, int yo) +{ +	int xx = (x - y) * (24 / 2) - xo; +	int yy = (x + y) * (24 / 4) - yo; +	bitmap.blit(m_sprite, xx, yy, 0, 0, 24, 24); +}
\ No newline at end of file @@ -0,0 +1,27 @@ +#pragma once + +#include "bitmap.h" + +enum CarDirection { +	North, +	East, +	South, +	West +}; + +class Level; + +class TrainCar { +public: +	TrainCar(int x, int y)  +		: x(x), y(y), m_sprite("car.png") {} + +	void update(Level&); +	void draw(Bitmap&, int, int); + +	int x, y; +private: +	Bitmap m_sprite; +	int m_progress; +	CarDirection m_dir; +};
\ No newline at end of file | 
