summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--car.pngbin0 -> 259 bytes
-rw-r--r--main.cpp11
-rw-r--r--train.cpp52
-rw-r--r--train.h27
4 files changed, 90 insertions, 0 deletions
diff --git a/car.png b/car.png
new file mode 100644
index 0000000..ae7c6ff
--- /dev/null
+++ b/car.png
Binary files differ
diff --git a/main.cpp b/main.cpp
index 9e3b621..610dbb1 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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
diff --git a/train.h b/train.h
new file mode 100644
index 0000000..249d92e
--- /dev/null
+++ b/train.h
@@ -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