run AI in separate goroutine
This commit is contained in:
parent
0cb604f102
commit
70600a1142
|
@ -1,6 +1,6 @@
|
|||
package game
|
||||
|
||||
func ChooseCommand(s State, teamID int) Command {
|
||||
func chooseCommand(s State, teamID int) Command {
|
||||
return chooseCommandHelper(s, teamID, aiDepth)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package game
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
type Command int
|
||||
|
||||
const (
|
||||
|
@ -12,9 +16,18 @@ const (
|
|||
|
||||
var validCommands = []Command{speedUp, slowDown, left, right, clearObstacle}
|
||||
|
||||
func PollCommands(s State) []Command {
|
||||
cmds := make([]Command, len(s.Teams))
|
||||
for i := range s.Teams {
|
||||
cmd := chooseCommand(s, i)
|
||||
log.Printf("team %d chose to %v", i, cmd)
|
||||
cmds[i] = cmd
|
||||
}
|
||||
return cmds
|
||||
}
|
||||
|
||||
func doCommand(cmd Command, s State, teamID int) State {
|
||||
da := 1
|
||||
//da += rand.Intn(3) - 1
|
||||
|
||||
r := ActiveRacer(s.Teams[teamID])
|
||||
if r == nil {
|
||||
|
|
13
main.go
13
main.go
|
@ -44,6 +44,9 @@ func run() error {
|
|||
second = time.Tick(time.Second)
|
||||
)
|
||||
|
||||
cmdC := make(chan []game.Command)
|
||||
go func() { cmdC <- game.PollCommands(s) }()
|
||||
|
||||
for !w.Closed() && !s.GameOver {
|
||||
switch {
|
||||
case w.Pressed(pixelgl.KeyQ):
|
||||
|
@ -58,13 +61,7 @@ func run() error {
|
|||
rs.Animating = true
|
||||
rs.Frame = 0
|
||||
|
||||
cmds := make([]game.Command, len(s.Teams))
|
||||
for i := range s.Teams {
|
||||
cmd := game.ChooseCommand(s, i)
|
||||
log.Printf("team %d chose to %v", i, cmd)
|
||||
cmds[i] = cmd
|
||||
}
|
||||
|
||||
cmds := <-cmdC
|
||||
s = game.UpdateState(s, sOld, cmds)
|
||||
turn++
|
||||
if s.GameOver {
|
||||
|
@ -72,10 +69,12 @@ func run() error {
|
|||
sOld = s
|
||||
turn = 1
|
||||
}
|
||||
go func() { cmdC <- game.PollCommands(s) }()
|
||||
}
|
||||
|
||||
w.Update()
|
||||
frames++
|
||||
|
||||
select {
|
||||
case <-second:
|
||||
w.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
|
||||
|
|
Loading…
Reference in New Issue