-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAOI_world.cpp
94 lines (83 loc) · 2.6 KB
/
AOI_world.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
#include "AOI_world.h"
#include <iostream>
AOI_world::AOI_world(int _xbegin, int _xend, int _ybegin, int _yend, int _xcount, int _ycount):
x_begin(_xbegin),x_end(_xend),y_begin(_ybegin),y_end(_yend),x_count(_xcount),y_count(_ycount)
{
/*创建所有空网格*/
grid tmp;
for (int i = 0; i < x_count * y_count; i++)
{
m_grids.push_back(tmp);
}
}
AOI_world::~AOI_world()
{
}
std::list<AOI_Player*> AOI_world::GetSrdPlayers(AOI_Player * _player)
{
std::list<AOI_Player *> ret;
/*将周围玩家append到ret中*/
/*计算玩家所属网格*/
int gid = (_player->getX() - x_begin) / GetXWildth() + (_player->getY() - y_begin) / GetYWildth()*x_count;
/*计算该格子x轴方向数第几个,y轴方向数第几个*/
int x_index = gid % x_count;
int y_index = gid / x_count;
if (x_index > 0 && y_index > 0)
{
/*有左上角玩家,则将左上角格子所有玩家append到ret中*/
auto playerlist = m_grids[gid - 1 - x_count].GetList();
ret.insert(ret.end(), playerlist.begin(),playerlist.end());
}
if (y_index > 0)
{
auto playerlist = m_grids[gid - x_count].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
if (x_index <x_count-1 && y_index > 0)
{
auto playerlist = m_grids[gid - x_count + 1].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
if (x_index > 0)
{
auto playerlist = m_grids[gid - 1].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
auto playerlist = m_grids[gid].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
if ( x_index < x_count - 1)
{
auto playerlist = m_grids[gid + 1].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
if (x_index > 0 && y_index < y_count-1)
{
auto playerlist = m_grids[gid -1 + x_count].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
if (y_index < y_count - 1)
{
auto playerlist = m_grids[gid + x_count].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
if (x_index < x_count - 1 && y_index < y_count - 1)
{
auto playerlist = m_grids[gid + x_count + 1].GetList();
ret.insert(ret.end(), playerlist.begin(), playerlist.end());
}
return ret;
}
void AOI_world::AddPlayer(AOI_Player * _player)
{
/*计算玩家所属网格*/
int gid = (_player->getX() - x_begin) / GetXWildth() + (_player->getY() - y_begin) / GetYWildth()*x_count;
/*将玩家添加到该网格*/
m_grids[gid].AddPlayer(_player);
}
void AOI_world::DelPlayer(AOI_Player * _player)
{
/*计算玩家所属网格*/
int gid = (_player->getX() - x_begin) / GetXWildth() + (_player->getY() - y_begin) / GetYWildth()*x_count;
/*将玩家摘出该网格*/
m_grids[gid].DelPlayer(_player);
}