From fa6bba4647489c3cdd347e91f608906bd76f7d75 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Sun, 9 Feb 2020 08:42:10 -0800 Subject: [PATCH] refactor --- game/ai.go | 8 ++++ game/commands.go | 8 ---- game/game.go | 95 +++++++++++++++++++++++------------------------- game/physics.go | 5 +++ 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/game/ai.go b/game/ai.go index 79eb921..187e78e 100644 --- a/game/ai.go +++ b/game/ai.go @@ -1,5 +1,13 @@ 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 { return chooseCommandHelper(s, teamID, aiDepth) } diff --git a/game/commands.go b/game/commands.go index e8b942a..1374933 100644 --- a/game/commands.go +++ b/game/commands.go @@ -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 { da := 1 diff --git a/game/game.go b/game/game.go index 837e794..1048600 100644 --- a/game/game.go +++ b/game/game.go @@ -28,9 +28,51 @@ type Racer struct { Battery Battery } -type Kinetics struct { - V int - A int +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) } type Battery struct { @@ -90,53 +132,6 @@ func maybePassBaton(s State, teamID int) State { 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 { return r.Position.Pos >= Steps } diff --git a/game/physics.go b/game/physics.go index d2ca01c..d5692e9 100644 --- a/game/physics.go +++ b/game/physics.go @@ -5,6 +5,11 @@ type Position struct { Pos int } +type Kinetics struct { + V int + A int +} + func accelerate(r Racer) Racer { if r.Kinetics.A < -MaxA { r.Kinetics.A = -MaxA