diff options
author | cflip <cflip@cflip.net> | 2022-02-27 19:37:36 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-02-27 19:37:36 -0700 |
commit | 8c8096dab1538592e6bacea6ba4e1f9882ae6e0b (patch) | |
tree | 35a69e588af9ace66a5b8c0211e05167ea40428b /src/level.cpp | |
parent | bba66a0ccb7281cd6c717fe9784a04a4fcf72700 (diff) |
Miscellaneous code cleanup
Diffstat (limited to 'src/level.cpp')
-rw-r--r-- | src/level.cpp | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/level.cpp b/src/level.cpp index b42608b..1e71799 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -1,7 +1,8 @@ #include "level.h" +#include "util.h" Level::Level(int width, int height) - : m_width(width), m_height(height) + : m_width(width), m_height(height), m_tileSprites("../res/tiles.png") { m_tiles = new uint8_t[width * height]; memset(m_tiles, 0, width * height); @@ -22,6 +23,67 @@ void Level::set(int x, int y, uint8_t tile) m_tiles[x + y * m_width] = tile; } +void Level::draw(Bitmap& bitmap, int xo, int yo) +{ + for (int y = 0; y < 32; ++y) { + for (int x = 0; x < 32; ++x) { + auto tile = get(x, y); + + int xx = (x - y) * (TileSize / 2) - xo; + int yy = (x + y) * (TileSize / 4) - yo; + + int tx = (x + y) % 2; + int ty = 0; + + if (tile == NorthSouth) { + tx = 0; + ty = 2; + } else if (tile == EastWest) { + tx = 1; + ty = 2; + } else if (tile == SouthEast) { + tx = 0; + ty = 3; + } else if (tile == SouthWest) { + tx = 3; + ty = 3; + } else if (tile == NorthWest) { + tx = 1; + ty = 3; + } else if (tile == NorthEast) { + tx = 2; + ty = 3; + } + + bitmap.blit(m_tileSprites, xx, yy, tx * 24, ty * 24, TileSize, TileSize); + } + } +} + +void Level::toggleTile(int x, int y) +{ + static const auto updateDirection = [&](int xt, int yt) { + if (get(xt, yt) > 0) + set(xt, yt, ChooseDirection(*this, xt, yt)); + }; + + if (inBounds(x, y)) { + auto tile = get(x, y); + + if (tile > 0) + tile = 0; + else + tile = ChooseDirection(*this, x, y); + + m_tiles[x + y * m_width] = tile; + + updateDirection(x - 1, y); + updateDirection(x + 1, y); + updateDirection(x, y - 1); + updateDirection(x, y + 1); + } +} + RailDirection ChooseDirection(Level& level, int x, int y) { if (!level.inBounds(x, y)) return NorthSouth; |