AOI兴趣点
在游戏开发中,客户端的角色往往有很多,有些角色并不用显示出来,只要显示当前角色周围的其它角色就行,角色的信息都是存储在服务器中的,所以服务器需要计算客户端角色周围的其它角色,然后通知给客户端
AOI格子类型
- 格子需要自己的ID
- 最大最下XY坐标
- 当前格子中的所有角色的ID
- 以及用来读写的锁
- 对外提供Add添加玩家ID的方法,Remove移除玩家ID的方法,以及获取当前格子中全部ID的方法
type Grid struct {
GID int
MaxX int
MaxY int
MinX int
MinY int
playerIDs map[int]bool
playerIDLock sync.RWMutex
}
func (g *Grid) Add(playerID int) {
g.playerIDLock.Lock()
defer g.playerIDLock.Unlock()
g.playerIDs[playerID] = true
}
func (g *Grid) Remove(playerID int) {
g.playerIDLock.Lock()
defer g.playerIDLock.Unlock()
delete(g.playerIDs, playerID)
}
func (g *Grid) GetPlayerIDs() (playerIDs []int) {
g.playerIDLock.RLock()
defer g.playerIDLock.RUnlock()
for key, _ := range g.playerIDs {
playerIDs = append(playerIDs, key)
}
return
}
func (g *Grid) String() string {
return fmt.Sprintf("Grid ID:%d, MaxX:%d, MaxY:%d, Minx:%d, MinY:%d ,PlayerIDs:%v",
g.GID, g.MaxX, g.MaxY, g.MinX, g.MinY, g.playerIDs)
}
func NewGrid(gid, maxX, maxY, minX, minY int) *Grid {
return &Grid{
GID: gid,
MaxX: maxX,
MaxY: maxY,
MinX: minX,
MinY: minY,
playerIDs: make(map[int]bool),
}
}
AOI区域管理模块
- 用来管理服务器中的所有格子的模块
- 需要最大最小XY的坐标,XY轴方向的格子数量,以及所有的格子
- 以及提供各种对外操作格子中的玩家ID的方法
type AOIManager struct {
MinX int
MaxX int
CountX int
MinY int
MaxY int
CountY int
Grids map[int]*Grid
}
func (am *AOIManager) getGridX() int {
return (am.MaxX - am.MinX) / am.CountX
}
func (am *AOIManager) getGridY() int {
return (am.MaxY - am.MinY) / am.CountY
}
func (am *AOIManager) String() string {
s := fmt.Sprintf("AOIManager:\n MinX:%d ,MaxX:%d ,CountX:%d ,MinY:%d ,MaxY:%d ,CountY:%d\n",
am.MinX, am.MaxX, am.CountX, am.MinY, am.MaxY, am.CountY)
for _, val := range am.Grids {
s += fmt.Sprintln(val)
}
return s
}
func NewAOIManager(minX, maxX, countX, minY, maxY, countY int) *AOIManager {
aoiMgr := &AOIManager{
MinX: minX,
MaxX: maxX,
CountX: countX,
MinY: minY,
MaxY: maxY,
CountY: countY,
Grids: make(map[int]*Grid),
}
for i := 0; i < countY; i++ {
for j := 0; j < countX; j++ {
gid := j + i*countY
aoiMgr.Grids[gid] = NewGrid(gid,
aoiMgr.MinX+(j+1)*aoiMgr.getGridX(),
aoiMgr.MinY+(i+1)*aoiMgr.getGridY(),
aoiMgr.MinX+j*aoiMgr.getGridX(),
aoiMgr.MinY+i*aoiMgr.getGridY())
}
}
return aoiMgr
}
// GetSurroundGridsByGID 通过格子编号获取该格子周围的格子
func (am *AOIManager) GetSurroundGridsByGID(gid int) (grids []*Grid) {
if _, ok := am.Grids[gid]; !ok {
return
}
grids = append(grids, am.Grids[gid])
idx := gid % am.CountX
if idx > 0 {
grids = append(grids, am.Grids[gid-1])
}
if idx < am.CountX-1 {
grids = append(grids, am.Grids[gid+1])
}
gIDsX := make([]int, 0)
for _, val := range grids {
gIDsX = append(gIDsX, val.GID)
}
for _, val := range gIDsX {
idy := val / am.CountY
if idy > 0 {
grids = append(grids, am.Grids[val-am.CountX])
}
if idy < am.CountY-1 {
grids = append(grids, am.Grids[val+am.CountX])
}
}
return
}
// GetSurroundPlayersByPos 通过位置获取周围的玩家
func (am *AOIManager) GetSurroundPlayersByPos(x, y float32) (playerIDs []int) {
//根据位置坐标先获取所在的格子
gid := am.GetGridIDByPos(x, y)
surroundGrids := am.GetSurroundGridsByGID(gid)
for i := 0; i < len(surroundGrids); i++ {
grid := surroundGrids[i]
playerIDs = append(playerIDs, grid.GetPlayerIDs()...)
}
return
}
// GetGridIDByPos 通过位置获取当前位置所在的格子
func (am *AOIManager) GetGridIDByPos(x, y float32) int {
idx := (int(x) - am.MinX) / am.getGridX()
idy := (int(y) - am.MinY) / am.getGridY()
return idy*am.CountX + idx
}
// AddPlayerIDToGridByGridID 添加一个PlayerID到一个格子中通过格子ID
func (am *AOIManager) AddPlayerIDToGridByGridID(playerID, gridID int) {
val, ok := am.Grids[gridID]
if !ok {
fmt.Println("PlayerID Not In Grids Add Error: PlayerID:", playerID, " GridID:", gridID)
return
}
val.Add(gridID)
}
// AddPlayerIDToGridByPos 添加一个PlayerID到一个格子中通过坐标
func (am *AOIManager) AddPlayerIDToGridByPos(playerID int, x, y float32) {
gID := am.GetGridIDByPos(x, y)
am.AddPlayerIDToGridByGridID(playerID, gID)
}
// RemovePlayerIDFormGridByGridID 移除一个格子中的PlayerID通过格子ID
func (am *AOIManager) RemovePlayerIDFormGridByGridID(playerID, gridID int) {
val, ok := am.Grids[gridID]
if !ok {
fmt.Println("PlayerID Not In Grids Remove Error: PlayerID:", playerID, " GridID:", gridID)
return
}
val.Remove(playerID)
}
// RemovePlayerIDFormGridByPos 移除一个格子中的PlayerID通过坐标
func (am *AOIManager) RemovePlayerIDFormGridByPos(playerID int, x, y float32) {
gID := am.GetGridIDByPos(x, y)
am.RemovePlayerIDFormGridByGridID(playerID, gID)
}
// GetGridAllPlayerIDs 通过格子ID获取格子中全部的PlayerID
func (am *AOIManager) GetGridAllPlayerIDs(gridID int) []int {
val, ok := am.Grids[gridID]
if !ok {
fmt.Println("PlayerID Not In Grids Remove Error:,", " GridID:", gridID)
return nil
}
return val.GetPlayerIDs()
}
AOI测试
func TestAOI(t *testing.T) {
aoiMgr := NewAOIManager(0, 250, 5, 0, 250, 5)
fmt.Println(aoiMgr)
}
func TestAOI1(t *testing.T) {
am := NewAOIManager(0, 250, 5, 0, 250, 5)
for _, grid := range am.Grids {
grids := am.GetSurroundGridsByGID(grid.GID)
slice := make([]int, 0, len(grids))
for i := 0; i < len(grids); i++ {
slice = append(slice, grids[i].GID)
}
fmt.Println(slice)
}
}