-
Notifications
You must be signed in to change notification settings - Fork 0
/
AOIWorld.cpp
95 lines (84 loc) · 2.81 KB
/
AOIWorld.cpp
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
#include "AOIWorld.h"
using namespace std;
AOIWorld::AOIWorld(int _x_begin, int _x_end, int _y_begin,
int _y_end, int _x_count, int _y_count)
:x_begin(_x_begin), x_end(_x_end), y_begin(_y_begin),
y_end(_y_end),x_count(_x_count), y_count(_y_count)
{
x_width = (x_end - x_begin) / x_count;
y_width = (y_end - y_begin) / y_count;
for (int i = 0; i < x_count * y_count; i++)
{
Grid temp_grid;
m_grids.push_back(temp_grid);
}
}
std::list<Player*> AOIWorld::GetRoundPlayer(Player* player)
{
std::list<Player*>Round_p;
//计算当前网格横着数和纵着数的个数
int grid_id = (player->getX() - x_begin) / x_width
+ (player->getY() - y_begin) / y_width * x_count;
int x_index = grid_id % x_count;
int y_index = grid_id / x_count;
if (x_index > 0 && y_index > 0)
{
list<Player*>& cur_list = m_grids[grid_id - 1 - x_count].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (y_index > 0)
{
list<Player*>& cur_list = m_grids[grid_id - x_count].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (x_index < x_count - 1 && y_index > 0)
{
list<Player*>& cur_list = m_grids[grid_id - x_count + 1].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (x_index > 0)
{
list<Player*>& cur_list = m_grids[grid_id - 1].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
//自己
list<Player*>& cur_list = m_grids[grid_id].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
if (x_index < x_count - 1)
{
list<Player*>& cur_list = m_grids[grid_id + 1].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (x_index > 0 && y_index < y_count - 1)
{
list<Player*>& cur_list = m_grids[grid_id - 1 + x_count].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (y_index < y_count - 1)
{
list<Player*>& cur_list = m_grids[grid_id + x_count].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
if (x_index < x_count - 1 && y_index < y_count - 1)
{
list<Player*>& cur_list = m_grids[grid_id + 1 + x_count].m_player;
Round_p.insert(Round_p.begin(), cur_list.begin(), cur_list.end());
}
return Round_p;
}
bool AOIWorld::AddPlayerS(Player* player)
{
/*计算所属网格号*/
//网格编号=(x-x轴起始坐标)/x轴网格宽度
//+(y-y轴起始坐标)/y轴宽度*x轴网格数量
int grid_id = (player->getX() - x_begin) / x_width
+ (player->getY() - y_begin) / y_width * x_count;
this->m_grids[grid_id].m_player.push_back(player);
return true;
}
void AOIWorld::DelPlayers(Player* player)
{
int grid_id = (player->getX() - x_begin) / x_width
+ (player->getY() - y_begin) / y_width * x_count;
this->m_grids[grid_id].m_player.remove(player);
}