diff --git a/config/server.json b/config/server.json index 4404a1d..586f541 100644 --- a/config/server.json +++ b/config/server.json @@ -1,7 +1,13 @@ { "tick": 66, "timescale": 1.0, - "width": 1600, - "height": 1100, - "obstacle": 20 + "width": 1000, + "height": 500, + "obstacles": [ + [100, 300, 200, 400], + [100, 100, 200, 200], + [400, 150, 600, 350], + [800, 300, 900, 400], + [800, 100, 900, 200] + ] } diff --git a/control.go b/control.go index cab3452..322fe61 100644 --- a/control.go +++ b/control.go @@ -64,6 +64,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { requested_game_name := c.Idg.Hash() width, height := float64(c.Conf.Width), float64(c.Conf.Height) obstacleCount := 0 + obstacles := []Obstacle{} maxPoints := c.Conf.MaxPoints mode := "deathmatch" @@ -90,6 +91,7 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { width = float64(cfg.Width) height = float64(cfg.Height) obstacleCount = cfg.ObstacleCount + obstacles = cfg.Obstacles maxPoints = cfg.MaxPoints mode = cfg.Mode } @@ -98,7 +100,27 @@ func (c *Controller) StartGame(w http.ResponseWriter, req *http.Request) { if g == nil { log.Printf("Game '%s' non-existant; making it now", requested_game_name) var err error - g, err = NewGame(requested_game_name, width, height, obstacleCount, c.Conf.Tick, maxPoints, mode) + g, err = NewGame( + requested_game_name, + width, + height, + c.Conf.Tick, + maxPoints, + mode, + ) + g.obstacleCount = obstacleCount + g.obstacles = obstacles + g.defaultObstacles = obstacles + if len(g.defaultObstacles) == 0 { + g.obstacles = GenerateObstacles( + g.obstacleCount, + g.width, + g.height, + ) + } else { + g.obstacles = c.Conf.Obstacles + } + if err != nil { log.Printf("problem creating game: %s: %s", requested_game_name, err) b, _ := json.Marshal(NewFailure("game creation failure")) diff --git a/deathmatch.go b/deathmatch.go index af27f46..60d2ba5 100644 --- a/deathmatch.go +++ b/deathmatch.go @@ -17,7 +17,13 @@ func (g *deathmatch) gameOver(gg *Game) (bool, *GameOver) { var stats *GameOver if gg.players_remaining <= 1 && len(gg.players) > 1 { - gg.obstacles = GenerateObstacles(gg.obstacle_count, gg.width, gg.height) + if len(gg.defaultObstacles) == 0 { + gg.obstacles = GenerateObstacles( + gg.obstacleCount, + gg.width, + gg.height, + ) + } log.Printf("game %s: game over", gg.id) stats = NewGameOver() diff --git a/game.go b/game.go index 2f20cb6..2c73fc4 100644 --- a/game.go +++ b/game.go @@ -59,8 +59,9 @@ type Game struct { players map[*Player]bool projectiles map[*Projectile]bool splosions map[*Splosion]bool + defaultObstacles []Obstacle obstacles []Obstacle - obstacle_count int + obstacleCount int register chan *Player unregister chan *Player turn int @@ -87,7 +88,7 @@ type GameMode interface { } // NewGame Poplulates a Game struct and starts the bandwidth calculator. -func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, mode string) (*Game, error) { +func NewGame(id string, width, height float64, tick, maxPoints int, mode string) (*Game, error) { bw, err := bandwidth.NewBandwidth( []int{1, 10, 60}, 1*time.Second, @@ -102,8 +103,6 @@ func NewGame(id string, width, height float64, obstacles, tick, maxPoints int, m unregister: make(chan *Player, maxPlayer), projectiles: make(map[*Projectile]bool), splosions: make(map[*Splosion]bool), - obstacles: GenerateObstacles(obstacles, width, height), - obstacle_count: obstacles, players: make(map[*Player]bool), turn: 0, width: width, diff --git a/protocol.go b/protocol.go index 39c6817..5131e3c 100644 --- a/protocol.go +++ b/protocol.go @@ -177,11 +177,22 @@ func (c *Controller) AddPlayer(ws *websocket.Conn) { gid.Id, float64(c.Conf.Width), float64(c.Conf.Height), - c.Conf.ObstacleCount, c.Conf.Tick, c.Conf.MaxPoints, "", ) + game.defaultObstacles = c.Conf.Obstacles + game.obstacleCount = c.Conf.ObstacleCount + log.Printf("%t", len(game.defaultObstacles) == 0) + if len(game.defaultObstacles) == 0 { + game.obstacles = GenerateObstacles( + game.obstacleCount, + game.width, + game.height, + ) + } else { + game.obstacles = c.Conf.Obstacles + } if err != nil { log.Printf("problem creating game: %s", gid.Id) websocket.JSON.Send(ws, NewFailure("game creation error"))