summaryrefslogtreecommitdiff
path: root/src/level.cpp
diff options
context:
space:
mode:
authorJun Zhang <jun@junz.org>2022-01-30 12:30:02 +0800
committerGitHub <noreply@github.com>2022-01-29 21:30:02 -0700
commitcb7af3f4cac90f95926477b4001f9f80037568d5 (patch)
tree022f4700c796a9935acd3ee0d0fd80a812a78464 /src/level.cpp
parent99b4763f1028e72bb06d8db6c7e8ace469f8989c (diff)
refactor: adjust the project infra. (#1)
* refactor: adjust the project infra. This patch adds cmake build system to the project, and adjust infrastructure stuff. Signed-off-by: Jun Zhang <jun@junz.org> * fix: remove compiler flags that only exist in GCC. Signed-off-by: Jun Zhang <jun@junz.org>
Diffstat (limited to 'src/level.cpp')
-rw-r--r--src/level.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/level.cpp b/src/level.cpp
new file mode 100644
index 0000000..4df1d5a
--- /dev/null
+++ b/src/level.cpp
@@ -0,0 +1,54 @@
+#include "level.h"
+
+Level::Level(int width, int height)
+ : m_width(width), m_height(height)
+{
+ m_tiles = new uint8_t[width * height];
+ memset(m_tiles, 0, width * height);
+}
+
+uint8_t Level::get(int x, int y)
+{
+ if (inBounds(x, y)) {
+ return m_tiles[x + y * m_width];
+ } else {
+ return 0;
+ }
+}
+
+void Level::set(int x, int y, uint8_t tile)
+{
+ if (inBounds(x, y))
+ m_tiles[x + y * m_width] = tile;
+}
+
+RailDirection ChooseDirection(Level& level, int x, int y)
+{
+ if (!level.inBounds(x, y)) return NorthSouth;
+
+ bool n = level.get(x, y - 1) > 0;
+ bool e = level.get(x + 1, y) > 0;
+ bool s = level.get(x, y + 1) > 0;
+ bool w = level.get(x - 1, y) > 0;
+
+ if ((n || s) && !(e || w)) {
+ return NorthSouth;
+ }
+ if ((e || w) && !(n || s)) {
+ return EastWest;
+ }
+ else if (s && e) {
+ return SouthEast;
+ }
+ else if (s && w) {
+ return SouthWest;
+ }
+ else if (n && w) {
+ return NorthWest;
+ }
+ else if (n && e) {
+ return NorthEast;
+ }
+
+ return NorthSouth;
+} \ No newline at end of file