1 Star 0 Fork 0

nggs/go-aoi

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
XZListAOIManager.go 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
seis 提交于 2017-12-04 22:48 +08:00 . add Data to aoi
package aoi
const _DEFAULT_AOI_DISTANCE = 100
type xzaoi struct {
aoi *AOI
neighbors map[*xzaoi]struct{}
xPrev, xNext *xzaoi
yPrev, yNext *xzaoi
markVal int
}
// XZListAOIManager is an implementation of AOICalculator using XZ lists
type XZListAOIManager struct {
xSweepList *xAOIList
zSweepList *yAOIList
}
func NewXZListAOICalculator() AOIManager {
return &XZListAOIManager{
xSweepList: newXAOIList(),
zSweepList: newYAOIList(),
}
}
// Enter is called when Entity enters Space
func (aoiman *XZListAOIManager) Enter(aoi *AOI, x, y Coord) {
xzaoi := &xzaoi{
aoi: aoi,
neighbors: map[*xzaoi]struct{}{},
}
aoi.x, aoi.y = x, y
aoi.implData = xzaoi
aoiman.xSweepList.Insert(xzaoi)
aoiman.zSweepList.Insert(xzaoi)
aoiman.adjust(xzaoi)
}
// Leave is called when Entity leaves Space
func (aoiman *XZListAOIManager) Leave(aoi *AOI) {
xzaoi := aoi.implData.(*xzaoi)
aoiman.xSweepList.Remove(xzaoi)
aoiman.zSweepList.Remove(xzaoi)
}
// Moved is called when Entity moves in Space
func (aoiman *XZListAOIManager) Moved(aoi *AOI, x, y Coord) {
oldX := aoi.x
oldY := aoi.y
aoi.x, aoi.y = x, y
xzaoi := aoi.implData.(*xzaoi)
if oldX != x {
aoiman.xSweepList.Move(xzaoi, oldX)
}
if oldY != y {
aoiman.zSweepList.Move(xzaoi, oldY)
}
aoiman.adjust(xzaoi)
}
// adjust is called by Entity to adjust neighbors
func (aoiman *XZListAOIManager) adjust(aoi *xzaoi) {
aoiman.xSweepList.Mark(aoi)
aoiman.zSweepList.Mark(aoi)
// AOI marked twice are neighbors
for neighbor := range aoi.neighbors {
if neighbor.markVal == 2 {
// neighbors kept
neighbor.markVal = -2 // mark this as neighbor
} else { // markVal < 2
// was neighbor, but not any more
delete(aoi.neighbors, neighbor)
aoi.aoi.callback.OnLeaveAOI(neighbor.aoi)
delete(neighbor.neighbors, aoi)
neighbor.aoi.callback.OnLeaveAOI(aoi.aoi)
}
}
// travel in X list again to find all new neighbors, whose markVal == 2
aoiman.xSweepList.GetClearMarkedNeighbors(aoi)
// travel in Z list again to unmark all
aoiman.zSweepList.ClearMark(aoi)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/nggs/aoi.git
git@gitee.com:nggs/aoi.git
nggs
aoi
go-aoi
v0.0.2

搜索帮助