allow the control interface to specify game type

This commit is contained in:
Fraser Graham 2014-01-15 22:16:06 -08:00
parent 9583de2ffa
commit 90b7731865
4 changed files with 20 additions and 11 deletions

View File

@ -18,15 +18,17 @@ type Config struct {
Height int `json:"height"`
Obstacles int `json:"obstacles"`
MaxPoints int `json:"max_points"`
Mode string `json:"mode"`
}
const (
TICK = 60
TIMESCALE = 1.0
WIDTH = 800
HEIGHT = 550
OBSTACLES = 5
MAX_POINTS = 500 // allowing for 50 pts in every category
TICK = 60
TIMESCALE = 1.0
WIDTH = 800
HEIGHT = 550
OBSTACLES = 5
MAX_POINTS = 500 // allowing for 50 pts in every category
DEFAULT_MODE = "deathmatch"
)
func loadConfig(filename string) (Config, error) {
@ -37,6 +39,7 @@ func loadConfig(filename string) (Config, error) {
Height: HEIGHT,
Obstacles: OBSTACLES,
MaxPoints: MAX_POINTS,
Mode: DEFAULT_MODE,
}
u, err := user.Current()
if err != nil {

View File

@ -24,8 +24,8 @@ func startGame(w http.ResponseWriter, req *http.Request) {
requested_game_name := idg.Hash()
width, height := float32(conf.Width), float32(conf.Height)
obstacles := 0
tick := conf.Tick
maxPoints := conf.MaxPoints
mode := "deathmatch"
// here we determine if we are going to run with defaults or pick them off
// a posted json blob
@ -50,14 +50,14 @@ func startGame(w http.ResponseWriter, req *http.Request) {
width = float32(cfg.Width)
height = float32(cfg.Height)
obstacles = cfg.Obstacles
tick = cfg.Tick
maxPoints = cfg.MaxPoints
mode = cfg.Mode
}
g := games.get(requested_game_name)
if g == nil {
log.Printf("Game '%s' non-existant; making it now", requested_game_name)
g = NewGame(requested_game_name, width, height, obstacles, tick, maxPoints)
g = NewGame(requested_game_name, width, height, obstacles, conf.Tick, maxPoints, mode)
go g.run()
games.add(g)
} else {

View File

@ -75,7 +75,7 @@ type GameMode interface {
gameOver(gg *game) (bool, *GameOver)
}
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *game {
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) *game {
g := &game{
id: id,
register: make(chan *player),
@ -98,7 +98,12 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *
tick_duration: tick,
players_remaining: 2,
winners: WinnerMap{m: make(map[string]int)},
mode: &melee{respawn: make(map[*Robot]float64)},
}
if mode == "melee" {
g.mode = &melee{respawn: make(map[*Robot]float64)}
} else {
g.mode = &deathmatch{}
}
g.mode.setup(g)

View File

@ -167,6 +167,7 @@ func addPlayer(ws *websocket.Conn) {
conf.Obstacles,
conf.Tick,
conf.MaxPoints,
"",
)
go game.run()
games.add(game)