Make obstacles know how to minify themselves
including a fix for using pointers instead of values.
This commit is contained in:
+25
-15
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
@@ -13,6 +14,30 @@ type Obstacle struct {
|
||||
Hp int `json:"-"`
|
||||
}
|
||||
|
||||
func (o *Obstacle) MarshalJSON() ([]byte, error) {
|
||||
payload := [4]float64{
|
||||
o.Bounds.A.X,
|
||||
o.Bounds.A.Y,
|
||||
|
||||
o.Bounds.B.X,
|
||||
o.Bounds.B.Y,
|
||||
}
|
||||
return json.Marshal(payload)
|
||||
}
|
||||
|
||||
func (o *Obstacle) UnmarshalJSON(incoming []byte) error {
|
||||
payload := [4]float64{}
|
||||
err := json.Unmarshal(incoming, &payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.Bounds = v.AABB2d{
|
||||
A: v.Point2d{X: payload[0], Y: payload[1]},
|
||||
B: v.Point2d{X: payload[2], Y: payload[3]},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o Obstacle) distance_from_point(p v.Point2d) float64 {
|
||||
dist := math.MaxFloat32
|
||||
|
||||
@@ -24,21 +49,6 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 {
|
||||
return 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
|
||||
}
|
||||
|
||||
// MinifyObstacles is a function used to convert []osbstacles into more tightly
|
||||
// packed [][4]int for smaller json payloads
|
||||
func MinifyObstacles(o []Obstacle) [][4]int {
|
||||
out := [][4]int{}
|
||||
for i := range o {
|
||||
out = append(out, o[i].minify())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GenerateObstacles returns a slice of (count) obstacles within a region
|
||||
// bounded by width, height.
|
||||
func GenerateObstacles(count int, width, height float64) []Obstacle {
|
||||
|
||||
Reference in New Issue
Block a user