Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

obstacle.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package botserv
  2. import (
  3. "math"
  4. "math/rand"
  5. v "bitbucket.org/hackerbots/vector"
  6. )
  7. type Obstacle struct {
  8. Bounds v.AABB2d `json:"bounds"`
  9. Hp int `json:"-"`
  10. }
  11. func (o Obstacle) distance_from_point(p v.Point2d) float32 {
  12. dist := math.MaxFloat32
  13. dist = math.Min(dist, float64(p.Sub(o.Bounds.A).Mag()))
  14. dist = math.Min(dist, float64(p.Sub(o.Bounds.B).Mag()))
  15. dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.A.X, o.Bounds.B.Y}).Mag()))
  16. dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.B.X, o.Bounds.A.Y}).Mag()))
  17. return float32(dist)
  18. }
  19. func (o Obstacle) minify() [4]int {
  20. out := [4]int{int(o.Bounds.A.X), int(o.Bounds.A.Y), int(o.Bounds.B.X), int(o.Bounds.B.Y)}
  21. return out
  22. }
  23. func MinifyObstacles(o []Obstacle) [][4]int {
  24. out := [][4]int{}
  25. for i := range o {
  26. out = append(out, o[i].minify())
  27. }
  28. return out
  29. }
  30. func GenerateObstacles(count int, width, height float32) []Obstacle {
  31. out := []Obstacle{}
  32. for i := 0; i < count; i++ {
  33. x := rand.Float32() * (width - (width / 10))
  34. y := rand.Float32() * (height - (height / 10))
  35. w := rand.Float32() * (width / 10)
  36. h := rand.Float32() * (height / 10)
  37. out = append(
  38. out,
  39. Obstacle{
  40. Bounds: v.AABB2d{
  41. A: v.Point2d{X: x, Y: y},
  42. B: v.Point2d{X: 20 + x + w, Y: 20 + y + h},
  43. },
  44. Hp: 10000,
  45. })
  46. }
  47. return out
  48. }