/
main.go
233 lines (190 loc) · 5.1 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"errors"
"flag"
"image"
"os"
"runtime/pprof"
"strconv"
"strings"
"time"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"geometry-jumper/game"
"geometry-jumper/keyboard"
"geometry-jumper/menu"
"geometry-jumper/ranchblt"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten"
"github.com/uber-go/zap"
)
var (
player *game.PlayerCharacter
platform *game.Stationary
keyboardWrapper = keyboard.NewKeyboardWrapper()
shapeCollection *game.ShapeCollection
logoScreen *ranchblt.Logo
showLogo = true
showMenu = true
mainMenu menu.Menu
endMenu menu.Menu
slowDownCount int64
logger zap.Logger
)
// Version is autoset from the build script
var Version string
// Build is autoset from the build script
var Build string
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var debug = flag.Bool("debug", false, "Turns on debug lines and debug messaging")
func gameLoop(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() {
slowDownCount++
logger.Debug("Running slow",
zap.Int64("Amount", slowDownCount),
)
return nil
}
if err := game.AudioContext.Update(); err != nil {
logger.Error("Failed to play audio",
zap.Error(err),
)
}
keyboardWrapper.Update()
if showLogo && !game.Debug {
logoScreen.Draw(screen)
return nil
}
if showMenu {
mainMenu.Update()
mainMenu.Draw(screen)
if keyboardWrapper.KeyPushed(ebiten.KeyEnter) {
if strings.ToLower(mainMenu.Selected()) == "start" {
showMenu = false
} else if strings.ToLower(mainMenu.Selected()) == "exit" {
return errors.New("User wanted to quit")
}
}
return nil
}
if game.Debug {
screen.DrawImage(game.UpperTrackLine, game.UpperTrackOpts)
screen.DrawImage(game.LowerTrackLine, game.LowerTrackOpts)
}
if !player.Collided {
shapeCollection.Update()
player.Update()
} else {
shapeCollection.Stop = true
endMenu.Update()
endMenu.Draw(screen)
screen.DrawImage(getScoreImage(player.Score()), &ebiten.DrawImageOptions{})
if keyboardWrapper.KeyPushed(ebiten.KeyEnter) {
if strings.ToLower(endMenu.Selected()) == "restart" {
shapeCollection = game.NewShapeCollection()
player = game.NewPlayerCharacter("Test", game.PersonStandingImage, game.PersonJumpingImage, keyboardWrapper)
} else if strings.ToLower(endMenu.Selected()) == "exit" {
return errors.New("User wanted to quit")
}
}
}
platform.Draw(screen)
shapeCollection.Draw(screen)
player.Draw(screen)
player.CheckCollision(shapeCollection)
player.CheckScore(shapeCollection)
if keyboardWrapper.KeyPushed(ebiten.KeyEscape) {
return errors.New("User wanted to quit") //Best way to do this?
}
return nil
}
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
lvl := zap.ErrorLevel
if *debug {
lvl = zap.DebugLevel
}
logger = zap.New(zap.NewTextEncoder(zap.TextNoTime()), lvl)
game.Load(logger)
game.Debug = *debug
options := []*menu.Option{}
options = append(options, &menu.Option{
Text: "Start",
})
options = append(options, &menu.Option{
Text: "Exit",
})
mainMenu = &menu.Regular{
BackgroundImage: game.TitleImage,
Height: game.ScreenHeight,
Width: game.ScreenWidth,
KeyboardWrapper: keyboardWrapper,
Options: options,
Font: game.Font,
}
options2 := []*menu.Option{}
options2 = append(options2, &menu.Option{
Text: "Restart",
})
options2 = append(options2, &menu.Option{
Text: "Exit",
})
endMenu = &menu.Regular{
BackgroundImage: game.EndImage,
Height: game.ScreenHeight,
Width: game.ScreenWidth,
KeyboardWrapper: keyboardWrapper,
Options: options2,
Font: game.Font,
}
shapeCollection = game.NewShapeCollection()
player = game.NewPlayerCharacter("Test", game.PersonStandingImage, game.PersonJumpingImage, keyboardWrapper)
platform = &game.Stationary{
Image: game.PlatformImage,
X: 0,
Y: 200,
}
logoScreen = ranchblt.NewLogoScreen(game.ScreenWidth, game.ScreenHeight)
logger.Info("Starting up game",
zap.String("Version", Version),
zap.String("Build", Build),
)
game.PlayBGM(game.BGM0)
game.SetBGMVolume(.1)
go logoTimer()
ebiten.Run(gameLoop, game.ScreenWidth, game.ScreenHeight, 2, "Geom Jump")
}
func logoTimer() {
timer := time.NewTimer(time.Second * 2)
<-timer.C
showLogo = false
}
func getScoreImage(score int) *ebiten.Image {
const size = 24
const dpi = 72
textImage, _ := ebiten.NewImage(game.ScreenWidth, game.ScreenHeight, ebiten.FilterNearest)
dst := image.NewRGBA(image.Rect(0, 0, game.ScreenWidth, game.ScreenHeight))
d := &font.Drawer{
Dst: dst,
Src: image.White,
Face: truetype.NewFace(game.Font, &truetype.Options{
Size: size,
DPI: dpi,
Hinting: font.HintingFull,
}),
}
st := "Score: " + strconv.Itoa(score)
s := font.MeasureString(d.Face, st)
d.Dot = fixed.P(game.ScreenWidth/2-s.Round()/2, game.ScreenHeight-100)
d.DrawString(st)
textImage.ReplacePixels(dst.Pix)
return textImage
}