dl
dl_tilemap.h
浏览该文件的文档.
1
12#pragma once
13
15namespace dl::TileMap
16{
18 template<Position TILE_SIZE>
20 {
21 public:
25 constexpr Position Map2World(const Point& xy)
26 {
27 return toPosition(xy) * TILE_SIZE;
28 }
29
33 constexpr Point World2Map(const Position& pos)
34 {
35 return toPoint(pos / TILE_SIZE);
36 }
37
38 constexpr Position GetSize() { return TILE_SIZE; }
39 };
40
42 template<Position TILE_SIZE>
44 {
45 public:
49 constexpr Position Map2World(const Point& xy)
50 {
51 return { (xy[1] + xy[0]) * TILE_SIZE[0] / 2.0, (xy[1] - xy[0]) * TILE_SIZE[1] / 2.0 };
52 }
53
57 constexpr Point World2Map(const Position& pos)
58 {
59 Position xy_div = pos / TILE_SIZE;
60 return toPoint(Position{ xy_div[0] - xy_div[1] + 0.5, xy_div[0] + xy_div[1] - 0.5 });
61 }
62
66 constexpr Position GetSizeBound(const Size& size)
67 {
68 constexpr Position TILE_SIZE_HALF = TILE_SIZE / 2.0;
69 unsigned sum = size[0] + size[1];
70 return TILE_SIZE_HALF * (Float)sum;
71 }
72 };
73
75 template<Position TILE_SIZE>
77 {
78 public:
82 constexpr Position Map2World(const Point& xy)
83 {
84 Position pos = { TILE_SIZE[0] * xy[0] , TILE_SIZE[1] / 2 * xy[1] };
85 if (xy[1] % 2 != 0)
86 {//奇数行向右偏移 w / 2
87 pos[0] += TILE_SIZE[0] / 2;
88 }
89 return pos;
90 }
91
95 constexpr Point World2Map(const Position& pos)
96 {
97 constexpr Position TILE_SIZE_HALF = TILE_SIZE / 2.0;
98 //四分之一矩形面积
99 constexpr Float s = Accumulate::Mul(TILE_SIZE_HALF);
100 //先计算矩形下标
101 Point p = toPoint(pos / TILE_SIZE);
102 //在矩形内坐标
103 Position p1 = pos - toPosition(p) * TILE_SIZE - TILE_SIZE_HALF;
104 //点围成矩形面积
105 Float sp = std::fabs(p1[0] * TILE_SIZE_HALF[1])
106 + std::fabs(p1[1] * TILE_SIZE_HALF[0]);
107 p[1] *= 2;
108 if (s < sp)
109 {
110 if (p1[0] > 0 && p1[1] > 0)
111 return p + Point{ 0, 1 };
112 else if (p1[0] < 0 && p1[1] > 0)
113 return p + Point{ -1, 1 };
114 else if (p1[0] < 0 && p1[1] < 0)
115 return p + Point{ -1, -1 };
116 else if (p1[0] > 0 && p1[1] < 0)
117 return p + Point{ 0, -1 };
118 else
119 return p;
120 }
121 else
122 {
123 return p;
124 }
125 }
126 };
127}
平菱形瓦片地图
constexpr Point World2Map(const Position &pos)
世界坐标 -> 地图坐标
constexpr Position Map2World(const Point &xy)
地图坐标 -> 世界坐标
斜菱形瓦片地图
constexpr Position GetSizeBound(const Size &size)
返回包围盒大小,传入地图格子数量
constexpr Position Map2World(const Point &xy)
地图坐标 -> 世界坐标
constexpr Point World2Map(const Position &pos)
世界坐标 -> 地图坐标
矩形瓦片地图
constexpr Position GetSize()
constexpr Position Map2World(const Point &xy)
地图坐标 -> 世界坐标
constexpr Point World2Map(const Position &pos)
世界坐标 -> 地图坐标
constexpr auto Mul(const C &container)
求积product
瓦片地图
Position2 Position
float Float
constexpr std::array< Point::value_type, N > toPoint(const std::array< T, N > &a)
转换到Point,已知长度
constexpr std::array< Position::value_type, N > toPosition(const std::array< T, N > &a)
转换到Position,已知长度
PointU Size
Point2 Point