1 Star 0 Fork 0

小鱼儿小董子/dongli-zinx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
grid.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
小鱼儿小董子 提交于 2025-01-14 22:07 +08:00 . first commit
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wanjimao/dongli-zinx.git
git@gitee.com:wanjimao/dongli-zinx.git
wanjimao
dongli-zinx
dongli-zinx
v0.0.1

搜索帮助