-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cx
515 lines (439 loc) · 16.9 KB
/
main.cx
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import "gl"
import "glfw"
import "os"
// ***************************
// STRUCTS
// ***************************
type Window struct {
Height i32
Width i32
}
type Sprite struct{
x1 f32
x2 f32
y1 f32
y2 f32
spriteSheet i32
}
type Bird struct{
x f32
y f32
vx f32
vy f32
gravity f32
radius f32
}
type Pipe struct {
x f32
y f32
vx f32
vy f32
h f32
w f32
topPipe bool
}
// ***************************
// Declare Game Components -- REFRACTOR REFRACTOR!!!
// ***************************
// Sprites Map
var spriteMap i32
var spriteMapWidth i32 = 400
var spriteMapHeight i32 = 128
// Bird Animation and Sprites
var frame f64
var birdScaleX f32 = 0.05
var birdScaleY f32 = 0.05
var spriteBird_Down Sprite
var spriteBird_Mid Sprite
var spriteBird_Up Sprite
var birdAnimation [2]Sprite
// Pusheen Cat Animation and Sprites
var spritePusheen0 Sprite
var spritePusheen1 Sprite
var spritePusheen2 Sprite
var spritePusheen3 Sprite
var pusheenScaleX f32 = 0.07
var pusheenScaleY f32 = 0.07
var pusheenAnimation [3]Sprite
// Pipe Sprite
var spritePipeBody Sprite
var spritePipeTop Sprite
var pSkullHeightTop f32 = -0.07
var pSkullHeightBottom f32 = 0.07
// Sky Sprite
var spriteSky Sprite
var skyScaleX f32 = 1.0
var skyScaleY f32 = 1.0
var skyStartX f32 = 0.0
var skyStartY f32 = 1.0
// Grass Sprite
var spriteGrass Sprite
var grassScaleX f32 = 2.0
var grassScaleY f32 = 0.1
var grassStartX f32 = 1.0
var grassStartY f32 = -0.9
// Background Sprite
var spriteBackground Sprite
var bgScaleX f32 = 1.0
var bgScaleY f32 = 0.4
var bgStartX f32 = 0.0
var bgStartY f32 = -0.40
// Bird Movements
var bird Bird
var birdGravity f32 = 0.001
var birdStartX f32 = -0.75
var birdStartY f32 = 0.5
var birdRadius f32 = 0.2
// Pipe
var randNum f32
var pipeGap f32 = 0.5
var pipeWidth f32 = 0.10
var pipeSpeed f32 = -0.009
var pTop [3]Pipe // Change array size to change number of pipes
var pBottom [3]Pipe
// *****************************************************************************************************
// WINDOW SETTING AND OPENGL SETTINGS
// *****************************************************************************************************
func initGlfw (mw Window) {
// Initiate glfw (Graphic Library Framework)
// to manage OpenGl Window Context
glfw.Init()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.CreateWindow("window", mw.Width, mw.Height, "PixelBird-cx")
glfw.MakeContextCurrent("window")
}
func initOpenGL() (program i32) {
gl.Init()
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
program = gl.CreateProgram()
gl.LinkProgram(program)
}
// *****************************************************************************************************
// HELPER FUNCTIONS
// *****************************************************************************************************
// Random Number between 0.3 to 1.3
func randNumGenerator() (rNum f32){
rNum = f32.div(i32.f32(i32.rand(3, 13)), 10.0)
}
// *****************************************************************************************************
// COLLISION DETECTION -- Have to fix... need to incorporate radius of bird/cat
// *****************************************************************************************************
func collision(bird Bird, p Pipe) (){
// ********************************
// bird COLLIDING WITH TOP PIPE
// ********************************
if p.topPipe == true {
if bird.x >= p.x && bird.x <= (p.x + p.w) && f32.sub(f32.add(bird.y,1.0),pSkullHeightTop) >= f32.sub(2.0, p.h) {
reset()
str.print("Top Pipe")
//f32.print(f32.add(bird.y, 1.0))
}
// ********************************
// bird COLLIDING WITH BOTTOM PIPE
// ********************************
} else if p.topPipe == false {
if bird.x >= p.x && bird.x <= (p.x + p.w) && f32.sub(f32.add(bird.y, 1.0),pSkullHeightBottom) <= p.h {
reset()
str.print("Bottom Pipe")
//f32.print(f32.add(bird.y, 1.0))
}
}
}
func reset() {
// ****************
// RESET BIRD
// ****************
bird.x = birdStartX
bird.y = 0.0
bird.vx = 0.0
bird.vy = 0.0
bird.gravity = birdGravity
// ****************
// RESET PIPE
// ****************
var pStartX f32 = 0.5
var p2pDist f32 = 0.7
for k := 0; k < len(pTop); k++{
pTop[k].x = pStartX
pBottom[k].x = pStartX
pStartX = pStartX + p2pDist
}
}
// *****************************************************************************************************
// RENDER AND DRAW
// Figure out how to scale appropriately...
// *****************************************************************************************************
// gl.Vertex2f(x, y)
// gl.Vertex3f(x, y, z)
// gl.Color3f(r, g, b)
// gl.Color4f(r, g, b, alpha)
// http://www.december.com/html/spec/colorper.html
func draw(xCoord f32, yCoord f32, scaleX f32, scaleY f32, texture i32, sprite Sprite) () {
// https://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work
gl.LoadIdentity()
gl.Translatef(xCoord, yCoord, 0.0) // x-coordinates, y-coordinates, z-coordinates
gl.Rotatef(0.0, 0.0, 0.0, 1.0) // Rotate along the z-axis
gl.BindTexture(gl.TEXTURE_2D, spriteMap)
gl.Scalef(scaleX, scaleY, 0.0)
gl.Begin(gl.QUADS)
gl.TexCoord2f(sprite.x1, sprite.y2)
gl.Vertex3f(-1.0, -1.0, 0.0)
gl.TexCoord2f(sprite.x1, sprite.y1)
gl.Vertex3f(-1.0, 1.0, 0.0)
gl.TexCoord2f(sprite.x2, sprite.y1)
gl.Vertex3f(1.0, 1.0, 0.0)
gl.TexCoord2f(sprite.x2, sprite.y2)
gl.Vertex3f(1.0, -1.0, 0.0)
gl.End()
}
// *****************************************************************************************************
// SPRITE LOADER
// *****************************************************************************************************
func scale (x i32, max i32) (out f32) {
out = f32.div(i32.f32(x), i32.f32(max))
}
func setSprite(s Sprite, x1 i32, x2 i32, y1 i32, y2 i32, W i32, H i32, spriteMap i32) (spriteRegion Sprite) {
// x1 = bottom left vertices of the Sprite
// x2 = bottom right vertices of the Sprite
// y1 = top left vertices of the Sprite
// y2 = top right vertices of the Sprite
// W = Width of the entire Sprite Map
// H = Height of the entire Sprite Map
s.x1 = scale(x1, W) // x1 divided by Sprite Map Width
s.x2 = scale(x2, W) // x2 divided by Sprite Map Width
s.y1 = scale(y1, H) // y1 divided by Sprite Map Height
s.y2 = scale(y2, H) // y2 divided by Sprite Map Height
s.spriteSheet = spriteMap
spriteRegion = s
}
// *****************************************************************************************************
// KEYBOARD EVENTS - http://www.glfw.org/docs/latest/group__keys.html
// *****************************************************************************************************
// SPACEBAR == 32 and UP KEY == 265
func keyPressed(window str, key i32, scancode i32, action i32, mods i32) () {
if key == 32 && action == 1 || key == 265 && action == 1{
bird.vy = f32.sub(bird.vy, -0.02)
}
}
// *****************************************************************************************************
// GAME STATES
// *****************************************************************************************************
func startGame(mw Window) () {
// ***************************
// DECLARATION
// ***************************
var program i32
var wd str
// ***************************
// Init
// ***************************
initGlfw(mw)
program = initOpenGL()
var pStartX f32 = 0.5 // Initial point of where pipe starts
var p2pDist f32 = 0.7 // The distance between each pipes.
var topPipeHeight f32
var bottomPipeHeight f32
// ***************************
// Initialize Instances
// OpenGL uses X, Y, Z coordinates where vertex relative to center of window ranges between -1 and 1.
// Center of screen would have coordinates of (0, 0)
// ***************************
bird = Bird{
x: birdStartX,
y: birdStartY,
vx: 0.00, // Velocity in x direction
vy: 0.00, // Velocity in y direction
gravity: birdGravity,
radius: birdRadius}
// **********************
// TOP AND BOTTOM PIPES
// **********************
for k := 0; k < len(pTop); k++{
randNum = randNumGenerator()
topPipeHeight = randNum
bottomPipeHeight = f32.sub(2.0,f32.add(topPipeHeight, pipeGap))
pTop[k] = Pipe{
x: pStartX,
y: 1.0,
vx: pipeSpeed,
vy: 0.00,
h: topPipeHeight, // Pipe Height
w: pipeWidth,
topPipe: true}
pBottom[k] = Pipe{
x: pStartX,
y: -1.0,
vx: pipeSpeed,
vy: 0.00,
h: bottomPipeHeight,
w: pipeWidth,
topPipe: false}
pStartX = pStartX + p2pDist
}
// ***************************
// LOAD SPRITE MAP
// ***************************
wd = os.GetWorkingDirectory()
spriteMap = gl.NewTexture(str.concat(wd, "textures/texture.png"))
// ********************************
// RETRIEVE SPRITE REGION FOR PUSHEEN CAT
// https://www.deviantart.com/chinzapep/art/Go-on-a-diet-Nyan-Cat-306288626 - Credit ChinZaPep for sprite
// ********************************
spritePusheen0 = setSprite(spritePusheen0, 167, 232, 17, 62, spriteMapWidth, spriteMapHeight, spriteMap)
spritePusheen1 = setSprite(spritePusheen1, 167, 227, 67, 113, spriteMapWidth, spriteMapHeight, spriteMap)
spritePusheen2 = setSprite(spritePusheen2, 248, 313, 17, 63, spriteMapWidth, spriteMapHeight, spriteMap)
spritePusheen3 = setSprite(spritePusheen3, 248, 313, 67, 113, spriteMapWidth, spriteMapHeight, spriteMap)
pusheenAnimation[0] = spritePusheen0
pusheenAnimation[1] = spritePusheen1
pusheenAnimation[2] = spritePusheen2
pusheenAnimation[3] = spritePusheen3
// ********************************
// RETRIEVE SPRITE REGION FOR BIRD
// ********************************
// Recall that setSprite(s Sprite, x1 i32, x2 i32, y1 i32, y2 i32, W i32, H i32, spriteMap i32)
//Uncomment to load Zombie
/*
spriteBird_Down = setSprite(spriteBird_Down, 136, 153, 0, 12, spriteMapWidth, spriteMapHeight, spriteMap)
spriteBird_Mid = setSprite(spriteBird_Mid, 154, 170, 0, 12, spriteMapWidth, spriteMapHeight, spriteMap)
spriteBird_Up = setSprite(spriteBird_Up, 171, 187, 0, 12, spriteMapWidth, spriteMapHeight, spriteMap)
birdAnimation[0] = spriteBird_Down
birdAnimation[1] = spriteBird_Mid
birdAnimation[2] = spriteBird_Up
*/
// ********************************
// RETRIEVE SPRITE REGION FOR BACKGROUND, SKY AND GRASS
// ********************************
spriteBackground = setSprite(spriteBackground, 0, 136, 0, 43, spriteMapWidth, spriteMapHeight, spriteMap)
spriteSky = setSprite(spriteSky, 0, 1, 0, 1, spriteMapWidth, spriteMapHeight, spriteMap)
spriteGrass = setSprite(spriteGrass, 0, 165, 43, 54, spriteMapWidth, spriteMapHeight, spriteMap)
// ********************************
// RETRIEVE SPRITE REGION FOR PIPE
// ********************************
spritePipeBody = setSprite(spritePipeBody, 136, 157, 16, 19, spriteMapWidth, spriteMapHeight, spriteMap)
spritePipeTop = setSprite(spritePipeTop, 192, 216, 0, 14, spriteMapWidth, spriteMapHeight, spriteMap)
// ***************************
// Main Loop for render and gameplay
// ***************************
for bool.not(glfw.ShouldClose("window")) {
gl.UseProgram(program)
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Enable(gl.TEXTURE_2D)
// *********************
// KEYBOARD COORDINATES
// *********************
glfw.SetKeyCallback("window", "keyPressed")
// *********************
// COLLISION DETECTION
// *********************
for k:= 0; k < len(pTop); k++{
collision(bird, pTop[k])
collision(bird, pBottom[k])
}
// ***********************************************************
// Draw Background, Sky and Grass
// ***********************************************************
draw(bgStartX, bgStartY, bgScaleX, bgScaleY, spriteMap, spriteBackground)
draw(skyStartX, skyStartY, skyScaleX, skyScaleY, spriteMap, spriteSky)
if f32.lteq(grassStartX, -1.0){
grassStartX = 1.0
} else {
grassStartX = f32.add(grassStartX, -0.01)
}
draw(grassStartX, grassStartY, grassScaleX, grassScaleY, spriteMap, spriteGrass)
// ***********************************************************
// DRAW PIPE
// ***********************************************************
randNum = randNumGenerator()
for k := 0; k < len(pTop); k++{
topPipeHeight = randNum
bottomPipeHeight = f32.sub(2.0,f32.add(topPipeHeight, pipeGap))
// ******************
// DRAW TOP PIPE
// ******************
if f32.lteq(pTop[k].x, f32.sub(-1.0, pipeWidth)) { // If pipe goes pass left side of screen, restart onto the right
pTop[k].x = 1.0
pTop[k].h = topPipeHeight
// **********
// DEBUGGING
// **********
//str.print("Top Height")
//f32.print(topPipeHeight)
} else {
pTop[k].x = f32.add(pTop[k].x, pTop[k].vx)
}
draw(pTop[k].x, pTop[k].y, pTop[k].w, pTop[k].h, spriteMap, spritePipeBody)
draw(pTop[k].x, f32.sub(pTop[k].y, pTop[k].h), f32.add(pTop[k].w, 0.01), pSkullHeightTop, spriteMap, spritePipeTop) // Drawing the skull on top of the pipe
// ******************
// DRAW BOTTOM PIPE
// ******************
if f32.lteq(pBottom[k].x, f32.sub(-1.0, pipeWidth)) {
pBottom[k].x = 1.0
pBottom[k].h = bottomPipeHeight
// **********
// DEBUGGING
// **********
//str.print("Bottom Height")
//f32.print(bottomPipeHeight)
} else {
pBottom[k].x = f32.add(pBottom[k].x, pBottom[k].vx)
}
draw(pBottom[k].x, pBottom[k].y, pBottom[k].w, pBottom[k].h, spriteMap, spritePipeBody)
draw(pBottom[k].x, f32.add(pBottom[k].y, pBottom[k].h), f32.add(pBottom[k].w, 0.01), pSkullHeightBottom, spriteMap, spritePipeTop) // Drawing the skull on top of the pipe
}
// ***********************************************************
// Draw Bird or Pusheen
// ***********************************************************
// FLAPPING MOTION
frame = f64.add(frame, 0.1D)
// For Zombie Bird
/*if f64.gt(frame, 2.0D) {
frame = 0.0D
}*/
// For Pusheen Cat
if f64.gt(frame, 3.0D) {
frame = 0.0D
}
// HITTING FLOOR
if f32.lteq(bird.y, -1.0) {
//bird.vy = f32.abs(bird.vy) // If bird hits floor, bounce back
reset()
// HITTING FLOOR
} else if f32.gteq(bird.y, 1.0) {
//bird.vy = (-1.0 * bird.vy) // If bird hits top, bounce to bottom
reset()
// Let gravity do the work on the bird
} else {
bird.vy = f32.sub(bird.vy, bird.gravity)
}
bird.y = f32.add(bird.y, bird.vy)
// Draw Pusheen Cat
draw(bird.x, bird.y, pusheenScaleX, pusheenScaleY, spriteMap, pusheenAnimation[f64.i32(frame)])
// Draw Zombie Bird - Uncomment to draw zombie bird
//draw(bird.x, bird.y, birdScaleX, birdScaleY, spriteMap, birdAnimation[f64.i32(frame)])
// ***********************************************************
// Refresh Components
// PollEvents will listen for key events such as keystrokes, etc
// SwapBuffer: GLFW uses double buffer. It kind of means that there
// is two canvas. The front buffer is the canvas that is being displayed
// while the back buffer is the invisible canvas you are drawing/rendering to.
// You would put this invisible canvas to the front for display once you are
// ready by calling SwapBuffer.
// ***********************************************************
glfw.PollEvents()
glfw.SwapBuffers("window")
}
}
// *****************************************************************************************************
// MAIN FUNCTION
// *****************************************************************************************************
func main () {
var myWindow Window
myWindow = Window{
Height: 748,
Width: 1024}
str.print("Let there be light!")
startGame(myWindow)
}