Advertisement
Guest User

Sky Runner source code bug fixed

a guest
Feb 26th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.31 KB | None | 0 0
  1.  
  2. //EVERYTHING INCLUDING VECTOR MATHS AND CUSTOM PHYSICS WAS DONE BY FLUXO4, AKA PRASHANT KARN (076BEI026)
  3. //Copyright (c) FLUXO4
  4.  
  5. #pragma region INCLUSIONS
  6. #include <math.h>
  7. //#include <stdlib.h>
  8. //#include <stdarg.h>
  9. #include <GL/glut.h>
  10. //#include <stdio.h>
  11. //#include <conio.h>
  12. #include <stdbool.h>
  13. #pragma endregion
  14.  
  15. #pragma region SETTINGS
  16. bool startWithEasyMode = true;
  17.  
  18. //TIME AFTER WHICH A PLATFORM DISAPPEARS
  19. int platformLife = 800;
  20.  
  21. //HOW QUICKLY THE APOCALYPSE TRAVELS
  22. float apocaspeedd = 4;
  23. #pragma endregion
  24.  
  25. #pragma region ONSCREENTEXT
  26. //FOR PRINTING STRINGS ON THE SCREEN
  27. void output(GLfloat x, GLfloat y, char* text)
  28. {
  29. char* p;
  30.  
  31. glPushMatrix();
  32. glTranslatef(x, y, 0);
  33. for (p = text; *p; p++)
  34. glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  35. glPopMatrix();
  36. }
  37. #pragma endregion
  38.  
  39. #pragma region VECTORMATHS
  40. // CUSTOM MATHS STRUCTS - FluxO4(076BEI026)//
  41. //Vector3
  42. struct Vector3 {
  43. float x;
  44. float y;
  45. float z;
  46. };
  47.  
  48.  
  49. //Vector functions
  50. struct Vector3 setVec(GLfloat x, GLfloat y, GLfloat z) {
  51. struct Vector3 t;
  52. t.x = x; t.y = y; t.z = z;
  53. return t;
  54. }
  55. float dot(struct Vector3 a, struct Vector3 b) {
  56. return a.x * b.x + a.y * b.y + a.z * b.z;
  57. }
  58. struct Vector3 crs(struct Vector3 a, struct Vector3 b) {
  59. return setVec(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
  60. }
  61. struct Vector3 add(struct Vector3 a, struct Vector3 b) {
  62. return setVec(a.x + b.x, a.y + b.y, a.z + b.z);
  63. }
  64. struct Vector3 rev(struct Vector3 a) {
  65. return setVec(-a.x, -a.y, -a.z);
  66. }
  67. float mag(struct Vector3 a){
  68. return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
  69. }
  70. struct Vector3 nor(struct Vector3 a) {
  71. float m = mag(a);
  72. return setVec(a.x/m, a.y/m, a.z/m);
  73. }
  74. struct Vector3 mul(float a, struct Vector3 b) {
  75. return setVec(a*b.x, a*b.y, a*b.z);
  76. }
  77. float angle(struct Vector3 a, struct Vector3 b) {
  78. return acos(dot(a,b)/(mag(a)*mag(b)));
  79. }
  80. struct Vector3 zero() {
  81. return setVec(0.0,0.0,0.0);
  82. }
  83. struct Vector3 project(struct Vector3 a, struct Vector3 target) {
  84. return mul(dot(a, target)/mag(target),nor(target));
  85. }
  86. struct Vector3 projectOnPlaneY(struct Vector3 a) {
  87. return setVec(a.x, 0.0, a.z);
  88. }
  89. float signedAngle(struct Vector3 a, struct Vector3 b, struct Vector3 up) {
  90. if (angle(crs(a, b), up) <= 3.14159) {
  91. return angle(a,b);
  92. }
  93. else {
  94. return -angle(a, b);
  95. }
  96. }
  97.  
  98. #pragma endregion
  99.  
  100. #pragma region CONSTANTS
  101. // --- CONSTANTS --- //
  102.  
  103. //Camera Average distance from player
  104. float camDist = 5;
  105. //Coefficient of parallel friction
  106. float Cp = 0.95;
  107. //Coefficient of normal friction
  108. float Cn = 0.5;
  109. //Coefficient of air resistance
  110. float Ca = 0.99;
  111. //Forward acceleration
  112. float forAcc = 0.01f;
  113. //Forward acceleration speed-up multiplier
  114. float suM = 2;
  115. //Gravitational acceleration
  116. float gravAcc = -0.003f;
  117.  
  118. float jumpStrength = 0.12f;
  119.  
  120. float flyFactor = 0.5f;
  121.  
  122. //EASY MODE CONSTANTS
  123. float e_Cp = 0.9f;
  124. float e_forAcc = 0.03f;
  125. float e_flyFactor = 0.16f;
  126. float e_jumpStrength = 0.13f;
  127. #pragma endregion
  128.  
  129. #pragma region VARIABLES_AND_OTHER
  130. // --- CAMERA VECTORS --- //
  131.  
  132. //Camera Position vector
  133. struct Vector3 cpos;
  134. //Camera Forward vector
  135. struct Vector3 cfor;
  136.  
  137. // --- PLAYER VECTORS --- //
  138.  
  139. //Player Position vector
  140. struct Vector3 ppos;
  141. //Player Forward vector
  142. struct Vector3 pfor;
  143. //Player Up vector
  144. struct Vector3 pUp;
  145. //player right vector;
  146. struct Vector3 pRit;
  147. //Player Velocity vector
  148. struct Vector3 velocity;
  149.  
  150. // --- PLAYER PROPERTIES --- //
  151. float playerColliderRadius = 0.5;
  152. struct Vector3 playerHitBox;
  153.  
  154. // --- WORLD VECTORS --- //
  155. //Y axis
  156. struct Vector3 VecUp;
  157.  
  158. //gravity vector
  159. struct Vector3 gravity;
  160.  
  161.  
  162.  
  163. //Player horizontal angle
  164. float pangle = 0.0;
  165.  
  166. //Camera horizontal angle
  167. float hangle = 0.0;
  168. //Camera vertical angle
  169. float vangle = 30.0;
  170.  
  171. float startx, starty;
  172.  
  173. float Zdelta = 0;
  174.  
  175. //TIME BEFORE PLATFORMS START THEIR LIFE CYCLE
  176. int safeTime = 1000;
  177.  
  178. //IF SEED IS ANYTHING OTHER THAN 0, THE GAME WON'T BE RANDOMISED
  179. float platformSeed = 0;
  180. //TO GET THE INITIAL SEED IF platformSeed is 0
  181. float randomiser = 0;
  182. //PlayerColour, reddish, bright
  183. static GLfloat playerColor[4] =
  184. { 1.3, 0.2, 0.0, 1.0 };
  185.  
  186. static GLfloat red[4] =
  187. { 0.8, 0.2, 0.0, 1.0 };
  188.  
  189. int score = 0;
  190. int scoreOffset = 0;
  191. //Key index
  192. bool keypressed[6] = { false, false, false, false, false, false};
  193. // 0 = Forward, 1 = backward, 2 = left, 3 = right, 4 = jump (Space), 5 = speed up (Ctrl)
  194. bool easymode = false;
  195.  
  196. bool grounded = false;
  197. bool mouseMoving = false;
  198. bool groundedTest = false;
  199. bool gamelost = false;
  200.  
  201. float apocaspeed;
  202.  
  203. int distanceCovered = 0;
  204.  
  205. int framecount;
  206. #pragma endregion
  207.  
  208. #pragma region PLATFORMS
  209.  
  210. struct Platform {
  211. struct Vector3 pos;
  212. struct Vector3 size;
  213. int age;
  214. };
  215.  
  216. //20 PLATFORMS TOTAL AT ANY INSTANT
  217. struct Platform forms[20];
  218.  
  219. int numPlatforms = 20;
  220.  
  221. void createPlatform(struct Vector3 pos, struct Vector3 size) {
  222. if (numPlatforms < 20) {
  223.  
  224. forms[numPlatforms].pos = pos;
  225. forms[numPlatforms].size = size;
  226.  
  227.  
  228.  
  229. numPlatforms++;
  230. }
  231. }
  232.  
  233. void createPlatformFor(struct Vector3 pos, struct Vector3 size, int index) {
  234. forms[index].pos = pos;
  235. forms[index].size = size;
  236. forms[index].age = 0;
  237. }
  238.  
  239. void showPlatforms() {
  240. for (int i = 0; i < numPlatforms; i++) {
  241. static GLfloat pcolor[4] =
  242. { 0.0, 0.0, 0.0, 1.0 };
  243. pcolor[0] = 0.2 + forms[i].age / (platformLife *0.8);
  244. if (pcolor[2] >0.8 ) {
  245. pcolor[2] = 0.8;
  246. }
  247. pcolor[1] = 0.2;
  248. pcolor[2] = 0.2 + 1 - forms[i].age / (platformLife * 0.6);
  249. if (pcolor[2] < 0) {
  250. pcolor[2] = 0;
  251. }
  252.  
  253. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, pcolor);
  254. //if (framecount > safeTime)
  255. {
  256. //forms[i].age == framecount + (int)forms[i].pos.z;
  257. //printf("%d \n", framecount);
  258. forms[i].age = framecount + (int)forms[i].pos.z / (0.1f * apocaspeed);
  259.  
  260. if (forms[i].age < 0) {
  261. forms[i].age = 0;
  262. }
  263. if (forms[i].age > platformLife) {
  264. forms[i].pos.y = 20.0;
  265. }
  266. }
  267.  
  268. glPushMatrix();
  269. glTranslatef(forms[i].pos.x, forms[i].pos.y, forms[i].pos.z);
  270. glScalef(forms[i].size.x, forms[i].size.y, forms[i].size.z);
  271. glutSolidCube(1);
  272. glPopMatrix();
  273. }
  274. }
  275.  
  276.  
  277. void createNextPlatform() {
  278. int d = ((int)(sqrt(platformSeed) * 100) % (10)) * 2;
  279. createPlatform(setVec(d, 0, -platformSeed * 8), setVec(3, 0.5, 5));
  280. platformSeed++;
  281. }
  282.  
  283. void createNextPlatformFor(int index) {
  284. int d = ((int)(sqrt(platformSeed + randomiser) * 100) % (10)) * 2;
  285. int d2 = ((int)(sqrt(platformSeed + randomiser) * 1000) % (10)) * 2;
  286. int d3 = ((int)(sqrt(platformSeed + randomiser) * 10) % (10)) * 2;
  287. if (easymode) {
  288. createPlatformFor(setVec(d, d3*0.1f, -(platformSeed * 8 + d2)), setVec(6, 0.3, d2 + 3), index);
  289. }
  290. else {
  291. createPlatformFor(setVec(d, 0, -(platformSeed * 8 + d2)), setVec(3, 0.5, d2 + 3), index);
  292. }
  293.  
  294. platformSeed++;
  295.  
  296. }
  297.  
  298.  
  299. #pragma endregion
  300.  
  301. #pragma region COLLISION_PHYSICS
  302. void addCollisionForce(struct Platform other) {
  303. if (mag(add(ppos, rev(setVec(other.pos.x, other.pos.y, other.pos.z - Zdelta)))) > (mag(playerHitBox) + mag(other.size))) {
  304. return;
  305. }
  306. float xx = 0, yy = 0, zz = 0;
  307. xx = fabs(ppos.x - other.pos.x) - fabs(playerHitBox.x * 0.5f + other.size.x * 0.5f);
  308. yy = fabs(ppos.y - other.pos.y) - fabs(playerHitBox.y * 0.5f + other.size.y * 0.5f);
  309. zz = fabs(ppos.z - other.pos.z + Zdelta) - fabs(playerHitBox.z * 0.5f + other.size.z * 0.5f);
  310. if (xx < 0 && yy < 0 && zz < 0) {
  311. int aa = maxof(xx, yy, zz);
  312. //printf("xx = %f , yy = %f , zz = %f \n", xx, yy, zz);
  313. groundedTest = true;
  314. if (aa == 2) {
  315. velocity.y = 0;
  316. //grounded = true;
  317. if (ppos.y > other.pos.y) {
  318. ppos.y += -yy - 0.0001f;
  319. if (!grounded) {
  320. groundedTest = true;
  321. grounded = true;
  322. }
  323. }
  324. else {
  325. ppos.y += yy + 0.0001f;
  326. if (grounded) {
  327. grounded = false;
  328. }
  329. }
  330. }
  331. else if (aa == 3) {
  332. //printf("groundedTest in the Z axis \n");
  333. velocity.z = 0;
  334. if (ppos.z > other.pos.z + Zdelta) {
  335. ppos.z += -zz - 0.0001f;
  336. }
  337. else {
  338. ppos.z += zz + 0.0001f;
  339. }
  340. }
  341. else if (aa == 1) {
  342. //printf("groundedTest in the X axis \n");
  343. velocity.x = 0;
  344. if (ppos.x > other.pos.x) {
  345. ppos.x += -xx - 0.0001f;
  346. }
  347. else {
  348. ppos.x += xx + 0.0001f;
  349. }
  350. }
  351. }
  352. else {
  353. }
  354. }
  355.  
  356. int maxof(double a, double b, double c) {
  357. //printf("Comparing %f , %f , %f \n", a, b, c );
  358. //printf("Max value ");
  359. if (a > b && a > c) {
  360.  
  361. //printf("%f \n", a);
  362. return 1;
  363. }
  364. else if (b > a && b > c) {
  365.  
  366. //printf("%f \n", b);
  367. return 2;
  368. }
  369. else if (c > a && c > b) {
  370.  
  371. //printf("%f \n", c);
  372. return 3;
  373. }
  374. }
  375. #pragma endregion
  376.  
  377. #pragma region HELPER_FUNCTIONS
  378. void swap(float* a, float* b) {
  379. float t = *a;
  380. *a = *b;
  381. *b = t;
  382. }
  383.  
  384. void varInit() {
  385. framecount = 0;
  386. //Player horizontal angle
  387. pangle = 0.0;
  388. distanceCovered = 0;
  389. //Camera horizontal angle
  390. hangle = 0.0;
  391. //Camera vertical angle
  392. vangle = 30.0;
  393.  
  394. startx, starty;
  395.  
  396. Zdelta = 0;
  397.  
  398. scoreOffset = 0;
  399. score = 0;
  400. //TIME BEFORE PLATFORMS START THEIR LIFE CYCLE
  401. safeTime = 1000;
  402.  
  403. //IF SEED IS ANYTHING OTHER THAN 0, THE GAME WON'T BE RANDOMISED
  404. platformSeed = 0;
  405. //TO GET THE INITIAL SEED IF platformSeed is 0
  406. randomiser = 0;
  407.  
  408. keypressed[0] = false;
  409. keypressed[1] = false;
  410. keypressed[2] = false;
  411. keypressed[3] = false;
  412. keypressed[4] = false;
  413. keypressed[5] = false;
  414.  
  415. easymode = false;
  416.  
  417. grounded = false;
  418. mouseMoving = false;
  419. groundedTest = false;
  420. gamelost = false;
  421.  
  422. platformLife = 800;
  423.  
  424. apocaspeed = apocaspeedd;
  425. }
  426.  
  427. void vectorInit() {
  428.  
  429. velocity = zero();
  430. ppos = setVec(0.0f, 5.0f, 0.0f);
  431. pfor = setVec(0.0f, 0.0f, -1.0f);
  432.  
  433. cpos.x = ppos.x - cfor.x * camDist;
  434. cpos.z = ppos.z - cfor.z * camDist;
  435. cpos.y = ppos.y + 1 - cfor.y * 3;
  436.  
  437. VecUp = setVec(0.0, 1.0, 0.0);
  438.  
  439. playerHitBox = setVec(0.5f,1.2f,0.5f);
  440.  
  441. gravity = setVec(0.0, gravAcc, 0.0);
  442. }
  443.  
  444. void vectorUpdate() {
  445. pfor.x = sin(pangle * 3.14159 / 180);
  446. pfor.z = -cos(pangle * 3.14159 / 180);
  447.  
  448. pRit = nor(crs(VecUp, pfor));
  449.  
  450. cfor.x = sin(hangle * 3.14159 / 180);
  451. cfor.z = -cos(hangle * 3.14159 / 180);
  452.  
  453. cpos.x = ppos.x - cfor.x * camDist;
  454. cpos.z = ppos.z - cfor.z * camDist;
  455. cpos.y = ppos.y + 1 - cfor.y * 3;
  456. }
  457.  
  458. void LookAt(struct Vector3 pos, struct Vector3 targetPos, struct Vector3 up) {
  459. gluLookAt(pos.x, pos.y, pos.z,
  460. targetPos.x, targetPos.y, targetPos.z,
  461. up.x, up.y, up.z);
  462. }
  463.  
  464. void easy_mode() {
  465. if (!easymode) {
  466. easymode = true;
  467. swap(&Cp, &e_Cp);
  468. swap(&e_forAcc, &forAcc);
  469. swap(&e_flyFactor, &flyFactor);
  470. swap(&e_jumpStrength, &jumpStrength);
  471.  
  472. if (framecount > 20) {
  473. for (int i = 0; i < numPlatforms; i++) {
  474. if (mag(add(forms[i].pos, rev(ppos))) > 10) {
  475. int d3 = ((int)(sqrt(platformSeed + randomiser) * 10) % (10)) * 2;
  476. platformSeed++;
  477. forms[i].pos.y = d3 * 0.1f;
  478. forms[i].size.x = 6;
  479. forms[i].size.y = 0.3;
  480. }
  481. }
  482. }
  483. }
  484. }
  485.  
  486. void hard_mode() {
  487. if (easymode) {
  488. easymode = false;
  489. swap(&Cp, &e_Cp);
  490. swap(&e_forAcc, &forAcc);
  491. swap(&e_flyFactor, &flyFactor);
  492. swap(&e_jumpStrength, &jumpStrength);
  493. for (int i = 0; i < numPlatforms; i++) {
  494. if (mag(add(forms[i].pos, rev(ppos))) > 10) {
  495. forms[i].pos.y = 0;
  496. forms[i].size.x = 3;
  497. forms[i].size.y = 0.5;
  498. }
  499. }
  500. }
  501. }
  502.  
  503. static void init(void)
  504. {
  505. static GLfloat pos[4] =
  506. { 5.0, 5.0, 10.0, 0.0 };
  507.  
  508.  
  509. vectorInit();
  510. varInit();
  511. if (startWithEasyMode) {
  512. easy_mode();
  513. }
  514.  
  515. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  516. glEnable(GL_CULL_FACE);
  517. glEnable(GL_LIGHTING);
  518. glEnable(GL_LIGHT0);
  519. glEnable(GL_DEPTH_TEST);
  520.  
  521. time_t t;
  522. srand((unsigned)time(&t));
  523. randomiser = (rand() % 500) * 0.01f;
  524.  
  525. createPlatformFor(setVec(0, 0, 0), setVec(6, 1, 6), 0);
  526.  
  527. for (int i = 1; i < 20; i++) {
  528. createNextPlatformFor(i);
  529. //createPlatformFor(setVec(0, 0, -platformSeed * 8 ), setVec(3, 0.5, 5), i);
  530. platformSeed++;
  531. }
  532.  
  533. showPlatforms;
  534.  
  535. glEnable(GL_NORMALIZE);
  536. }
  537.  
  538.  
  539. #pragma endregion
  540.  
  541. #pragma region CHARACTER_MODEL
  542. void createLeg() {
  543.  
  544. glPushMatrix();
  545. glRotatef(90.0, 0.0, 1.0, 0.0);
  546.  
  547. glPushMatrix();
  548. glTranslatef(0.0,0.1,0.0);
  549. glScalef(0.2,0.2,0.2);
  550. glRotatef(90.0,0.0,0.0,1.0);
  551. glutSolidTetrahedron();
  552. glPopMatrix();
  553.  
  554. glPushMatrix();
  555. glRotatef(180.0, 0.0, 1.0, 0.0);
  556. glPushMatrix();
  557. glTranslatef(0.0, -0.125, 0.0);
  558. glScalef(0.2, 0.5, 0.2);
  559. glRotatef(-90.0, 0.0, 0.0, 1.0);
  560. glutSolidTetrahedron();
  561. glPopMatrix();
  562. glPopMatrix();
  563.  
  564. glPopMatrix();
  565.  
  566. }
  567.  
  568. void createBody() {
  569.  
  570. glPushMatrix();
  571. glTranslatef(0.0,0.7,0.0);
  572. glRotatef(90.0, 0.0, 1.0, 0.0);
  573.  
  574. glPushMatrix();
  575. glTranslatef(0.0, 0.1, 0.0);
  576. glScalef(0.2, 0.2, 0.4);
  577. glRotatef(90.0, 0.0, 0.0, 1.0);
  578. glutSolidTetrahedron();
  579. glPopMatrix();
  580.  
  581. glPushMatrix();
  582. glRotatef(180.0, 0.0, 1.0, 0.0);
  583. glPushMatrix();
  584. glTranslatef(0.0, -0.13, 0.0);
  585. glScalef(0.2, 0.5, 0.4);
  586. glRotatef(-90.0, 0.0, 0.0, 1.0);
  587. glutSolidTetrahedron();
  588. glPopMatrix();
  589. glPopMatrix();
  590.  
  591. glPopMatrix();
  592.  
  593. }
  594.  
  595. void createCharacter() {
  596. float animFactor = framecount * 0.2;
  597. if (keypressed[5]) {
  598. animFactor = animFactor * 2;
  599. }
  600. if (!grounded) {
  601. animFactor = animFactor * 2;
  602. }
  603.  
  604. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, playerColor);
  605.  
  606. glPushMatrix();
  607. if (grounded && (keypressed[0] || keypressed[1])) {
  608. glRotatef(40 * sin(animFactor), 1.0, 0.0, 0.0);
  609. }
  610. else if (!grounded && (keypressed[0] || keypressed[1])) {
  611. glRotatef(-50 + 40 * sin(animFactor), 1.0, 0.0, 0.0);
  612. }
  613. else if (!grounded) {
  614. glRotatef(-50, 1.0, 0.0, 0.0);
  615. }
  616. glPushMatrix();
  617. glTranslatef(0.2,0.0,0.0);
  618. glRotatef(5.0, 0.0, 0.0, 1.0);
  619. createLeg();
  620. glPopMatrix();
  621. glPopMatrix();
  622.  
  623. glPushMatrix();
  624. if (grounded && (keypressed[0] || keypressed[1])) {
  625. glRotatef(-40 * sin(animFactor), 1.0, 0.0, 0.0);
  626. }
  627. else if (!grounded && (keypressed[0] || keypressed[1])) {
  628. glRotatef(-60 - 40 * sin(animFactor), 1.0, 0.0, 0.0);
  629. }
  630. else if (!grounded) {
  631. glRotatef(-60, 1.0, 0.0, 0.0);
  632. }
  633. glPushMatrix();
  634. glTranslatef(-0.2, 0.0, 0.0);
  635. glRotatef(-5.0, 0.0, 0.0, 1.0);
  636. createLeg();
  637. glPopMatrix();
  638. glPopMatrix();
  639.  
  640. glPushMatrix();
  641. if (grounded && keypressed[0]) {
  642. glRotatef(-40 * sin(animFactor), 1.0, 0.0, 0.0);
  643. glTranslatef(0.0, 0.0, 0.55 * sin(animFactor));
  644. }
  645. else if (!grounded) {
  646. glRotatef(90, 0.0, 0.0, 1.0);
  647. glTranslatef(0.5, -1.15, 0.0);
  648. }
  649. glPushMatrix();
  650. glTranslatef(0.5, 0.65, 0.0);
  651. glScalef(0.6,1.0,0.6);
  652. glRotatef(30.0, 0.0, 0.0, 1.0);
  653. createLeg();
  654. glPopMatrix();
  655. glPopMatrix();
  656.  
  657. glPushMatrix();
  658. if (grounded && keypressed[0]) {
  659. glRotatef(40 * sin(animFactor), 1.0, 0.0, 0.0);
  660. glTranslatef(0.0, 0.0, - 0.55 * sin(animFactor));
  661. }
  662. else if (!grounded) {
  663. glRotatef(-90, 0.0, 0.0, 1.0);
  664. glTranslatef(-0.5, -1.15, 0.0);
  665. }
  666. glPushMatrix();
  667. glTranslatef(-0.5, 0.65, 0.0);
  668. glScalef(0.6, 1.0, 0.6);
  669. glRotatef(-30.0, 0.0, 0.0, 1.0);
  670. createLeg();
  671. glPopMatrix();
  672. glPopMatrix();
  673.  
  674. glPushMatrix();
  675. if (grounded && keypressed[0]) {
  676. glTranslatef(0.0, 0.03 * sin(animFactor * 2.0), 0.0);
  677. }
  678. createBody();
  679. glPushMatrix();
  680. glTranslatef(0.0, 1.4, 0.0);
  681. glScalef(0.4, 0.4, 0.4);
  682. glutSolidIcosahedron();
  683. glPopMatrix();
  684. glPopMatrix();
  685.  
  686. }
  687. #pragma endregion
  688.  
  689. #pragma region MAIN_LOOP_FUNCTIONS
  690. void changeSize(int w, int h)
  691. {
  692. if (h == 0)
  693. h = 1;
  694. float ratio = w * 1.0 / h;
  695.  
  696. // Use the Projection Matrix
  697. glMatrixMode(GL_PROJECTION);
  698.  
  699. // Reset Matrix
  700. glLoadIdentity();
  701.  
  702. // Set the viewport to be the entire window
  703. glViewport(0, 0, w, h);
  704.  
  705. // Set the correct perspective.
  706. gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  707.  
  708. // Get Back to the Modelview
  709. glMatrixMode(GL_MODELVIEW);
  710. }
  711.  
  712. void renderScene(void)
  713. {
  714.  
  715. // Clear Color and Depth Buffers
  716.  
  717. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  718.  
  719. // Reset transformations
  720. glLoadIdentity();
  721.  
  722. /*GLfloat pos[4] =
  723. { cpos.x + 5.0, cpos.x + 5.0, cpos.x + 10.0, 0.0 };
  724. glLightfv(GL_LIGHT0, GL_POSITION, pos);*/
  725.  
  726. LookAt(cpos, add(cpos, cfor), setVec(0.0f,1.0f,0.0f));
  727.  
  728. // --- DEBUG CUBE --- //
  729. /*glPushMatrix();
  730. glTranslatef(ppos.x,ppos.y,ppos.z);
  731. glScalef(playerHitBox.x, playerHitBox.y, playerHitBox.z);
  732. glutWireCube(1);
  733. glPopMatrix();*/
  734.  
  735. glPushMatrix();
  736. glTranslatef(ppos.x, ppos.y-0.3f, ppos.z);
  737. glScalef(0.5f, 0.5f, 0.5f);
  738. if (!grounded) {
  739. glRotatef(-20, 1.0, 0.0, 0.0);
  740. }
  741. glPushMatrix();
  742. glRotatef(-pangle, 0.0, 1.0, 0.0);
  743. createCharacter();
  744. glPopMatrix();
  745. glPopMatrix();
  746.  
  747. glPushMatrix();
  748. glTranslatef(0.0,0.0, - Zdelta);
  749. showPlatforms();
  750. glPopMatrix();
  751.  
  752. framecount++;
  753.  
  754.  
  755. glPushAttrib(GL_ENABLE_BIT);
  756. glDisable(GL_DEPTH_TEST);
  757. glDisable(GL_LIGHTING);
  758. glMatrixMode(GL_PROJECTION);
  759. glPushMatrix();
  760. glLoadIdentity();
  761. gluOrtho2D(0, 1600, -900, 150);
  762. glMatrixMode(GL_MODELVIEW);
  763. glPushMatrix();
  764. glLoadIdentity();
  765.  
  766. //score = (int)(-Zdelta) + scoreOffset;
  767. char scorec[6];
  768. sprintf_s(scorec, 6, "%d", score);
  769.  
  770. output(10, 10, scorec);
  771.  
  772. if (ppos.y < -10.0) {
  773. output(200, -200, "OOPS! YOU FELL!");
  774. output(500, -700, "PRESS 'R'");
  775. }
  776. glPopMatrix();
  777. glMatrixMode(GL_PROJECTION);
  778. glPopMatrix();
  779. glPopAttrib();
  780. glMatrixMode(GL_MODELVIEW);
  781.  
  782. {
  783. glPushAttrib(GL_ENABLE_BIT);
  784. glDisable(GL_DEPTH_TEST);
  785. glDisable(GL_LIGHTING);
  786. glMatrixMode(GL_PROJECTION);
  787. glPushMatrix();
  788. glLoadIdentity();
  789. gluOrtho2D(0, 6400, -3600, 150);
  790. glMatrixMode(GL_MODELVIEW);
  791. glPushMatrix();
  792. glLoadIdentity();
  793.  
  794. if (easymode) {
  795. output(4500, 10, "EASY (TOGGLE WITH 'M')");
  796. }
  797. else {
  798. output(4500, 10, "HARD (TOGGLE WITH 'M')");
  799. }
  800.  
  801. glPopMatrix();
  802. glMatrixMode(GL_PROJECTION);
  803. glPopMatrix();
  804. glPopAttrib();
  805. glMatrixMode(GL_MODELVIEW);
  806. }
  807. glutSwapBuffers();
  808.  
  809. }
  810.  
  811. void idleScene(void) {
  812. if (ppos.y < -40.0) {
  813. //Sleep(10);
  814. //printf("SCORE IS %d", (int)(-Zdelta));
  815. if (!gamelost) {
  816. gamelost = true;
  817. }
  818.  
  819. return;
  820. }
  821.  
  822. Sleep(20);
  823. //Keypresses
  824.  
  825. if ((int)(-Zdelta) > distanceCovered) {
  826. if (easymode) {
  827. score = score + ((int)(-Zdelta) - distanceCovered);
  828. }
  829. else {
  830. if (framecount % 2 == 0) {
  831. score = score + (int)(((int)(-Zdelta) - distanceCovered) * 2);
  832. }
  833. else {
  834. score = score + (int)(((int)(-Zdelta) - distanceCovered));
  835. }
  836. }
  837. distanceCovered = (int)(-Zdelta);
  838. }
  839.  
  840. if (framecount % 200 == 0 && platformLife > 400) {
  841. platformLife = (int)platformLife * (0.995f);
  842. apocaspeed = apocaspeed * (1.02f);
  843. }
  844. if (!easymode && framecount % 20 == 1) {
  845. scoreOffset += 1;
  846. }
  847.  
  848. if (grounded) {
  849. float f = forAcc;
  850. if (keypressed[5]) {
  851. f = forAcc * suM;
  852. }
  853.  
  854. if (keypressed[0]) {
  855. velocity.x += pfor.x * f;
  856. velocity.z += pfor.z * f;
  857. }
  858. else if (keypressed[1]) {
  859. velocity.x -= pfor.x * forAcc;
  860. velocity.z -= pfor.z * forAcc;
  861. }
  862. }
  863. else {
  864. if (keypressed[0]) {
  865. velocity.x += pfor.x * forAcc * flyFactor;
  866. velocity.z += pfor.z * forAcc * flyFactor;
  867. }
  868. else if (keypressed[1]) {
  869. velocity.x -= pfor.x * forAcc * flyFactor;
  870. velocity.z -= pfor.z * forAcc * flyFactor;
  871. }
  872. }
  873.  
  874.  
  875.  
  876. if (keypressed[2]) {
  877. pangle -= 3;
  878. }
  879. else if (keypressed[3]) {
  880. pangle += 3;
  881. }
  882.  
  883.  
  884.  
  885.  
  886. //friction
  887. if (grounded) {
  888. struct Vector3 t2 = projectOnPlaneY(velocity);
  889. struct Vector3 t = add(mul(Cp, project(t2, pfor)), mul(Cn, project(t2, pRit)));
  890. velocity = setVec(t.x, velocity.y, t.z);
  891. }
  892. else {
  893. velocity = mul(Ca, velocity);
  894. }
  895.  
  896. //collision
  897. for (int i = 0; i < numPlatforms; i++) {
  898. addCollisionForce(forms[i]);
  899. }
  900.  
  901. for (int i = 0; i < numPlatforms; i++) {
  902. if (forms[i].pos.z - Zdelta > 40) {
  903. createNextPlatformFor(i);
  904. break;
  905. }
  906. }
  907.  
  908. if (!groundedTest) {
  909. if (grounded) {
  910. grounded = false;
  911. }
  912. }
  913. else {
  914. groundedTest = false;
  915. }
  916.  
  917. //jump and gravity
  918. if (grounded) {
  919. if (keypressed[4]) {
  920. if (keypressed[5]) {
  921. velocity.y = jumpStrength * 1.5f;;
  922. }
  923. else {
  924. velocity.y =jumpStrength;
  925. }
  926. }
  927. }
  928. else {
  929. velocity = add(velocity, gravity);
  930. }
  931.  
  932.  
  933. if (!mouseMoving) {
  934. if(fabs(pangle - hangle) > 0.5f)
  935. hangle = hangle + (pangle - hangle) * 0.1f;
  936. }
  937.  
  938. vectorUpdate();
  939.  
  940. //Velocity
  941. //ppos = add(ppos, velocity);
  942. //Velocity to centre z
  943. ppos = add(ppos, setVec(velocity.x, velocity.y, 0.0));
  944. Zdelta = Zdelta + velocity.z;
  945.  
  946. glutPostRedisplay();
  947. }
  948. #pragma endregion
  949.  
  950. #pragma region MOUSE_AND_KEYS
  951. void Keys(unsigned char key, int x, int y)
  952. {
  953. if (glutGetModifiers())
  954. {
  955. if (GLUT_ACTIVE_SHIFT) {
  956. keypressed[5] = true;
  957. }
  958. else {
  959. keypressed[5] = false;
  960. }
  961. }
  962. else {
  963. keypressed[5] = false;
  964. }
  965.  
  966.  
  967. if (key == 'a' || key == 'A')
  968. keypressed[2] = true;
  969. //break;
  970. if (key == 'd' || key == 'D')
  971. keypressed[3] = true;
  972. //break;
  973. if (key == 'w' || key == 'W')
  974. keypressed[0] = true;
  975. //break;
  976. if (key == 's' || key == 'S') {
  977. keypressed[1] = true;
  978. keypressed[4] = false;
  979. keypressed[5] = false;
  980. }
  981. //break;
  982. if (key == ' ')
  983. keypressed[4] = true;
  984. //break;
  985. if (key == '0')
  986. keypressed[5] = true;
  987. //break;
  988. if (key == 'f')
  989. keypressed[5] = true;
  990.  
  991. if (key == 'm') {
  992. if (!easymode) {
  993. easy_mode();
  994. }
  995. else {
  996. hard_mode();
  997. }
  998. }
  999.  
  1000. if (key == 'r') {
  1001. if (ppos.y < -10) {
  1002. init();
  1003. }
  1004. }
  1005.  
  1006. if (key == 27) {
  1007. exit(0);
  1008. }
  1009.  
  1010. }
  1011.  
  1012. void SKeys(int key, int xx, int yy)
  1013. {
  1014. if (glutGetModifiers())
  1015. {
  1016. if (GLUT_ACTIVE_SHIFT) {
  1017. keypressed[5] = true;
  1018. }
  1019. else {
  1020. keypressed[5] = false;
  1021. }
  1022. }
  1023. else {
  1024. keypressed[5] = false;
  1025. }
  1026.  
  1027. if (key == GLUT_KEY_LEFT) {
  1028. keypressed[2] = true;
  1029. }
  1030.  
  1031.  
  1032. if (key == GLUT_KEY_RIGHT) {
  1033. keypressed[3] = true;
  1034. }
  1035.  
  1036. if (key == GLUT_KEY_UP) {
  1037. keypressed[0] = true;
  1038. }
  1039.  
  1040. if (key == GLUT_KEY_DOWN) {
  1041. keypressed[1] = true;
  1042. keypressed[4] = false;
  1043. keypressed[5] = false;
  1044. }
  1045. }
  1046.  
  1047. void KeysUp(unsigned char key, int xx, int yy) {
  1048. if (glutGetModifiers())
  1049. {
  1050. if (GLUT_ACTIVE_SHIFT) {
  1051. keypressed[5] = true;
  1052. }
  1053. else {
  1054. keypressed[5] = false;
  1055. }
  1056. }
  1057. else {
  1058. keypressed[5] = false;
  1059. }
  1060.  
  1061.  
  1062.  
  1063. if(key == 'a' || key == 'A')
  1064. keypressed[2] = false;
  1065. //break;
  1066. if (key == 'd' || key == 'D')
  1067. keypressed[3] = false;
  1068. //break;
  1069. if (key == 'w' || key == 'W')
  1070. keypressed[0] = false;
  1071. //break;
  1072. if (key == 's' || key == 'S') {
  1073. keypressed[1] = false;
  1074. keypressed[4] = false;
  1075. keypressed[5] = false;
  1076. }
  1077. //break;
  1078. if (key == ' ')
  1079. keypressed[4] = false;
  1080. //break;
  1081. if (key == '0')
  1082. keypressed[5] = false;
  1083. //break;
  1084. if (key == 'f')
  1085. keypressed[5] = false;
  1086.  
  1087.  
  1088.  
  1089. }
  1090.  
  1091. void SKeysUp(int key, int xx, int yy) {
  1092. if (glutGetModifiers())
  1093. {
  1094. if (GLUT_ACTIVE_SHIFT) {
  1095. keypressed[5] = true;
  1096. }
  1097. else {
  1098. keypressed[5] = false;
  1099. }
  1100. }
  1101. else {
  1102. keypressed[5] = false;
  1103. }
  1104.  
  1105.  
  1106. if (key == GLUT_KEY_LEFT) {
  1107. keypressed[2] = false;
  1108. }
  1109.  
  1110.  
  1111. if (key == GLUT_KEY_RIGHT) {
  1112. keypressed[3] = false;
  1113. }
  1114.  
  1115. if (key == GLUT_KEY_UP) {
  1116. keypressed[0] = false;
  1117. }
  1118.  
  1119. if (key == GLUT_KEY_DOWN) {
  1120. keypressed[1] = false;
  1121. keypressed[4] = false;
  1122. keypressed[5] = false;
  1123. }
  1124. }
  1125.  
  1126. static void mouse(int button, int state, int x, int y)
  1127. {
  1128. startx = x;
  1129. starty = y;
  1130.  
  1131. if (button == GLUT_LEFT_BUTTON) {
  1132. if (state == GLUT_DOWN) {
  1133.  
  1134. mouseMoving = true;
  1135. }
  1136. if (state == GLUT_UP) {
  1137. mouseMoving = false;
  1138. }
  1139. }
  1140.  
  1141. if (button == GLUT_RIGHT_BUTTON) {
  1142. if (state == GLUT_DOWN) {
  1143. keypressed[4] = true;
  1144. }
  1145. if (state == GLUT_UP) {
  1146. keypressed[4] = false;
  1147. }
  1148. }
  1149. }
  1150.  
  1151. static void motion(int x, int y) {
  1152. if (mouseMoving) {
  1153. hangle = hangle + (x - startx) / 2;
  1154. vangle = vangle - (y - starty) / 2;
  1155. /*if (hangle >= 180) { hangle = hangle - 180; }
  1156. if (hangle < -180) { hangle = hangle + 180; }*/
  1157.  
  1158. cfor.x = sin(hangle * 3.14159 / 180);
  1159. cfor.z = -cos(hangle * 3.14159 / 180);
  1160. cfor.y = sin(vangle * 3.14159 / 180);
  1161.  
  1162. cpos.x = ppos.x - cfor.x * camDist;
  1163. cpos.z = ppos.z - cfor.z * camDist;
  1164. cpos.y = ppos.y + 1 - cfor.y * 3;
  1165. //hangle2 = hangle2 + (y - starty) / 10;
  1166. startx = x;
  1167. starty = y;
  1168. }
  1169.  
  1170. glutPostRedisplay();
  1171. }
  1172. #pragma endregion
  1173.  
  1174. int main(int argc, char** argv)
  1175. {
  1176.  
  1177. // init GLUT and create window
  1178.  
  1179. glutInit(&argc, argv);
  1180.  
  1181. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  1182.  
  1183. glutInitWindowPosition(0, 20);
  1184. glutInitWindowSize(1120, 630);
  1185. glutCreateWindow("Sky-runner");
  1186.  
  1187.  
  1188. init();
  1189. glutIdleFunc(idleScene);
  1190. glutDisplayFunc(renderScene);
  1191. glutReshapeFunc(changeSize);
  1192. glutMouseFunc(mouse);
  1193. glutMotionFunc(motion);
  1194. glutKeyboardFunc(Keys);
  1195. glutSpecialFunc(SKeys);
  1196. glutKeyboardUpFunc(KeysUp);
  1197. glutSpecialUpFunc(SKeysUp);
  1198.  
  1199.  
  1200. glEnable(GL_DEPTH_TEST);
  1201.  
  1202. glutMainLoop();
  1203.  
  1204. return 1;
  1205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement