server/obstacle.go
Stephen McQuay c8efd34080 ignore new binary
Create new bserv binary

Herein lie the following ideas:

- change package visibility of many things
- try to stash the globals into structs
- the code is far from correct; it merely compiles
2014-03-31 23:21:14 -07:00

58 lines
1.3 KiB
Go

package botserv
import (
"math"
"math/rand"
v "bitbucket.org/hackerbots/vector"
)
type Obstacle struct {
Bounds v.AABB2d `json:"bounds"`
Hp int `json:"-"`
}
func (o Obstacle) distance_from_point(p v.Point2d) float32 {
dist := math.MaxFloat32
dist = math.Min(dist, float64(p.Sub(o.Bounds.A).Mag()))
dist = math.Min(dist, float64(p.Sub(o.Bounds.B).Mag()))
dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.A.X, o.Bounds.B.Y}).Mag()))
dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.B.X, o.Bounds.A.Y}).Mag()))
return float32(dist)
}
func (o Obstacle) minify() [4]int {
out := [4]int{int(o.Bounds.A.X), int(o.Bounds.A.Y), int(o.Bounds.B.X), int(o.Bounds.B.Y)}
return out
}
func MinifyObstacles(o []Obstacle) [][4]int {
out := [][4]int{}
for i := range o {
out = append(out, o[i].minify())
}
return out
}
func GenerateObstacles(count int, width, height float32) []Obstacle {
out := []Obstacle{}
for i := 0; i < count; i++ {
x := rand.Float32() * (width - (width / 10))
y := rand.Float32() * (height - (height / 10))
w := rand.Float32() * (width / 10)
h := rand.Float32() * (height / 10)
out = append(
out,
Obstacle{
Bounds: v.AABB2d{
A: v.Point2d{X: x, Y: y},
B: v.Point2d{X: 20 + x + w, Y: 20 + y + h},
},
Hp: 10000,
})
}
return out
}