-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
116 lines (88 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"time"
config "github.com/AkselsLedins/google-hashcode-2018-live-simulation/config"
simulator "github.com/AkselsLedins/google-hashcode-2018-live-simulation/simulator"
ui "github.com/AkselsLedins/google-hashcode-2018-live-simulation/ui"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
var (
size *int
windowSize *float64
frames = 0
second = time.Tick(time.Second)
noGuiFlag *bool
outputFile *string
inputFile *string
simulation *simulator.Simulation
)
func init() {
simulation = simulator.NewSimulation()
outputFile = flag.String("o", "", "Path to your result")
inputFile = flag.String("i", "", "Path to the exercice input")
noGuiFlag = flag.Bool("noGui", false, "Run the simulation without a GUI")
flag.Parse()
simulation.ParseOutputFile(*outputFile)
simulation.ParseInputFile(*inputFile)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: config.Config.UI.WindowTitle,
Bounds: pixel.R(0, 0, 1024, 720),
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
tick := time.Tick(6 * time.Millisecond)
imd := imdraw.New(nil)
last := time.Now()
for !win.Closed() && !simulation.Ended {
imd.Clear()
frames++
dt := time.Since(last).Seconds()
last = time.Now()
// set the camera for the scene
win.SetMatrix(ui.Cam().GetMatrix(win))
// handle user inputs
simulation.HandleEvents(win, dt)
select {
case <-tick:
win.Clear(colornames.Black)
simulation.Run(imd)
imd.Draw(win)
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
frames = 0
}
// reset the matrix
win.SetMatrix(pixel.IM)
// drall the UI
ui.DrawStepNumber(win, simulation.Step)
ui.DrawScore(win, simulation.Score)
ui.DrawNumberOfVehicles(win, len(simulation.Vehicles))
ui.DrawNumberOfTrips(win, len(simulation.Trips))
ui.DrawStartHint(win)
win.Update()
}
fmt.Printf("Score: %d\n", simulation.Score)
}
func noGui() {
simulation.Start()
for !simulation.Ended {
simulation.Run(nil)
}
fmt.Printf("%d\n", simulation.Score)
}
func main() {
if *noGuiFlag == true {
noGui()
return
}
pixelgl.Run(run)
}