Browse Source

Make obstacles know how to minify themselves

including a fix for using pointers instead of values.
Stephen McQuay 5 years ago
parent
commit
961cf5d7da
3 changed files with 30 additions and 30 deletions
  1. 4
    14
      game.go
  2. 25
    15
      obstacle.go
  3. 1
    1
      protocol.go

+ 4
- 14
game.go View File

@@ -135,7 +135,6 @@ func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, m
135 135
 // tick is the method called every TICK ms.
136 136
 func (g *Game) tick(payload *Boardstate) {
137 137
 	g.players_remaining = 0
138
-	payload.Objects = MinifyObstacles(g.obstacles)
139 138
 
140 139
 	// Update Players
141 140
 	for p := range g.players {
@@ -205,12 +204,9 @@ func (g *Game) sendUpdate(payload *Boardstate) {
205 204
 
206 205
 		for _, r := range p.Robots {
207 206
 			player_payload.MyRobots = append(player_payload.MyRobots, *r)
208
-			// player_payload.OtherRobots = append(
209
-			//  player_payload.OtherRobots,
210
-			//  r.GetTruncatedDetails())
211 207
 		}
212 208
 
213
-		player_payload.Objects = [][4]int{}
209
+		player_payload.Obstacles = []Obstacle{}
214 210
 		player_payload.Splosions = []Splosion{}
215 211
 		player_payload.Projectiles = []Projectile{}
216 212
 		living_robots := 0
@@ -264,23 +260,17 @@ func (g *Game) sendUpdate(payload *Boardstate) {
264 260
 				// Filter objects
265 261
 				for _, ob := range g.obstacles {
266 262
 					if ob.distance_from_point(r.Position) < float64(r.Stats.ScannerRadius)+r.ScanCounter {
267
-						player_payload.Objects = append(
268
-							player_payload.Objects, ob.minify())
263
+						player_payload.Obstacles = append(
264
+							player_payload.Obstacles, ob)
269 265
 					}
270 266
 				}
271 267
 			}
272 268
 		}
273 269
 
274
-		// if living_robots == 0 {
275
-		// 	player_payload.OtherRobots = payload.OtherRobots
276
-		// 	player_payload.Projectiles = payload.Projectiles
277
-		// 	player_payload.Splosions = payload.Splosions
278
-		// 	player_payload.Objects = payload.Objects
279
-		// }
280
-
281 270
 		p.send <- player_payload
282 271
 	}
283 272
 	for s := range g.spectators {
273
+		payload.Obstacles = g.obstacles
284 274
 		s.send <- payload
285 275
 	}
286 276
 

+ 25
- 15
obstacle.go View File

@@ -1,6 +1,7 @@
1 1
 package server
2 2
 
3 3
 import (
4
+	"encoding/json"
4 5
 	"math"
5 6
 	"math/rand"
6 7
 
@@ -13,6 +14,30 @@ type Obstacle struct {
13 14
 	Hp     int      `json:"-"`
14 15
 }
15 16
 
17
+func (o *Obstacle) MarshalJSON() ([]byte, error) {
18
+	payload := [4]float64{
19
+		o.Bounds.A.X,
20
+		o.Bounds.A.Y,
21
+
22
+		o.Bounds.B.X,
23
+		o.Bounds.B.Y,
24
+	}
25
+	return json.Marshal(payload)
26
+}
27
+
28
+func (o *Obstacle) UnmarshalJSON(incoming []byte) error {
29
+	payload := [4]float64{}
30
+	err := json.Unmarshal(incoming, &payload)
31
+	if err != nil {
32
+		return err
33
+	}
34
+	o.Bounds = v.AABB2d{
35
+		A: v.Point2d{X: payload[0], Y: payload[1]},
36
+		B: v.Point2d{X: payload[2], Y: payload[3]},
37
+	}
38
+	return nil
39
+}
40
+
16 41
 func (o Obstacle) distance_from_point(p v.Point2d) float64 {
17 42
 	dist := math.MaxFloat32
18 43
 
@@ -24,21 +49,6 @@ func (o Obstacle) distance_from_point(p v.Point2d) float64 {
24 49
 	return dist
25 50
 }
26 51
 
27
-func (o Obstacle) minify() [4]int {
28
-	out := [4]int{int(o.Bounds.A.X), int(o.Bounds.A.Y), int(o.Bounds.B.X), int(o.Bounds.B.Y)}
29
-	return out
30
-}
31
-
32
-// MinifyObstacles is a function used to convert []osbstacles into more tightly
33
-// packed [][4]int for smaller json payloads
34
-func MinifyObstacles(o []Obstacle) [][4]int {
35
-	out := [][4]int{}
36
-	for i := range o {
37
-		out = append(out, o[i].minify())
38
-	}
39
-	return out
40
-}
41
-
42 52
 // GenerateObstacles returns a slice of (count) obstacles within a region
43 53
 // bounded by width, height.
44 54
 func GenerateObstacles(count int, width, height float64) []Obstacle {

+ 1
- 1
protocol.go View File

@@ -115,7 +115,7 @@ type Boardstate struct {
115 115
 	OtherRobots []OtherRobot `json:"robots"`
116 116
 	Projectiles []Projectile `json:"projectiles"`
117 117
 	Splosions   []Splosion   `json:"splosions"`
118
-	Objects     [][4]int     `json:"objects"`
118
+	Obstacles   []Obstacle   `json:"objects"`
119 119
 	Type        string       `json:"type"`
120 120
 	Turn        int          `json:"turn"`
121 121
 	AllBots     []BotHealth  `json:"all_bots"`

Loading…
Cancel
Save