-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
290 lines (232 loc) · 5.75 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"fmt"
"image"
"io/ioutil"
"os"
_ "image/png"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/golang/freetype/truetype"
"golang.org/x/image/colornames"
)
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
func loadSprite(path string) (pixel.Sprite, error) {
pic, err := loadPicture(path)
if err != nil {
return *pixel.NewSprite(pic, pic.Bounds()), err
}
sprite := pixel.NewSprite(pic, pic.Bounds())
return *sprite, nil
}
func loadTTF(path string, size float64, origin pixel.Vec) *text.Text {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
font, err := truetype.Parse(bytes)
if err != nil {
panic(err)
}
face := truetype.NewFace(font, &truetype.Options{
Size: size,
GlyphCacheEntries: 1,
})
atlas := text.NewAtlas(face, text.ASCII)
txt := text.New(origin, atlas)
return txt
}
type mode int
const (
menu mode = 0
flying mode = 1
mapper mode = 2
)
func run() {
// Set up window configs
cfg := pixelgl.WindowConfig{ // Default: 1024 x 768
Title: "woosh!",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
//Monitor: pixelgl.PrimaryMonitor(),
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
// Importantr variables
var shipX, shipY, velX, velY, radians float64
engineOn := false
gravity := 0.02 // Default: 0.004
shipAcc := 0.08 // Default: 0.008
tilt := 0.025 // Default: 0.001
whichOn := false
onNumber := 0
shipOffName := "res/ship.png"
shipOn1Name := "res/ship_on.png"
shipOn2Name := "res/ship_on2.png"
camVector := win.Bounds().Center()
bg, _ := loadSprite("res/sky.png")
// Jetpack - Rendering
engineOff, err := loadSprite(shipOffName)
if err != nil {
panic(err)
}
engineOn1, err := loadSprite(shipOn1Name)
if err != nil {
panic(err)
}
engineOn2, err := loadSprite(shipOn2Name)
if err != nil {
panic(err)
}
txt := loadTTF("fonts/oldgamefatty.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200))
menutxt := loadTTF("fonts/oldgamefatty.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y+200))
currentSprite := engineOff
frameCounter := 0
areadout := int(shipY)
vreadout := int(velY)
currentmode := flying
// Game Loop
for !win.Closed() {
if currentmode == flying {
win.Update()
win.Clear(colornames.Green)
// Jetpack - Controls
engineOn = win.Pressed(pixelgl.KeyUp) || win.Pressed(pixelgl.KeyW)
if win.Pressed(pixelgl.KeyRight) || win.Pressed(pixelgl.KeyD) {
radians -= tilt
} else if win.Pressed(pixelgl.KeyLeft) || win.Pressed(pixelgl.KeyA) {
radians += tilt
} else if win.JustPressed(pixelgl.KeyW) {
currentmode = menu
} else if win.JustPressed(pixelgl.KeyM) {
currentmode = mapper
}
if shipY < 0 {
shipY = 0
velY = -0.3 * velY
}
if engineOn {
heading := pixel.Unit(radians)
acc := heading.Scaled(shipAcc)
velY += acc.X
velX -= acc.Y
whichOn = !whichOn
onNumber++
if onNumber == 5 { // every 5 frames, toggle anishipMation
onNumber = 0
if whichOn {
currentSprite = engineOn1
} else {
currentSprite = engineOn2
}
}
} else {
currentSprite = engineOff
}
velY -= gravity
positionVector := pixel.V(win.Bounds().Center().X+shipX, win.Bounds().Center().Y+shipY-372)
shipMat := pixel.IM
shipMat = shipMat.Scaled(pixel.ZV, 4)
shipMat = shipMat.Moved(positionVector)
shipMat = shipMat.Rotated(positionVector, radians)
shipX += velX
shipY += velY
// Camera
camVector.X += (positionVector.X - camVector.X) * 0.2
camVector.Y += (positionVector.Y - camVector.Y) * 0.2
shipY = pixel.Clamp(shipY, -5, 22500)
shipX = pixel.Clamp(shipX, -14843, 25085)
if camVector.X > 25085 {
camVector.X = 25085
} else if camVector.X < -14843 {
camVector.X = -14843
}
if camVector.Y > 22500 {
camVector.Y = 22500
}
cam := pixel.IM.Moved(win.Bounds().Center().Sub(camVector))
win.SetMatrix(cam)
// Drawing to the screen
win.SetSmooth(true)
bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10))
//doesnt work
// txt.Draw(win, shipMat)
txt.Clear()
fmt.Fprintf(txt, "altitude: %d\n", areadout)
fmt.Fprintf(txt, "velocity: %d", vreadout)
txt.Draw(win, pixel.IM.Moved(positionVector))
if frameCounter >= 10 {
areadout = int(shipY)
vreadout = int(velY)
frameCounter = 0
}
win.SetSmooth(false)
currentSprite.Draw(win, shipMat)
frameCounter++
}
if currentmode == menu {
currentitem := 0
win.SetMatrix(pixel.IM)
for currentmode == menu {
win.Update()
win.Clear(colornames.Blue)
if win.JustPressed(pixelgl.KeyUp) {
currentitem++
if currentitem > 2 {
currentitem = 0
}
}
if win.JustPressed(pixelgl.KeyDown) {
currentitem--
if currentitem < 0 {
currentitem = 2
}
}
if win.JustPressed(pixelgl.KeyEnter) {
switch currentitem {
case 0:
currentmode = flying
case 1:
return
case 2:
return
}
}
win.SetSmooth(false)
menutxt.Clear()
fmt.Fprintf(menutxt, "%d\n", currentitem)
menutxt.Draw(win, pixel.IM)
}
}
if currentmode == mapper {
win.Update()
win.Clear(colornames.Pink)
if win.JustPressed(pixelgl.KeyQ) || win.JustPressed(pixelgl.KeyM) {
currentmode = flying
}
win.SetSmooth(false)
}
}
}
func main() {
pixelgl.Run(run)
}