-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAOIWorld.cpp
More file actions
96 lines (89 loc) · 3.3 KB
/
Copy pathAOIWorld.cpp
File metadata and controls
96 lines (89 loc) · 3.3 KB
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
#include "AOIWorld.h"
AOIWorld::AOIWorld(int _xBegin, int _xEnd, int _yBegin, int _yEnd, int _xCount, int _yCount)
:xBegin(_xBegin), xEnd(_xEnd), yBegin(_yBegin), yEnd(_yEnd), xCount(_xCount), yCount(_yCount)
{
//x轴网格宽度=(x周结束坐标-x轴起始坐标)/x网格数量
xWidth = (xEnd - xBegin) / xCount;
yWidth = (yEnd - yBegin) / yCount;
//创建格子容器
for (int i = 0; i < xCount * yCount; i++) {
Grid tmp;
mGridsVector.push_back(tmp);
}
}
AOIWorld::~AOIWorld()
{
}
std::list<Player*> AOIWorld::GetSurroundPlayers(Player* _player)
{
std::list<Player*> listRet;
//计算网格编号
int gridID = (_player->GetX() - xBegin) / xWidth + (_player->GetY() - yBegin) / yWidth * xCount;
//x的索引:从左往右数的第几个-1
int xIndex = gridID % xCount;
//y的索引:从上往下数的第几个-1
int yIndex = gridID / xCount;
//各个方向上的格子依次判断
//左上角格子
if (xIndex > 0 && yIndex > 0) {
//把左上角格子里的玩家列表插入到listRet并返回
std::list<Player*> curPlayerList = mGridsVector[gridID - 1 - xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//正上方格子
if (yIndex > 0) {
//把正上方格子里的玩家列表插入到listRet并返回
std::list<Player*>& curPlayerList = mGridsVector[gridID - xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//右上方格子
if (xIndex < xCount - 1 && yIndex > 0) {
std::list<Player*>& curPlayerList = mGridsVector[gridID + 1 - xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//正左边格子
if (xIndex > 0) {
std::list<Player*>& curPlayerList = mGridsVector[gridID - 1].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//自己的格子(自己也是周围玩家)
std::list<Player*>& curPlayerList = mGridsVector[gridID].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
//正右边格子
if (xIndex < xCount - 1) {
std::list<Player*>& curPlayerList = mGridsVector[gridID + 1].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//左下角格子
if (xIndex > 0 && yIndex < yCount - 1) {
std::list<Player*>& curPlayerList = mGridsVector[gridID - 1 + xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//正下方格子
if (yIndex < yCount - 1) {
std::list<Player*>& curPlayerList = mGridsVector[gridID + xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
//右下角格子
if (xIndex < xCount - 1 && yIndex < yCount - 1) {
std::list<Player*>& curPlayerList = mGridsVector[gridID + 1 + xCount].mPlayersList;
listRet.insert(listRet.begin(), curPlayerList.begin(), curPlayerList.end());
}
return listRet;
}
bool AOIWorld::AddPlayer(Player* _player)
{
//网格编号=(x-x轴起始坐标)/x轴网格宽度 + (y-y轴起始坐标)/y轴宽度*x轴网格数量
//计算网格编号
int gridID = (_player->GetX() - xBegin) / xWidth + (_player->GetY() - yBegin) / yWidth * xCount;
//添加到网格中
mGridsVector[gridID].mPlayersList.push_back(_player);
return true;
}
void AOIWorld::DelPlayer(Player* _player)
{
//计算网格编号
int gridID = (_player->GetX() - xBegin) / xWidth + (_player->GetY() - yBegin) / yWidth * xCount;
//摘除玩家
mGridsVector[gridID].mPlayersList.remove(_player);
}