First pass at documentations.
This commit is contained in:
@@ -13,16 +13,21 @@ import (
|
||||
|
||||
const maxPlayer = 128
|
||||
|
||||
// BotHealth is sent to all players so they know how other robots are
|
||||
// doing.
|
||||
type BotHealth struct {
|
||||
RobotId string `json:"robot_id"`
|
||||
Health int `json:"health"`
|
||||
}
|
||||
|
||||
// Scanner contains a Robot/Projectile hash and is sent to the user to
|
||||
// let them know which things they know about.
|
||||
type Scanner struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// BotStats is stats for a single Player's Robot.
|
||||
type BotStats struct {
|
||||
Kills int
|
||||
Deaths int
|
||||
@@ -33,25 +38,31 @@ type BotStats struct {
|
||||
Wins int
|
||||
}
|
||||
|
||||
// PlayerStats is what you want many of. Contains a map of BotStats and total
|
||||
// wins.
|
||||
type PlayerStats struct {
|
||||
BotStats map[string]*BotStats
|
||||
Wins int
|
||||
}
|
||||
|
||||
// GameStats is a collection of PlayerStats for all players involved.
|
||||
type GameStats struct {
|
||||
PlayerStats map[string]*PlayerStats
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// Game is the main point of interest in this application. Embodies all info
|
||||
// required to keep track of players, robots, stats, projectils, etc.
|
||||
// Currently Controllers have a map of these.
|
||||
type Game struct {
|
||||
id string
|
||||
players map[*player]bool
|
||||
players map[*Player]bool
|
||||
projectiles map[*Projectile]bool
|
||||
splosions map[*Splosion]bool
|
||||
obstacles []Obstacle
|
||||
obstacle_count int
|
||||
register chan *player
|
||||
unregister chan *player
|
||||
register chan *Player
|
||||
unregister chan *Player
|
||||
turn int
|
||||
players_remaining int
|
||||
width, height float32
|
||||
@@ -68,12 +79,14 @@ type Game struct {
|
||||
bw *bandwidth.Bandwidth
|
||||
}
|
||||
|
||||
// This is the interface that different gametypes should implement.
|
||||
type GameMode interface {
|
||||
setup(g *Game)
|
||||
tick(gg *Game, payload *Boardstate)
|
||||
gameOver(gg *Game) (bool, *GameOver)
|
||||
}
|
||||
|
||||
// NewGame Poplulates a Game struct and starts the bandwidth calculator.
|
||||
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) (*Game, error) {
|
||||
bw, err := bandwidth.NewBandwidth(
|
||||
[]int{1, 10, 60},
|
||||
@@ -85,13 +98,13 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m
|
||||
go bw.Run()
|
||||
g := &Game{
|
||||
id: id,
|
||||
register: make(chan *player, maxPlayer),
|
||||
unregister: make(chan *player, maxPlayer),
|
||||
register: make(chan *Player, maxPlayer),
|
||||
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),
|
||||
players: make(map[*Player]bool),
|
||||
turn: 0,
|
||||
width: width,
|
||||
height: height,
|
||||
@@ -119,6 +132,7 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// tick is the method called every TICK ms.
|
||||
func (g *Game) tick(payload *Boardstate) {
|
||||
g.players_remaining = 0
|
||||
payload.Objects = MinifyObstacles(g.obstacles)
|
||||
@@ -174,6 +188,8 @@ func (g *Game) tick(payload *Boardstate) {
|
||||
}
|
||||
}
|
||||
|
||||
// sendUpdate is what we use to determine what data goes out to each client;
|
||||
// performs filtering and sorting of the data.
|
||||
func (g *Game) sendUpdate(payload *Boardstate) {
|
||||
// Ensure that the robots are always sent in a consistent order
|
||||
sort.Sort(RobotSorter{Robots: payload.OtherRobots})
|
||||
@@ -270,6 +286,7 @@ func (g *Game) sendUpdate(payload *Boardstate) {
|
||||
|
||||
}
|
||||
|
||||
// run is the method that contians the main game loop.
|
||||
func (g *Game) run() {
|
||||
ticker := time.NewTicker(time.Duration(g.tick_duration) * time.Millisecond)
|
||||
for {
|
||||
@@ -323,6 +340,8 @@ func (g *Game) run() {
|
||||
log.Println("run done")
|
||||
}
|
||||
|
||||
// sendGameOver is a special method that sends a GameOver object to the clients
|
||||
// instead of a normal Boardstate message.
|
||||
func (g *Game) sendGameOver(eg *GameOver) {
|
||||
log.Printf("sending out game over message: %+v", eg)
|
||||
for p := range g.players {
|
||||
|
||||
Reference in New Issue
Block a user