You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

63 lines
1.6 KiB

  1. package server
  2. import (
  3. "math"
  4. "math/rand"
  5. v "bitbucket.org/hackerbots/vector"
  6. )
  7. // Obstacle is the implementation of the generic building type in the game.
  8. type Obstacle struct {
  9. Bounds v.AABB2d `json:"bounds"`
  10. Hp int `json:"-"`
  11. }
  12. func (o Obstacle) distance_from_point(p v.Point2d) float32 {
  13. dist := math.MaxFloat32
  14. dist = math.Min(dist, float64(p.Sub(o.Bounds.A).Mag()))
  15. dist = math.Min(dist, float64(p.Sub(o.Bounds.B).Mag()))
  16. dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.A.X, Y: o.Bounds.B.Y}).Mag()))
  17. dist = math.Min(dist, float64(p.Sub(v.Point2d{X: o.Bounds.B.X, Y: o.Bounds.A.Y}).Mag()))
  18. return float32(dist)
  19. }
  20. func (o Obstacle) minify() [4]int {
  21. out := [4]int{int(o.Bounds.A.X), int(o.Bounds.A.Y), int(o.Bounds.B.X), int(o.Bounds.B.Y)}
  22. return out
  23. }
  24. // MinifyObstacles is a function used to convert []osbstacles into more tightly
  25. // packed [][4]int for smaller json payloads
  26. func MinifyObstacles(o []Obstacle) [][4]int {
  27. out := [][4]int{}
  28. for i := range o {
  29. out = append(out, o[i].minify())
  30. }
  31. return out
  32. }
  33. // GenerateObstacles returns a slice of (count) obstacles within a region
  34. // bounded by width, height.
  35. func GenerateObstacles(count int, width, height float32) []Obstacle {
  36. out := []Obstacle{}
  37. for i := 0; i < count; i++ {
  38. x := rand.Float32() * (width - (width / 10))
  39. y := rand.Float32() * (height - (height / 10))
  40. w := rand.Float32() * (width / 10)
  41. h := rand.Float32() * (height / 10)
  42. out = append(
  43. out,
  44. Obstacle{
  45. Bounds: v.AABB2d{
  46. A: v.Point2d{X: x, Y: y},
  47. B: v.Point2d{X: 20 + x + w, Y: 20 + y + h},
  48. },
  49. Hp: 10000,
  50. })
  51. }
  52. return out
  53. }