diff options
-rw-r--r-- | car.png | bin | 0 -> 259 bytes | |||
-rw-r--r-- | level.h | 10 | ||||
-rw-r--r-- | main.cpp | 12 | ||||
-rw-r--r-- | train.cpp | 52 | ||||
-rw-r--r-- | train.h | 27 |
5 files changed, 94 insertions, 7 deletions
Binary files differ @@ -18,11 +18,11 @@ private: enum RailDirection { NorthSouth = 1, - EastWest = 2, - SouthEast = 3, - SouthWest = 4, - NorthWest = 5, - NorthEast = 6, + EastWest, + SouthEast, + SouthWest, + NorthWest, + NorthEast, }; RailDirection ChooseDirection(Level& level, int x, int y);
\ No newline at end of file @@ -5,6 +5,7 @@ #include "windows.h" #include "bitmap.h" #include "level.h" +#include "train.h" #include "iostream" #pragma comment(lib, "winmm.lib") // For audio files @@ -89,6 +90,7 @@ int main(int argc, char **argv) Level level(32, 32); Bitmap bitmap(Width, Height); + TrainCar car(0, 0); //bool audio_play1 = PlaySound(TEXT("res\\Starliner.wav"), NULL, SND_LOOP | SND_ASYNC); //Adds Starliner.wav and plays it on startup and loops, @@ -98,8 +100,6 @@ int main(int argc, char **argv) int xDrag, yDrag; bool isDragging = false; - - window.onMouseDown([&](int button, int x, int y) { if (button == 1) { static const auto update_direction = [&](int xt, int yt) { @@ -128,6 +128,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; } }); @@ -148,6 +154,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 |