-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.h
139 lines (135 loc) · 3.97 KB
/
map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef MAP_H
#define MAP_H
class Node;
#include "node.h"
#include "noderect.h"
#include <cstddef>
#include <iosfwd>
#include <vector>
struct Point
{
int x;
int y;
};
static const Point coords[] = {
{-1,-1},
{-1,0},
{-1,1},
{0,-1},
//{0,0},自己,不需要迭代了
{0,1},
{1,-1},
{1,0},
{1,1},
};
class NeighbourIterator
{
public:
NeighbourIterator(const NodeRect& rect, const NodeSavingF* startNode)
:m_coordIdx(0),m_rect(rect),m_startNode(startNode) {
findNext();//保证刚开始调用的是正确的点
}
const NeighbourIterator& operator++(){//operator++必须保证找到的就是一个可以使用的邻居。
++m_coordIdx;
findNext();
return *this;
}
NodeSavingF* operator*() const{
const int coordsSize = sizeof(coords) / sizeof(Point);
NodeSavingF* ret = getNeighbour(coordsSize, coords);
return ret;
}
protected:
int m_coordIdx;
const NodeRect& m_rect;
const NodeSavingF* m_startNode;
NodeSavingF* getNeighbour(const int num,const Point* coords) const{
if(m_coordIdx >= num)
{
return NULL;
}
const int x = m_startNode->getX() + coords[m_coordIdx].x;
const int y = m_startNode->getY() + coords[m_coordIdx].y;
return m_rect.getNode(x, y);
}
void findNext(){
const int coordsSize = sizeof(coords) / sizeof(Point);
Node* ret = getNeighbour(coordsSize, coords);
while(ret == NULL && m_coordIdx < coordsSize){//m_coordIdx < coordsSize这句话,应该可以提前到外面来。
++m_coordIdx;
ret = getNeighbour(coordsSize, coords);
}
}
};
class JpsNeighbourIterator : public NeighbourIterator
{
JpsNeighbourIterator(const NodeRect& rect, const NodeSavingF* startNode)
: NeighbourIterator(rect, startNode){}
const NeighbourIterator& operator++(){
++m_coordIdx;
const int coordsSize = sizeof(coords) / sizeof(Point);
NodeSavingF* ret = getNeighbour(coordsSize, coords);
while(ret == NULL && m_coordIdx < coordsSize){
++m_coordIdx;
ret = getNeighbour(coordsSize, coords);
}
return *this;
}
};
//class OthogonalNeighbourIterator : public NeighbourIterator
//{
//public:
// OthogonalNeighbourIterator(const NodeRect& rect, const NodeSavingF* startNode)
// :NeighbourIterator(rect, startNode){}
// NodeSavingF* operator*() const{
// static const Point coords[] = {
// {-1,0},
// {0,-1},
// //{0,0},自己,不需要迭代了
// {0,1},
// {1,0},
// };
// return getNeighbour(sizeof(coords) / sizeof(Point), coords);
// }
//};
//class DiagonalNeighbourIterator : public NeighbourIterator
//{
//public:
// DiagonalNeighbourIterator(const NodeRect& rect, const NodeSavingF* startNode)
// :NeighbourIterator(rect, startNode){}
// NodeSavingF* operator*() const{
// static const Point coords[] = {
// {-1,-1},
// {-1,1},
// //{0,0},自己,不需要迭代了
// {1,-1},
// {1,1},
// };
// return getNeighbour(sizeof(coords) / sizeof(Point), coords);
// }
//};
class Map
{
public:
Map();
void setMap(const char* mapString);
void getNeighbours();
NodeSavingF* getNode(int x, int y) const{return m_nodes.getNode(x, y);}
NeighbourIterator begin(const NodeSavingF* node) const{
return NeighbourIterator(m_nodes, node);
}
/*DiagonalNeighbourIterator beginDiagonal(const Node *node) const{
return DiagonalNeighbourIterator(m_nodes, node);
}
OthogonalNeighbourIterator beginOthogonal(const Node *node) const{
return OthogonalNeighbourIterator(m_nodes, node);
}*/
void dumpMap(std::ofstream& os, const std::vector<const NodeSavingF*>& path);
bool isWalkableAt(const int x, const int y) const{
const NodeSavingF* node = m_nodes.getNode(x,y);
return node && !node->isBlock();
}
protected:
NodeRect m_nodes;
};
#endif // MAP_H