代码拉取完成,页面将自动刷新
package core
import (
"fmt"
"sync"
)
// GrID A grid class in a map 一个地图中的格子类
type GrID struct {
GID int // Grid ID
MinX int // Left boundary coordinate of the grid
MaxX int // Right boundary coordinate of the grid
MinY int // Upper boundary coordinate of the grid
MaxY int // Lower boundary coordinate of the grid
playerIDs map[int]bool // IDs of players or objects in the current grid
pIDLock sync.RWMutex // Lock for protecting the playerIDs map
}
// NewGrID Initialize a grid
func NewGrID(gID, minX, maxX, minY, maxY int) *GrID {
return &GrID{
GID: gID,
MinX: minX,
MaxX: maxX,
MinY: minY,
MaxY: maxY,
playerIDs: make(map[int]bool),
}
}
// Add a player to the current grid
func (g *GrID) Add(playerID int) {
g.pIDLock.Lock()
defer g.pIDLock.Unlock()
g.playerIDs[playerID] = true
}
// Remove a player from the grid
func (g *GrID) Remove(playerID int) {
g.pIDLock.Lock()
defer g.pIDLock.Unlock()
delete(g.playerIDs, playerID)
}
// GetPlyerIDs Get all players in the current grid
func (g *GrID) GetPlyerIDs() (playerIDs []int) {
g.pIDLock.RLock()
defer g.pIDLock.RUnlock()
for k := range g.playerIDs {
playerIDs = append(playerIDs, k)
}
return
}
// String Print information method
func (g *GrID) String() string {
return fmt.Sprintf("GrID ID: %d, minX:%d, maxX:%d, minY:%d, maxY:%d, playerIDs:%v",
g.GID, g.MinX, g.MaxX, g.MinY, g.MaxY, g.playerIDs)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。