allow object specification

This commit is contained in:
Stephen McQuay 2013-11-16 19:57:13 -08:00
parent f6b3027ded
commit 5a2651d0dd
3 changed files with 11 additions and 7 deletions

View File

@ -23,6 +23,7 @@ func startGame(w http.ResponseWriter, req *http.Request) {
requested_game_name := idg.Hash() requested_game_name := idg.Hash()
width, height := float32(conf.Width), float32(conf.Height) width, height := float32(conf.Width), float32(conf.Height)
obstacles := 0
tick := conf.Tick tick := conf.Tick
// here we determine if we are going to run with defaults or pick them off // here we determine if we are going to run with defaults or pick them off
@ -34,10 +35,11 @@ func startGame(w http.ResponseWriter, req *http.Request) {
} }
req.Body.Close() req.Body.Close()
cfg := struct { cfg := struct {
Width float32 `json:"width"` Width float32 `json:"width"`
Height float32 `json:"height"` Height float32 `json:"height"`
Name string `json:"name"` Name string `json:"name"`
Tick int `json:"tick"` Obstacles int `json:"obstacles"`
Tick int `json:"tick"`
}{} }{}
err = json.Unmarshal(body, &cfg) err = json.Unmarshal(body, &cfg)
if err != nil { if err != nil {
@ -49,6 +51,7 @@ func startGame(w http.ResponseWriter, req *http.Request) {
requested_game_name = cfg.Name requested_game_name = cfg.Name
width = cfg.Width width = cfg.Width
height = cfg.Height height = cfg.Height
obstacles = cfg.Obstacles
tick = cfg.Tick tick = cfg.Tick
} }
@ -56,7 +59,7 @@ func startGame(w http.ResponseWriter, req *http.Request) {
g := games.get(requested_game_name) g := games.get(requested_game_name)
if g == nil { if g == nil {
log.Printf("Game '%s' non-existant; making it now", requested_game_name) log.Printf("Game '%s' non-existant; making it now", requested_game_name)
g = NewGame(requested_game_name, width, height, tick) g = NewGame(requested_game_name, width, height, obstacles, tick)
go g.run() go g.run()
games.add(g) games.add(g)
} else { } else {

View File

@ -66,14 +66,14 @@ type game struct {
winners WinnerMap winners WinnerMap
} }
func NewGame(id string, width, height float32, tick int) *game { func NewGame(id string, width, height float32, obstacles, tick int) *game {
g := &game{ g := &game{
id: id, id: id,
register: make(chan *player), register: make(chan *player),
unregister: make(chan *player, maxPlayer), unregister: make(chan *player, maxPlayer),
projectiles: make(map[*Projectile]bool), projectiles: make(map[*Projectile]bool),
splosions: make(map[*Splosion]bool), splosions: make(map[*Splosion]bool),
obstacles: GenerateObstacles(conf.Obstacles, width, height), obstacles: GenerateObstacles(obstacles, width, height),
players: make(map[*player]bool), players: make(map[*player]bool),
turn: 0, turn: 0,
width: width, width: width,

View File

@ -165,6 +165,7 @@ func addPlayer(ws *websocket.Conn) {
gid.Id, gid.Id,
float32(conf.Width), float32(conf.Width),
float32(conf.Height), float32(conf.Height),
conf.Obstacles,
conf.Tick, conf.Tick,
) )
go game.run() go game.run()