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 constexpr Position Map2World(const PointU& xy)
30 {
31 return Map2World(toPoint(xy));
32 }
33
37 constexpr Point World2Map(const Position& pos)
38 {
39 return toPoint(pos / TILE_SIZE);
40 }
41
42 constexpr Position GetSize() { return TILE_SIZE; }
43 };
44
46 template<Position TILE_SIZE>
48 {
49 public:
53 constexpr Position Map2World(const Point& xy)
54 {
55 return { (xy[1] + xy[0]) * TILE_SIZE[0] / 2.0_f, (xy[1] - xy[0]) * TILE_SIZE[1] / 2.0_f };
56 }
57
61 constexpr Point World2Map(const Position& pos)
62 {
63 Position xy_div = pos / TILE_SIZE;
64 return toPoint(Position{ xy_div[0] - xy_div[1] + 0.5_f, xy_div[0] + xy_div[1] - 0.5_f });
65 }
66
70 constexpr Position GetSizeBound(const Size& size)
71 {
72 constexpr Position TILE_SIZE_HALF = TILE_SIZE / 2.0_f;
73 unsigned sum = size[0] + size[1];
74 return TILE_SIZE_HALF * (Float)sum;
75 }
76 };
77
79 template<Position TILE_SIZE>
81 {
82 public:
86 constexpr Position Map2World(const Point& xy)
87 {
88 Position pos = { TILE_SIZE[0] * xy[0] , TILE_SIZE[1] / 2 * xy[1] };
89 if (xy[1] % 2 != 0)
90 {//奇数行向右偏移 w / 2
91 pos[0] += TILE_SIZE[0] / 2;
92 }
93 return pos;
94 }
95
99 constexpr Point World2Map(const Position& pos)
100 {
101 constexpr Position TILE_SIZE_HALF = TILE_SIZE / 2.0_f;
102 //四分之一矩形面积
103 constexpr Float s = Accumulate::Mul(TILE_SIZE_HALF);
104 //先计算矩形下标
105 Point p = toPoint(pos / TILE_SIZE);
106 //在矩形内坐标
107 Position p1 = pos - toPosition(p) * TILE_SIZE - TILE_SIZE_HALF;
108 //点围成矩形面积
109 Float sp = std::fabs(p1[0] * TILE_SIZE_HALF[1])
110 + std::fabs(p1[1] * TILE_SIZE_HALF[0]);
111 p[1] *= 2;
112 if (s < sp)
113 {
114 if (p1[0] > 0 && p1[1] > 0)
115 return p + Point{ 0, 1 };
116 else if (p1[0] < 0 && p1[1] > 0)
117 return p + Point{ -1, 1 };
118 else if (p1[0] < 0 && p1[1] < 0)
119 return p + Point{ -1, -1 };
120 else if (p1[0] > 0 && p1[1] < 0)
121 return p + Point{ 0, -1 };
122 else
123 return p;
124 }
125 else
126 {
127 return p;
128 }
129 }
130 };
131}
平菱形瓦片地图
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 Map2World(const PointU &xy)
constexpr Position GetSize()
constexpr Position Map2World(const Point &xy)
地图坐标 -> 世界坐标
constexpr Point World2Map(const Position &pos)
世界坐标 -> 地图坐标
constexpr auto Mul(const C &container)
求积product
瓦片地图
Position2 Position
constexpr Array< Position::value_type, N > toPosition(const Array< T, N > &a)
转换到Position,已知长度
float Float
Point2U PointU
constexpr Array< Point::value_type, N > toPoint(const Array< T, N > &a)
转换到Point,已知长度
PointU Size
Point2 Point