refactor
This commit is contained in:
parent
06abd29278
commit
fa6bba4647
|
@ -1,5 +1,13 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
|
func pollCommands(s State) []Command {
|
||||||
|
cmds := make([]Command, len(s.Teams))
|
||||||
|
for i := range s.Teams {
|
||||||
|
cmds[i] = chooseCommand(s, i)
|
||||||
|
}
|
||||||
|
return cmds
|
||||||
|
}
|
||||||
|
|
||||||
func chooseCommand(s State, teamID int) Command {
|
func chooseCommand(s State, teamID int) Command {
|
||||||
return chooseCommandHelper(s, teamID, aiDepth)
|
return chooseCommandHelper(s, teamID, aiDepth)
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,14 +54,6 @@ func CommandLoop(w *pixelgl.Window, s State, stateCA chan<- State) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pollCommands(s State) []Command {
|
|
||||||
cmds := make([]Command, len(s.Teams))
|
|
||||||
for i := range s.Teams {
|
|
||||||
cmds[i] = chooseCommand(s, i)
|
|
||||||
}
|
|
||||||
return cmds
|
|
||||||
}
|
|
||||||
|
|
||||||
func doCommand(cmd Command, s State, teamID int) State {
|
func doCommand(cmd Command, s State, teamID int) State {
|
||||||
da := 1
|
da := 1
|
||||||
|
|
||||||
|
|
95
game/game.go
95
game/game.go
|
@ -28,9 +28,51 @@ type Racer struct {
|
||||||
Battery Battery
|
Battery Battery
|
||||||
}
|
}
|
||||||
|
|
||||||
type Kinetics struct {
|
func ActiveRacer(t Team) *Racer {
|
||||||
V int
|
for _, r := range t.Racers {
|
||||||
A int
|
if r.ID == t.Baton.HolderID {
|
||||||
|
rr := r
|
||||||
|
return &rr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateRacer(s State, r Racer) State {
|
||||||
|
t := s.Teams[r.TeamID]
|
||||||
|
for i, rr := range t.Racers {
|
||||||
|
if rr.ID == r.ID {
|
||||||
|
racers := append([]Racer{}, t.Racers[:i]...)
|
||||||
|
racers = append(racers, r)
|
||||||
|
racers = append(racers, t.Racers[i+1:]...)
|
||||||
|
t.Racers = racers
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s = updateTeam(s, t)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateTeam(s State, t Team) State {
|
||||||
|
teams := append([]Team{}, s.Teams[:t.id]...)
|
||||||
|
teams = append(teams, t)
|
||||||
|
teams = append(teams, s.Teams[t.id+1:]...)
|
||||||
|
s.Teams = teams
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func destroyRacer(s State, r Racer) State {
|
||||||
|
// insert derelict where racer was
|
||||||
|
s.Derelicts = append(s.Derelicts, Obstacle{Position: r.Position})
|
||||||
|
|
||||||
|
// spawn racer back at starting position
|
||||||
|
r.Position = s.SpawnPoints[r.ID].Pos
|
||||||
|
r.Kinetics = Kinetics{}
|
||||||
|
r.Battery.Charge = r.Battery.Capacity
|
||||||
|
|
||||||
|
return updateRacer(s, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Battery struct {
|
type Battery struct {
|
||||||
|
@ -90,53 +132,6 @@ func maybePassBaton(s State, teamID int) State {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func ActiveRacer(t Team) *Racer {
|
|
||||||
for _, r := range t.Racers {
|
|
||||||
if r.ID == t.Baton.HolderID {
|
|
||||||
rr := r
|
|
||||||
return &rr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateRacer(s State, r Racer) State {
|
|
||||||
t := s.Teams[r.TeamID]
|
|
||||||
for i, rr := range t.Racers {
|
|
||||||
if rr.ID == r.ID {
|
|
||||||
racers := append([]Racer{}, t.Racers[:i]...)
|
|
||||||
racers = append(racers, r)
|
|
||||||
racers = append(racers, t.Racers[i+1:]...)
|
|
||||||
t.Racers = racers
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s = updateTeam(s, t)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateTeam(s State, t Team) State {
|
|
||||||
teams := append([]Team{}, s.Teams[:t.id]...)
|
|
||||||
teams = append(teams, t)
|
|
||||||
teams = append(teams, s.Teams[t.id+1:]...)
|
|
||||||
s.Teams = teams
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func destroyRacer(s State, r Racer) State {
|
|
||||||
// insert derelict where racer was
|
|
||||||
s.Derelicts = append(s.Derelicts, Obstacle{Position: r.Position})
|
|
||||||
|
|
||||||
// spawn racer back at starting position
|
|
||||||
r.Position = s.SpawnPoints[r.ID].Pos
|
|
||||||
r.Kinetics = Kinetics{}
|
|
||||||
r.Battery.Charge = r.Battery.Capacity
|
|
||||||
|
|
||||||
return updateRacer(s, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func won(r Racer, s State) bool {
|
func won(r Racer, s State) bool {
|
||||||
return r.Position.Pos >= Steps
|
return r.Position.Pos >= Steps
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,11 @@ type Position struct {
|
||||||
Pos int
|
Pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Kinetics struct {
|
||||||
|
V int
|
||||||
|
A int
|
||||||
|
}
|
||||||
|
|
||||||
func accelerate(r Racer) Racer {
|
func accelerate(r Racer) Racer {
|
||||||
if r.Kinetics.A < -MaxA {
|
if r.Kinetics.A < -MaxA {
|
||||||
r.Kinetics.A = -MaxA
|
r.Kinetics.A = -MaxA
|
||||||
|
|
Loading…
Reference in New Issue