Skip to content

Instantly share code, notes, and snippets.

@sknjpn
Created July 3, 2018 14:32
Show Gist options
  • Save sknjpn/64332987b7921768ade1ac8679c4ad3c to your computer and use it in GitHub Desktop.
Save sknjpn/64332987b7921768ade1ac8679c4ad3c to your computer and use it in GitHub Desktop.
#pragma once
# include <Siv3D.hpp>
class TileMap;
class Tile
{
friend TileMap;
Point m_position;
Tile* m_left = nullptr;
Tile* m_up = nullptr;
Tile* m_right = nullptr;
Tile* m_down = nullptr;
Array<Tile*> m_adjacentTiles;
public:
Tile() = default;
const Array<Tile*>& getAdjacentTiles() const noexcept { return m_adjacentTiles; }
Tile& getLeft() const noexcept { return *m_left; }
Tile& getUp() const noexcept { return *m_up; }
Tile& getRight() const noexcept { return *m_right; }
Tile& getDown() const noexcept { return *m_down; }
};
class TileMap
{
Grid<Tile> m_tiles;
public:
TileMap(int w, int h)
: m_tiles(w, h)
{
for (auto p : step(Size(w, h)))
{
auto& t = m_tiles[p.y][p.x];
if (p.x != 0) { t.m_left = &m_tiles[p.y][p.x - 1]; t.m_adjacentTiles.emplace_back(t.m_left); }
if (p.y != 0) { t.m_up = &m_tiles[p.y - 1][p.x]; t.m_adjacentTiles.emplace_back(t.m_up); }
if (p.x != w - 1) { t.m_right = &m_tiles[p.y][p.x + 1]; t.m_adjacentTiles.emplace_back(t.m_right); }
if (p.y != h - 1) { t.m_down = &m_tiles[p.y + 1][p.x]; t.m_adjacentTiles.emplace_back(t.m_down); }
}
}
TileMap(const Size& size)
: TileMap(size.x, size.y)
{}
const Tile& getTile(const Point& position) const { return m_tiles[position.y][position.x]; }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment