// Required for the Spatial interface from rtreego func (b *Bug) Bounds() *rtreego.Rect { r, _ := rtreego.NewRect( rtreego.Point{float64(b.xpos), float64(b.ypos)}, []float64{float64(b.width), float64(b.height)}, ) return r }
func (s *State) PlaceNewBug(b *Bug, nearPosition Positioner) (ok bool) { desiredRect, _ := rtreego.NewRect(positionerToRtreePoint(nearPosition), []float64{float64(b.W()), float64(b.H())}) x, y, ok := closestEmptyPosition(s, desiredRect) if !ok { return false } s.BugList.PushBack(b) b.xpos = x b.ypos = y s.BugGrid[x][y] = b s.BugTree.Insert(b) return true }
func closestEmptyPosition(s *State, r *rtreego.Rect) (nx int, ny int, ok bool) { x := int(r.PointCoord(0)) y := int(r.PointCoord(1)) w := int(r.LengthsCoord(0)) h := int(r.LengthsCoord(1)) // These loops try to place the creature at distances at least far enough away to // avoid intersecting it for dx := -w; dx <= w; dx += w { for dy := -h; dy <= h; dy += h { nx, ny := restrictToGrid(s, x+dx, y+dy, w, h) restrictedRect, _ := rtreego.NewRect(rtreego.Point{float64(nx), float64(ny)}, []float64{r.LengthsCoord(0), float64(r.LengthsCoord(1))}) if !hasIntersections(s.BugTree, restrictedRect) { return nx, ny, true } } } return -1, -1, false }
func nativeCoordsToRtreeRect(x, y, w, h int) *rtreego.Rect { r, _ := rtreego.NewRect(rtreego.Point{float64(x), float64(y)}, []float64{float64(w), float64(h)}) return r }