diff --git a/game/builtin/aoi2d.go b/game/builtin/aoi2d.go index 7482594..e7f66c7 100644 --- a/game/builtin/aoi2d.go +++ b/game/builtin/aoi2d.go @@ -133,9 +133,9 @@ func (slf *AOI2D) setAreaSize(width, height int) { slf.areaHeight = float64(height) slf.areaWidthLimit = int(math.Ceil(slf.width / slf.areaWidth)) slf.areaHeightLimit = int(math.Ceil(slf.height / slf.areaHeight)) - areas := make([][]map[int64]game.AOIEntity2D, slf.areaWidthLimit) + areas := make([][]map[int64]game.AOIEntity2D, slf.areaWidthLimit+1) for i := 0; i < len(areas); i++ { - entities := make([]map[int64]game.AOIEntity2D, slf.areaHeightLimit) + entities := make([]map[int64]game.AOIEntity2D, slf.areaHeightLimit+1) for e := 0; e < len(entities); e++ { entities[e] = map[int64]game.AOIEntity2D{} } @@ -161,7 +161,6 @@ func (slf *AOI2D) addEntity(entity game.AOIEntity2D) { heightArea := int(y / slf.areaHeight) guid := entity.GetGuid() slf.areas[widthArea][heightArea][guid] = entity - focus := map[int64]game.AOIEntity2D{} slf.focus[guid] = focus slf.rangeVisionAreaEntities(entity, func(eg int64, e game.AOIEntity2D) { @@ -207,14 +206,26 @@ func (slf *AOI2D) rangeVisionAreaEntities(entity game.AOIEntity2D, handle func(g } else if sw > slf.areaWidthLimit { sw = slf.areaWidthLimit } - for w := sw; w <= widthArea+widthSpan; w++ { + ew := widthArea - widthSpan + if ew < sw { + ew = sw + } else if ew > slf.areaWidthLimit { + ew = slf.areaWidthLimit + } + for w := sw; w < ew; w++ { sh := heightArea - heightSpan if sh < 0 { sh = 0 } else if sh > slf.areaHeightLimit { sh = slf.areaHeightLimit } - for h := sh; h <= heightArea+heightSpan; h++ { + eh := widthArea - widthSpan + if eh < sh { + eh = sh + } else if eh > slf.areaHeightLimit { + eh = slf.areaHeightLimit + } + for h := sh; h < eh; h++ { var areaX, areaY float64 if w < widthArea { tempW := w + 1 diff --git a/game/builtin/aoi2d_test.go b/game/builtin/aoi2d_test.go new file mode 100644 index 0000000..5952ee9 --- /dev/null +++ b/game/builtin/aoi2d_test.go @@ -0,0 +1,51 @@ +package builtin + +import ( + "fmt" + "github.com/kercylan98/minotaur/utils/random" + "testing" + "time" +) + +type Ent struct { + guid int64 + x, y, vision float64 +} + +func (slf *Ent) SetGuid(guid int64) { + slf.guid = guid +} + +func (slf *Ent) GetGuid() int64 { + return slf.guid +} + +func (slf *Ent) GetPosition() (x, y float64) { + return slf.x, slf.y +} + +func (slf *Ent) GetVision() float64 { + return slf.vision +} + +func TestNewAOI2D(t *testing.T) { + aoi := NewAOI2D(10000, 10000, 100, 100) + + start := time.Now() + for i := 0; i < 50000; i++ { + aoi.AddEntity(&Ent{ + guid: int64(i), + x: float64(random.Int(0, 10000)), + y: float64(random.Int(0, 10000)), + vision: 200, + }) + } + fmt.Println("添加耗时:", time.Since(start)) + + //start = time.Now() + //aoi.SetAreaSize(1000, 1000) + //fmt.Println("重设区域大小耗时:", time.Since(start)) + start = time.Now() + aoi.SetSize(10100, 10100) + fmt.Println("重设大小耗时:", time.Since(start)) +}