-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage00.c
547 lines (418 loc) · 17.9 KB
/
stage00.c
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <assert.h>
#include <nusys.h>
#include <ultra64.h>
#include "graphic.h"
#include "main.h"
#include "stage00.h"
#include "skybox.h"
#include "cube_render.h"
#include "debug.h"
#include "testcube.h"
#include "meteor.h"
#include "collisionmath.h"
#include "projectile_texture.h"
#define PLAYER_HEALTH_MAX 7
#define DAMAGE_TIMER_MAX 2 //Number of seconds between damage pings
int playerHealth = PLAYER_HEALTH_MAX; //Number of available hearts
float damageTimer = 0.0f;
int damageTimerActive = 0; //Boolean (N64 does not support bool). Determines whether the damage cooldown is active
Vec3d cameraPos = {0.0f, 0.0f, -50.0f};
Vec3d cameraTarget = {0, 0, 0};
Vec3d cameraForward = {0.0f, 0.0f, 1.0f};
Vec3d cameraEulerAngles = {0.0f, 0.0f, 0.0f};
Vec3d cameraUp = {0.0f, 1.0f, 0.0f};
Vec3d cameraLeft = {1.0f, 0.0f, 0.0f};
//booleans
char pitch = 0;
char roll = 0;
char yaw = 0;
char skyboxOn = 1;
char yawDirection = 1;
char rollDirection = 1;
char pitchDirection = 1;
float yawVelocity = 127;
float pitchVelocity = 127;
float rollVelocity = 127;
float forwardVelocity = 0;
float forwardMin = -200;
float forwardMax = 500;
Vec3d playerVelocity = {
0,
0,
0
};
float forwardAcceleration = 500;
Mtx skyboxTranslation;
float yawRotation[4][4] = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}
};
float pitchRotation[4][4] = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}
};
float rollRotation[4][4] = {
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f}
};
Mtx curMeteorRotation;
Mtx curMeteorTranslation;
#define METEOR_COUNT 100
int curMeteorCount = METEOR_COUNT;
struct Meteor MeteorList[METEOR_COUNT];
Mtx MeteorTransformationStack[METEOR_COUNT];
int test_rotations[METEOR_COUNT];
Vec3d test_positions[METEOR_COUNT];
Projectile projectiles[MAX_PROJECTILES];
int projectile_count = 0;
int score = 0;
// the 'setup' function
void initStage00() {
// the advantage of initializing these values here, rather than statically, is
// that if you switch stages/levels, and later return to this stage, you can
// call this function to reset these values.
// In the older version of C used by the N64 compiler (roughly C89); variables
// must be declared at the top of a function or block scope. This is an example
// of using block scope to declare a variable in the middle of a function.
generateMeteors();
initialize_projectile_array(projectiles);
}
// the 'update' function
void updateGame00() {
// read controller input from controller 1 (index 0)
nuContDataGetEx(contdata, 0);
//Re-Generate meteors if all are destroyed
if(curMeteorCount <= 0){
generateMeteors();
curMeteorCount = METEOR_COUNT;
}
//Check the damage cooldown timer
if(damageTimerActive == 1 && damageTimer >= DAMAGE_TIMER_MAX){
damageTimerActive = 0;
damageTimer = 0.0f;
}
float joystickMagnitude = sqrtf(contdata[0].stick_y * contdata[0].stick_y + contdata[0].stick_x * contdata[0].stick_x);
if (contdata[0].button & A_BUTTON){
//forwardVelocity = forwardVelocity + forwardAcceleration;
playerVelocity.x += cameraForward.x * forwardAcceleration *.016;
playerVelocity.y += cameraForward.y * forwardAcceleration *.016;
playerVelocity.z += cameraForward.z * forwardAcceleration *.016;
//playerVelocity = applyInertDamp(cameraForward, playerVelocity, 75, 1);
}
if (contdata[0].button & B_BUTTON){
//forwardVelocity = forwardVelocity - forwardAcceleration;
playerVelocity.x += -1 * cameraForward.x * forwardAcceleration * .016;
playerVelocity.y += -1 * cameraForward.y * forwardAcceleration * .016;
playerVelocity.z += -1 * cameraForward.z * forwardAcceleration * .016;
//playerVelocity = applyInertDamp(cameraForward, playerVelocity, 75, -1);
}
float velocityMagnitude = sqrtf(playerVelocity.x * playerVelocity.x + playerVelocity.y * playerVelocity.y
+ playerVelocity.z * playerVelocity.z);
if(velocityMagnitude > forwardMax){
Vec3d normalizedVelocity = {
playerVelocity.x / velocityMagnitude,
playerVelocity.y / velocityMagnitude,
playerVelocity.z / velocityMagnitude
};
playerVelocity.x = normalizedVelocity.x * forwardMax;
playerVelocity.y = normalizedVelocity.y * forwardMax;
playerVelocity.z = normalizedVelocity.z * forwardMax;
}
if(contdata[0].button & U_CBUTTONS){
playerVelocity.x = 0;
playerVelocity.y = 0;
playerVelocity.z = 0;
}
roll = 0;
if (contdata[0].button & L_CBUTTONS){
roll = 1;
rollDirection = -1;
}
if (contdata[0].button & R_CBUTTONS){
roll = 1;
rollDirection = 1;
}
if(contdata[0].trigger & Z_TRIG){
fire_projectile(cameraPos, cameraForward, projectiles, &projectile_count);
}
yaw = 0;
int stick_x_mag = sqrtf(contdata[0].stick_x * contdata[0].stick_x);
int stick_y_mag = sqrtf(contdata[0].stick_y * contdata[0].stick_y);
if(contdata[0].stick_x !=0 && (stick_x_mag > 40)){
//debug_printf("x: %x\n", contdata[0].stick_x);
yaw = 1;
yawDirection = contdata[0].stick_x / sqrtf(contdata[0].stick_x * contdata[0].stick_x);
}
pitch = 0;
if(contdata[0].stick_y !=0 && (stick_y_mag > 40)){
pitch = 1;
//debug_printf("y: %x\n", contdata[0].stick_y);
pitchDirection = contdata[0].stick_y / sqrtf(contdata[0].stick_y * contdata[0].stick_y);
}
// update square rotations
cameraPos.x = cameraPos.x + (playerVelocity.x * .016);
cameraPos.y = cameraPos.y + (playerVelocity.y * .016);
cameraPos.z = cameraPos.z + (playerVelocity.z * .016);
//Detect collisions
for(int i = 0; i < METEOR_COUNT; i++){
float meteorRadius = MeteorList[i].radius * MeteorList[i].scale;
Vec3d meteorPosition = MeteorList[i].position;
if(MeteorList[i].enabled == 1) {
if (isColliding(50, meteorRadius, cameraPos, MeteorList[i].position)) {
VelocityOut collisionOutput;
Vec3d oppositeVelocity = {
-1 * playerVelocity.x,
-1 * playerVelocity.y,
-1 * playerVelocity.z
};
collisionOutput = respondCollision(50, cameraPos, playerVelocity, 770, meteorRadius, meteorPosition,
oppositeVelocity, 5000);
playerVelocity = collisionOutput.first;
if(damageTimerActive == 0){
playerHealth = playerHealth - 1;
damageTimerActive = 1;
}
}
}
}
//Detect projectile-meteor collisions, update health data
for(int i = 0; i < MAX_PROJECTILES; i++){
for(int j = 0; j < METEOR_COUNT; j++){
if(projectiles[i].isEliminated == 1 || MeteorList[j].enabled == 0){ //Calculate nothing if either is disabled
continue;
}
if(isColliding(projectiles[i].radius, MeteorList[j].radius, projectiles[i].pos, MeteorList[j].position)){
projectiles[i].isEliminated = 1;
MeteorList[j].health = MeteorList[j].health - 2;
debug_printf("Meteor %i hit\n", j);
debug_printf("Meteor %i health: %i\n", j, MeteorList[j].health);
if(MeteorList[j].health <= 0){
MeteorList[j].enabled = 0;
curMeteorCount = curMeteorCount - 1;
score = 100 * MeteorList[j].scale;
debug_printf("Meteor %i destroyed\n", j);
debug_printf("Score\n", j);
}
}
}
}
cameraTarget.x = cameraPos.x + cameraForward.x;
cameraTarget.y = cameraPos.y + cameraForward.y;
cameraTarget.z = cameraPos.z + cameraForward.z;
updateMeteors();
handle_projectiles(projectiles, cameraForward, cameraPos, &projectile_count);
//debug_printf("Velocity: %f %f %f \n", playerVelocity.x, playerVelocity.y, playerVelocity.z);
//debug_printf("Magnitude: %f Forward Max: %f\n", velocityMagnitude, forwardMax);
//debug_printf("Position: %f %f %f \n", cameraPos.x, cameraPos.y, cameraPos.z);
}
// the 'draw' function
void makeDL00() {
unsigned short perspNorm;
GraphicsTask * gfxTask;
// switch the current graphics task
// also updates the displayListPtr global variable
gfxTask = gfxSwitchTask();
// prepare the RCP for rendering a graphics task
gfxRCPInit();
// clear the color framebuffer and Z-buffer, similar to glClear()
gfxClearCfb();
//gfxSetBackgroundColor();
//debug_printf("Rom initialized");
//Perform the camera vector rotations
if(yaw) {
guRotateF(yawRotation, -1 * yawVelocity * yawDirection * .016, cameraUp.x, cameraUp.y, cameraUp.z);
guMtxXFMF(yawRotation, cameraForward.x, cameraForward.y, cameraForward.z,
&cameraForward.x, &cameraForward.y, & cameraForward.z);
guMtxXFMF(yawRotation, cameraLeft.x, cameraLeft.y, cameraLeft.z,
&cameraLeft.x, &cameraLeft.y, &cameraLeft.z);
}
if(pitch) {
guRotateF(pitchRotation, -1 * pitchVelocity * pitchDirection * .016, cameraLeft.x, cameraLeft.y, cameraLeft.z);
guMtxXFMF(pitchRotation, cameraForward.x, cameraForward.y, cameraForward.z,
&cameraForward.x, &cameraForward.y, & cameraForward.z);
guMtxXFMF(pitchRotation, cameraUp.x, cameraUp.y, cameraUp.z,
&cameraUp.x, &cameraUp.y, &cameraUp.z);
}
if(roll) {
guRotateF(rollRotation, -1 * rollVelocity * rollDirection * .016, cameraForward.x, cameraForward.y, cameraForward.z);
guMtxXFMF(rollRotation, cameraLeft.x, cameraLeft.y, cameraLeft.z,
&cameraLeft.x, &cameraLeft.y, & cameraLeft.z);
guMtxXFMF(rollRotation, cameraUp.x, cameraUp.y, cameraUp.z,
&cameraUp.x, &cameraUp.y, &cameraUp.z);
}
guNormalize(&cameraForward.x, &cameraForward.y, &cameraForward.z);
// initialize the projection matrix, similar to glPerspective() or glm::perspective()
guPerspective(&gfxTask->projection, &perspNorm, FOVY, ASPECT, NEAR_PLANE,
FAR_PLANE, 1.0);
// Our first actual displaylist command. This writes the command as a value at
// the tail of the current display list, and we increment the display list
// tail pointer, ready for the next command to be written.
// As for what this command does... it's just required when using a perspective
// projection. Try pasting 'gSPPerspNormalize' into google if you want more
// explanation, as all the SDK documentation has been placed online by
// hobbyists and is well indexed.
gSPPerspNormalize(displayListPtr++, perspNorm);
// initialize the modelview matrix, similar to gluLookAt() or glm::lookAt()
guLookAt(&gfxTask->modelview, cameraPos.x, cameraPos.y,
cameraPos.z, cameraTarget.x, cameraTarget.y,
cameraTarget.z, cameraUp.x, cameraUp.y, cameraUp.z);
// load the projection matrix into the matrix stack.
// given the combination of G_MTX_flags we provide, effectively this means
// "replace the projection matrix with this new matrix"
gSPMatrix(
displayListPtr++,
// we use the OS_K0_TO_PHYSICAL macro to convert the pointer to this matrix
// into a 'physical' address as required by the RCP
OS_K0_TO_PHYSICAL(&(gfxTask->projection)),
// these flags tell the graphics microcode what to do with this matrix
// documented here: http://n64devkit.square7.ch/tutorial/graphics/1/1_3.htm
G_MTX_PROJECTION | // using the projection matrix stack...
G_MTX_LOAD | // don't multiply matrix by previously-top matrix in stack
G_MTX_NOPUSH // don't push another matrix onto the stack before operation
);
gSPMatrix(displayListPtr++,
OS_K0_TO_PHYSICAL(&(gfxTask->modelview)),
// similarly this combination means "replace the modelview matrix with this new matrix"
G_MTX_MODELVIEW | G_MTX_NOPUSH | G_MTX_LOAD
);
guTranslate(&skyboxTranslation, cameraPos.x, cameraPos.y, cameraPos.z);
gSPMatrix(displayListPtr++, &skyboxTranslation, G_MTX_MODELVIEW | G_MTX_PUSH | G_MTX_MUL);
if(skyboxOn > -1) {
gSPDisplayList(displayListPtr++, skybox_dl);
}
gSPPopMatrix(displayListPtr++, G_MTX_MODELVIEW);
//gSPDisplayList(displayListPtr++, projectile_dl);
drawMeteors();
draw_projectiles(projectiles);
gDPPipeSync(displayListPtr++);
//debug_printf("%i FPS\n", nuScGetFrameRate());
// mark the end of the display list
gDPFullSync(displayListPtr++);
gSPEndDisplayList(displayListPtr++);
//draw the star particles;
//TODO draw the star particles
//This is commented out as it causes an emulator crash
//TODO fix star particle drawing
//updateStarParticles(cameraPos, cameraForward);
// assert that the display list isn't longer than the memory allocated for it,
// otherwise we would have corrupted memory when writing it.
// isn't unsafe memory access fun?
// this could be made safer by instead asserting on the displaylist length
// every time the pointer is advanced, but that would add some overhead.
assert(displayListPtr - gfxTask->displayList < MAX_DISPLAY_LIST_COMMANDS);
// create a graphics task to render this displaylist and send it to the RCP
nuGfxTaskStart(
gfxTask->displayList,
(int)(displayListPtr - gfxTask->displayList) * sizeof (Gfx),
NU_GFX_UCODE_F3DEX2, // load the 'F3DEX' version graphics microcode, which runs on the RCP to process this display list
NU_SC_SWAPBUFFER // tells NuSystem to immediately display the frame on screen after the RCP finishes rendering it
);
}
// the nusystem callback for the stage, called once per frame
void stage00(int pendingGfx)
{
// produce a new displaylist (unless we're running behind, meaning we already
// have the maximum queued up)
if(pendingGfx < 1 && playerHealth > 0) {
makeDL00();
}else{
//TODO GameOver screen
}
// update the state of the world for the next frame
updateGame00();
}
void generateMeteors(){
//Generate values for each meteor.
for(int i = 0; i < METEOR_COUNT; i++){
MeteorList[i].position.x = RAND(6000) - 3000;
MeteorList[i].position.y = RAND(6000) - 3000;
MeteorList[i].position.z = RAND(6000) - 3000;
MeteorList[i].Turning_Axis.x = RAND(2) - 1; //Range 1 - 101 inclusive, shifted to 0 - 100, normalized
MeteorList[i].Turning_Axis.y = RAND(2) - 1;
MeteorList[i].Turning_Axis.z = RAND(2) - 1;
MeteorList[i].rotation_angle = 13;
MeteorList[i].scale = RAND(3);
MeteorList[i].radius = 215;
MeteorList[i].enabled = 1;
MeteorList[i].health = 10 * MeteorList[i].scale;
//debug_printf("Angle of rotation: %i\n", test_rotations[i]);
//debug_printf("X: %f\n", test_positions[i].x);
}
}
void updateMeteors(){
for(int i = 0; i < METEOR_COUNT; i++){
MeteorList[i].rotation_angle = ((MeteorList[i].rotation_angle + 1) % 360); //Rotate the meteors.
}
}
void drawMeteors(){
gSPDisplayList(displayListPtr++, asteroid_material);
for(int i = 0; i < METEOR_COUNT; i++){
struct Meteor curMeteor = MeteorList[i];
if(MeteorList[i].enabled == 1) {
//debug_printf("X: %f Y: %f Z %f \n", curMeteor.x, curMeteor.y, curMeteor.z);
//debug_printf("Angle of rotation: %f\n", curMeteor.rotation_angle);
int rotation_x = curMeteor.Turning_Axis.x * curMeteor.rotation_angle;
int rotation_y = curMeteor.Turning_Axis.y * curMeteor.rotation_angle;
int rotation_z = curMeteor.Turning_Axis.z * curMeteor.rotation_angle;
guPosition(&MeteorTransformationStack[i], rotation_x, rotation_y, rotation_z, curMeteor.scale,
curMeteor.position.x, curMeteor.position.y, curMeteor.position.z);
gSPMatrix(displayListPtr++, OS_K0_TO_PHYSICAL(&MeteorTransformationStack[i]), G_MTX_MODELVIEW | G_MTX_PUSH);
gSPDisplayList(displayListPtr++, asteroid_Sphere_mesh);
gSPPopMatrix(displayListPtr++, G_MTX_MODELVIEW);
}
}
}
Vec3d applyInertDamp(Vec3d forward, Vec3d velocity, float rate, int forwardScale){
float velMag = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y + velocity.z * velocity.z);
if(velMag == 0){
velMag = 1;
}
//Scale the forward vector by the current velocity magnitude
Vec3d desiredVelocity = {
forwardScale * forward.x * velMag,
forwardScale * forward.y * velMag,
forwardScale * forward.z * velMag
};
//Take the difference between the two vectors
Vec3d difference = {
velocity.x - desiredVelocity.x,
velocity.y - desiredVelocity.y,
velocity.z - desiredVelocity.z
};
//normalize the difference then scale it by the rate
float difMag = sqrtf(difference.x * difference.x + difference.y * difference.y + difference.z * difference.z);
if(difMag == 0){
difMag = 1;
}
Vec3d change = {
(difference.x / difMag) * rate * .016,
(difference.y / difMag) * rate * .016,
(difference.z / difMag) * rate * .016
};
//update the input velocity
Vec3d output = {
velocity.x + change.x,
velocity.y + change.y,
velocity.z + change.z
};
return output;
}
void draw_projectiles(Projectile * projectiles){
for(int i = 0; i < MAX_PROJECTILES; i++){
if(!projectiles[i].isEliminated) {
gSPMatrix(displayListPtr++, OS_K0_TO_PHYSICAL(&(projectiles[i].translation)), G_MTX_MODELVIEW | G_MTX_PUSH
| G_MTX_MUL);
//gSPMatrix(displayListPtr++, OS_K0_TO_PHYSICAL(&(projectiles[i].transform)), G_MTX_MODELVIEW | G_MTX_PUSH
//| G_MTX_MUL);
gSPDisplayList(displayListPtr++, projectile_dl);
gSPPopMatrix(displayListPtr++, G_MTX_MODELVIEW);
//gSPPopMatrix(displayListPtr++, G_MTX_MODELVIEW);
}
}
}