Advertisement
Guest User

The sky runner final

a guest
Feb 26th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.32 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.  
  473. platformSeed += -numPlatforms;
  474. for (int i = 0; i < numPlatforms; i++) {
  475. if (mag(add(forms[i].pos, rev(ppos))) > 10) {
  476. int d3 = ((int)(sqrt(platformSeed + randomiser) * 10) % (10)) * 2;
  477. platformSeed++;
  478. forms[i].pos.y = d3 * 0.1f;
  479. forms[i].size.x = 6;
  480. forms[i].size.y = 0.3;
  481. }
  482. }
  483.  
  484. }
  485. }
  486.  
  487. void hard_mode() {
  488. if (easymode) {
  489. easymode = false;
  490. swap(&Cp, &e_Cp);
  491. swap(&e_forAcc, &forAcc);
  492. swap(&e_flyFactor, &flyFactor);
  493. swap(&e_jumpStrength, &jumpStrength);
  494. for (int i = 0; i < numPlatforms; i++) {
  495. if (mag(add(forms[i].pos, rev(ppos))) > 10) {
  496. forms[i].pos.y = 0;
  497. forms[i].size.x = 3;
  498. forms[i].size.y = 0.5;
  499. }
  500. }
  501. }
  502. }
  503.  
  504. static void init(void)
  505. {
  506. static GLfloat pos[4] =
  507. { 5.0, 5.0, 10.0, 0.0 };
  508.  
  509.  
  510. vectorInit();
  511. varInit();
  512. if (startWithEasyMode) {
  513. easy_mode();
  514. }
  515.  
  516. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  517. glEnable(GL_CULL_FACE);
  518. glEnable(GL_LIGHTING);
  519. glEnable(GL_LIGHT0);
  520. glEnable(GL_DEPTH_TEST);
  521.  
  522. time_t t;
  523. srand((unsigned)time(&t));
  524. randomiser = (rand() % 500) * 0.01f;
  525.  
  526. createPlatformFor(setVec(0, 0, 0), setVec(6, 1, 6), 0);
  527.  
  528. for (int i = 1; i < 20; i++) {
  529. createNextPlatformFor(i);
  530. //createPlatformFor(setVec(0, 0, -platformSeed * 8 ), setVec(3, 0.5, 5), i);
  531. platformSeed++;
  532. }
  533.  
  534. showPlatforms;
  535.  
  536. glEnable(GL_NORMALIZE);
  537. }
  538.  
  539.  
  540. #pragma endregion
  541.  
  542. #pragma region CHARACTER_MODEL
  543. void createLeg() {
  544.  
  545. glPushMatrix();
  546. glRotatef(90.0, 0.0, 1.0, 0.0);
  547.  
  548. glPushMatrix();
  549. glTranslatef(0.0,0.1,0.0);
  550. glScalef(0.2,0.2,0.2);
  551. glRotatef(90.0,0.0,0.0,1.0);
  552. glutSolidTetrahedron();
  553. glPopMatrix();
  554.  
  555. glPushMatrix();
  556. glRotatef(180.0, 0.0, 1.0, 0.0);
  557. glPushMatrix();
  558. glTranslatef(0.0, -0.125, 0.0);
  559. glScalef(0.2, 0.5, 0.2);
  560. glRotatef(-90.0, 0.0, 0.0, 1.0);
  561. glutSolidTetrahedron();
  562. glPopMatrix();
  563. glPopMatrix();
  564.  
  565. glPopMatrix();
  566.  
  567. }
  568.  
  569. void createBody() {
  570.  
  571. glPushMatrix();
  572. glTranslatef(0.0,0.7,0.0);
  573. glRotatef(90.0, 0.0, 1.0, 0.0);
  574.  
  575. glPushMatrix();
  576. glTranslatef(0.0, 0.1, 0.0);
  577. glScalef(0.2, 0.2, 0.4);
  578. glRotatef(90.0, 0.0, 0.0, 1.0);
  579. glutSolidTetrahedron();
  580. glPopMatrix();
  581.  
  582. glPushMatrix();
  583. glRotatef(180.0, 0.0, 1.0, 0.0);
  584. glPushMatrix();
  585. glTranslatef(0.0, -0.13, 0.0);
  586. glScalef(0.2, 0.5, 0.4);
  587. glRotatef(-90.0, 0.0, 0.0, 1.0);
  588. glutSolidTetrahedron();
  589. glPopMatrix();
  590. glPopMatrix();
  591.  
  592. glPopMatrix();
  593.  
  594. }
  595.  
  596. void createCharacter() {
  597. float animFactor = framecount * 0.2;
  598. if (keypressed[5]) {
  599. animFactor = animFactor * 2;
  600. }
  601. if (!grounded) {
  602. animFactor = animFactor * 2;
  603. }
  604.  
  605. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, playerColor);
  606.  
  607. glPushMatrix();
  608. if (grounded && (keypressed[0] || keypressed[1])) {
  609. glRotatef(40 * sin(animFactor), 1.0, 0.0, 0.0);
  610. }
  611. else if (!grounded && (keypressed[0] || keypressed[1])) {
  612. glRotatef(-50 + 40 * sin(animFactor), 1.0, 0.0, 0.0);
  613. }
  614. else if (!grounded) {
  615. glRotatef(-50, 1.0, 0.0, 0.0);
  616. }
  617. glPushMatrix();
  618. glTranslatef(0.2,0.0,0.0);
  619. glRotatef(5.0, 0.0, 0.0, 1.0);
  620. createLeg();
  621. glPopMatrix();
  622. glPopMatrix();
  623.  
  624. glPushMatrix();
  625. if (grounded && (keypressed[0] || keypressed[1])) {
  626. glRotatef(-40 * sin(animFactor), 1.0, 0.0, 0.0);
  627. }
  628. else if (!grounded && (keypressed[0] || keypressed[1])) {
  629. glRotatef(-60 - 40 * sin(animFactor), 1.0, 0.0, 0.0);
  630. }
  631. else if (!grounded) {
  632. glRotatef(-60, 1.0, 0.0, 0.0);
  633. }
  634. glPushMatrix();
  635. glTranslatef(-0.2, 0.0, 0.0);
  636. glRotatef(-5.0, 0.0, 0.0, 1.0);
  637. createLeg();
  638. glPopMatrix();
  639. glPopMatrix();
  640.  
  641. glPushMatrix();
  642. if (grounded && keypressed[0]) {
  643. glRotatef(-40 * sin(animFactor), 1.0, 0.0, 0.0);
  644. glTranslatef(0.0, 0.0, 0.55 * sin(animFactor));
  645. }
  646. else if (!grounded) {
  647. glRotatef(90, 0.0, 0.0, 1.0);
  648. glTranslatef(0.5, -1.15, 0.0);
  649. }
  650. glPushMatrix();
  651. glTranslatef(0.5, 0.65, 0.0);
  652. glScalef(0.6,1.0,0.6);
  653. glRotatef(30.0, 0.0, 0.0, 1.0);
  654. createLeg();
  655. glPopMatrix();
  656. glPopMatrix();
  657.  
  658. glPushMatrix();
  659. if (grounded && keypressed[0]) {
  660. glRotatef(40 * sin(animFactor), 1.0, 0.0, 0.0);
  661. glTranslatef(0.0, 0.0, - 0.55 * sin(animFactor));
  662. }
  663. else if (!grounded) {
  664. glRotatef(-90, 0.0, 0.0, 1.0);
  665. glTranslatef(-0.5, -1.15, 0.0);
  666. }
  667. glPushMatrix();
  668. glTranslatef(-0.5, 0.65, 0.0);
  669. glScalef(0.6, 1.0, 0.6);
  670. glRotatef(-30.0, 0.0, 0.0, 1.0);
  671. createLeg();
  672. glPopMatrix();
  673. glPopMatrix();
  674.  
  675. glPushMatrix();
  676. if (grounded && keypressed[0]) {
  677. glTranslatef(0.0, 0.03 * sin(animFactor * 2.0), 0.0);
  678. }
  679. createBody();
  680. glPushMatrix();
  681. glTranslatef(0.0, 1.4, 0.0);
  682. glScalef(0.4, 0.4, 0.4);
  683. glutSolidIcosahedron();
  684. glPopMatrix();
  685. glPopMatrix();
  686.  
  687. }
  688. #pragma endregion
  689.  
  690. #pragma region MAIN_LOOP_FUNCTIONS
  691. void changeSize(int w, int h)
  692. {
  693. if (h == 0)
  694. h = 1;
  695. float ratio = w * 1.0 / h;
  696.  
  697. // Use the Projection Matrix
  698. glMatrixMode(GL_PROJECTION);
  699.  
  700. // Reset Matrix
  701. glLoadIdentity();
  702.  
  703. // Set the viewport to be the entire window
  704. glViewport(0, 0, w, h);
  705.  
  706. // Set the correct perspective.
  707. gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  708.  
  709. // Get Back to the Modelview
  710. glMatrixMode(GL_MODELVIEW);
  711. }
  712.  
  713. void renderScene(void)
  714. {
  715.  
  716. // Clear Color and Depth Buffers
  717.  
  718. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  719.  
  720. // Reset transformations
  721. glLoadIdentity();
  722.  
  723. /*GLfloat pos[4] =
  724. { cpos.x + 5.0, cpos.x + 5.0, cpos.x + 10.0, 0.0 };
  725. glLightfv(GL_LIGHT0, GL_POSITION, pos);*/
  726.  
  727. LookAt(cpos, add(cpos, cfor), setVec(0.0f,1.0f,0.0f));
  728.  
  729. // --- DEBUG CUBE --- //
  730. /*glPushMatrix();
  731. glTranslatef(ppos.x,ppos.y,ppos.z);
  732. glScalef(playerHitBox.x, playerHitBox.y, playerHitBox.z);
  733. glutWireCube(1);
  734. glPopMatrix();*/
  735.  
  736. glPushMatrix();
  737. glTranslatef(ppos.x, ppos.y-0.3f, ppos.z);
  738. glScalef(0.5f, 0.5f, 0.5f);
  739. if (!grounded) {
  740. glRotatef(-20, 1.0, 0.0, 0.0);
  741. }
  742. glPushMatrix();
  743. glRotatef(-pangle, 0.0, 1.0, 0.0);
  744. createCharacter();
  745. glPopMatrix();
  746. glPopMatrix();
  747.  
  748. glPushMatrix();
  749. glTranslatef(0.0,0.0, - Zdelta);
  750. showPlatforms();
  751. glPopMatrix();
  752.  
  753. framecount++;
  754.  
  755.  
  756. glPushAttrib(GL_ENABLE_BIT);
  757. glDisable(GL_DEPTH_TEST);
  758. glDisable(GL_LIGHTING);
  759. glMatrixMode(GL_PROJECTION);
  760. glPushMatrix();
  761. glLoadIdentity();
  762. gluOrtho2D(0, 1600, -900, 150);
  763. glMatrixMode(GL_MODELVIEW);
  764. glPushMatrix();
  765. glLoadIdentity();
  766.  
  767. //score = (int)(-Zdelta) + scoreOffset;
  768. char scorec[6];
  769. sprintf_s(scorec, 6, "%d", score);
  770.  
  771. output(10, 10, scorec);
  772.  
  773. if (ppos.y < -10.0) {
  774. output(200, -200, "OOPS! YOU FELL!");
  775. output(500, -700, "PRESS 'R'");
  776. }
  777. glPopMatrix();
  778. glMatrixMode(GL_PROJECTION);
  779. glPopMatrix();
  780. glPopAttrib();
  781. glMatrixMode(GL_MODELVIEW);
  782.  
  783. {
  784. glPushAttrib(GL_ENABLE_BIT);
  785. glDisable(GL_DEPTH_TEST);
  786. glDisable(GL_LIGHTING);
  787. glMatrixMode(GL_PROJECTION);
  788. glPushMatrix();
  789. glLoadIdentity();
  790. gluOrtho2D(0, 6400, -3600, 150);
  791. glMatrixMode(GL_MODELVIEW);
  792. glPushMatrix();
  793. glLoadIdentity();
  794.  
  795. if (easymode) {
  796. output(4500, 10, "EASY (TOGGLE WITH 'M')");
  797. }
  798. else {
  799. output(4500, 10, "HARD (TOGGLE WITH 'M')");
  800. }
  801.  
  802. glPopMatrix();
  803. glMatrixMode(GL_PROJECTION);
  804. glPopMatrix();
  805. glPopAttrib();
  806. glMatrixMode(GL_MODELVIEW);
  807. }
  808. glutSwapBuffers();
  809.  
  810. }
  811.  
  812. void idleScene(void) {
  813. if (ppos.y < -40.0) {
  814. //Sleep(10);
  815. //printf("SCORE IS %d", (int)(-Zdelta));
  816. if (!gamelost) {
  817. gamelost = true;
  818. }
  819.  
  820. return;
  821. }
  822.  
  823. Sleep(20);
  824. //Keypresses
  825.  
  826. if ((int)(-Zdelta) > distanceCovered) {
  827. if (easymode) {
  828. score = score + ((int)(-Zdelta) - distanceCovered);
  829. }
  830. else {
  831. if (framecount % 2 == 0) {
  832. score = score + (int)(((int)(-Zdelta) - distanceCovered) * 2);
  833. }
  834. else {
  835. score = score + (int)(((int)(-Zdelta) - distanceCovered));
  836. }
  837. }
  838. distanceCovered = (int)(-Zdelta);
  839. }
  840.  
  841. if (framecount % 200 == 0 && platformLife > 400) {
  842. platformLife = (int)platformLife * (0.995f);
  843. apocaspeed = apocaspeed * (1.02f);
  844. }
  845. if (!easymode && framecount % 20 == 1) {
  846. scoreOffset += 1;
  847. }
  848.  
  849. if (grounded) {
  850. float f = forAcc;
  851. if (keypressed[5]) {
  852. f = forAcc * suM;
  853. }
  854.  
  855. if (keypressed[0]) {
  856. velocity.x += pfor.x * f;
  857. velocity.z += pfor.z * f;
  858. }
  859. else if (keypressed[1]) {
  860. velocity.x -= pfor.x * forAcc;
  861. velocity.z -= pfor.z * forAcc;
  862. }
  863. }
  864. else {
  865. if (keypressed[0]) {
  866. velocity.x += pfor.x * forAcc * flyFactor;
  867. velocity.z += pfor.z * forAcc * flyFactor;
  868. }
  869. else if (keypressed[1]) {
  870. velocity.x -= pfor.x * forAcc * flyFactor;
  871. velocity.z -= pfor.z * forAcc * flyFactor;
  872. }
  873. }
  874.  
  875.  
  876.  
  877. if (keypressed[2]) {
  878. pangle -= 3;
  879. }
  880. else if (keypressed[3]) {
  881. pangle += 3;
  882. }
  883.  
  884.  
  885.  
  886.  
  887. //friction
  888. if (grounded) {
  889. struct Vector3 t2 = projectOnPlaneY(velocity);
  890. struct Vector3 t = add(mul(Cp, project(t2, pfor)), mul(Cn, project(t2, pRit)));
  891. velocity = setVec(t.x, velocity.y, t.z);
  892. }
  893. else {
  894. velocity = mul(Ca, velocity);
  895. }
  896.  
  897. //collision
  898. for (int i = 0; i < numPlatforms; i++) {
  899. addCollisionForce(forms[i]);
  900. }
  901.  
  902. for (int i = 0; i < numPlatforms; i++) {
  903. if (forms[i].pos.z - Zdelta > 40) {
  904. createNextPlatformFor(i);
  905. break;
  906. }
  907. }
  908.  
  909. if (!groundedTest) {
  910. if (grounded) {
  911. grounded = false;
  912. }
  913. }
  914. else {
  915. groundedTest = false;
  916. }
  917.  
  918. //jump and gravity
  919. if (grounded) {
  920. if (keypressed[4]) {
  921. if (keypressed[5]) {
  922. velocity.y = jumpStrength * 1.5f;;
  923. }
  924. else {
  925. velocity.y =jumpStrength;
  926. }
  927. }
  928. }
  929. else {
  930. velocity = add(velocity, gravity);
  931. }
  932.  
  933.  
  934. if (!mouseMoving) {
  935. if(fabs(pangle - hangle) > 0.5f)
  936. hangle = hangle + (pangle - hangle) * 0.1f;
  937. }
  938.  
  939. vectorUpdate();
  940.  
  941. //Velocity
  942. //ppos = add(ppos, velocity);
  943. //Velocity to centre z
  944. ppos = add(ppos, setVec(velocity.x, velocity.y, 0.0));
  945. Zdelta = Zdelta + velocity.z;
  946.  
  947. glutPostRedisplay();
  948. }
  949. #pragma endregion
  950.  
  951. #pragma region MOUSE_AND_KEYS
  952. void Keys(unsigned char key, int x, int y)
  953. {
  954. if (glutGetModifiers())
  955. {
  956. if (GLUT_ACTIVE_SHIFT) {
  957. keypressed[5] = true;
  958. }
  959. else {
  960. keypressed[5] = false;
  961. }
  962. }
  963. else {
  964. keypressed[5] = false;
  965. }
  966.  
  967.  
  968. if (key == 'a' || key == 'A')
  969. keypressed[2] = true;
  970. //break;
  971. if (key == 'd' || key == 'D')
  972. keypressed[3] = true;
  973. //break;
  974. if (key == 'w' || key == 'W')
  975. keypressed[0] = true;
  976. //break;
  977. if (key == 's' || key == 'S') {
  978. keypressed[1] = true;
  979. keypressed[4] = false;
  980. keypressed[5] = false;
  981. }
  982. //break;
  983. if (key == ' ')
  984. keypressed[4] = true;
  985. //break;
  986. if (key == '0')
  987. keypressed[5] = true;
  988. //break;
  989. if (key == 'f')
  990. keypressed[5] = true;
  991.  
  992. if (key == 'm') {
  993. if (!easymode) {
  994. easy_mode();
  995. }
  996. else {
  997. hard_mode();
  998. }
  999. }
  1000.  
  1001. if (key == 'r') {
  1002. if (ppos.y < -10) {
  1003. init();
  1004. }
  1005. }
  1006.  
  1007. if (key == 27) {
  1008. exit(0);
  1009. }
  1010.  
  1011. }
  1012.  
  1013. void SKeys(int key, int xx, int yy)
  1014. {
  1015. if (glutGetModifiers())
  1016. {
  1017. if (GLUT_ACTIVE_SHIFT) {
  1018. keypressed[5] = true;
  1019. }
  1020. else {
  1021. keypressed[5] = false;
  1022. }
  1023. }
  1024. else {
  1025. keypressed[5] = false;
  1026. }
  1027.  
  1028. if (key == GLUT_KEY_LEFT) {
  1029. keypressed[2] = true;
  1030. }
  1031.  
  1032.  
  1033. if (key == GLUT_KEY_RIGHT) {
  1034. keypressed[3] = true;
  1035. }
  1036.  
  1037. if (key == GLUT_KEY_UP) {
  1038. keypressed[0] = true;
  1039. }
  1040.  
  1041. if (key == GLUT_KEY_DOWN) {
  1042. keypressed[1] = true;
  1043. keypressed[4] = false;
  1044. keypressed[5] = false;
  1045. }
  1046. }
  1047.  
  1048. void KeysUp(unsigned char key, int xx, int yy) {
  1049. if (glutGetModifiers())
  1050. {
  1051. if (GLUT_ACTIVE_SHIFT) {
  1052. keypressed[5] = true;
  1053. }
  1054. else {
  1055. keypressed[5] = false;
  1056. }
  1057. }
  1058. else {
  1059. keypressed[5] = false;
  1060. }
  1061.  
  1062.  
  1063.  
  1064. if(key == 'a' || key == 'A')
  1065. keypressed[2] = false;
  1066. //break;
  1067. if (key == 'd' || key == 'D')
  1068. keypressed[3] = false;
  1069. //break;
  1070. if (key == 'w' || key == 'W')
  1071. keypressed[0] = false;
  1072. //break;
  1073. if (key == 's' || key == 'S') {
  1074. keypressed[1] = false;
  1075. keypressed[4] = false;
  1076. keypressed[5] = false;
  1077. }
  1078. //break;
  1079. if (key == ' ')
  1080. keypressed[4] = false;
  1081. //break;
  1082. if (key == '0')
  1083. keypressed[5] = false;
  1084. //break;
  1085. if (key == 'f')
  1086. keypressed[5] = false;
  1087.  
  1088.  
  1089.  
  1090. }
  1091.  
  1092. void SKeysUp(int key, int xx, int yy) {
  1093. if (glutGetModifiers())
  1094. {
  1095. if (GLUT_ACTIVE_SHIFT) {
  1096. keypressed[5] = true;
  1097. }
  1098. else {
  1099. keypressed[5] = false;
  1100. }
  1101. }
  1102. else {
  1103. keypressed[5] = false;
  1104. }
  1105.  
  1106.  
  1107. if (key == GLUT_KEY_LEFT) {
  1108. keypressed[2] = false;
  1109. }
  1110.  
  1111.  
  1112. if (key == GLUT_KEY_RIGHT) {
  1113. keypressed[3] = false;
  1114. }
  1115.  
  1116. if (key == GLUT_KEY_UP) {
  1117. keypressed[0] = false;
  1118. }
  1119.  
  1120. if (key == GLUT_KEY_DOWN) {
  1121. keypressed[1] = false;
  1122. keypressed[4] = false;
  1123. keypressed[5] = false;
  1124. }
  1125. }
  1126.  
  1127. static void mouse(int button, int state, int x, int y)
  1128. {
  1129. startx = x;
  1130. starty = y;
  1131.  
  1132. if (button == GLUT_LEFT_BUTTON) {
  1133. if (state == GLUT_DOWN) {
  1134.  
  1135. mouseMoving = true;
  1136. }
  1137. if (state == GLUT_UP) {
  1138. mouseMoving = false;
  1139. }
  1140. }
  1141.  
  1142. if (button == GLUT_RIGHT_BUTTON) {
  1143. if (state == GLUT_DOWN) {
  1144. keypressed[4] = true;
  1145. }
  1146. if (state == GLUT_UP) {
  1147. keypressed[4] = false;
  1148. }
  1149. }
  1150. }
  1151.  
  1152. static void motion(int x, int y) {
  1153. if (mouseMoving) {
  1154. hangle = hangle + (x - startx) / 2;
  1155. vangle = vangle - (y - starty) / 2;
  1156. /*if (hangle >= 180) { hangle = hangle - 180; }
  1157. if (hangle < -180) { hangle = hangle + 180; }*/
  1158.  
  1159. cfor.x = sin(hangle * 3.14159 / 180);
  1160. cfor.z = -cos(hangle * 3.14159 / 180);
  1161. cfor.y = sin(vangle * 3.14159 / 180);
  1162.  
  1163. cpos.x = ppos.x - cfor.x * camDist;
  1164. cpos.z = ppos.z - cfor.z * camDist;
  1165. cpos.y = ppos.y + 1 - cfor.y * 3;
  1166. //hangle2 = hangle2 + (y - starty) / 10;
  1167. startx = x;
  1168. starty = y;
  1169. }
  1170.  
  1171. glutPostRedisplay();
  1172. }
  1173. #pragma endregion
  1174.  
  1175. int main(int argc, char** argv)
  1176. {
  1177.  
  1178. // init GLUT and create window
  1179.  
  1180. glutInit(&argc, argv);
  1181.  
  1182. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  1183.  
  1184. glutInitWindowPosition(0, 20);
  1185. glutInitWindowSize(1120, 630);
  1186. glutCreateWindow("Sky-runner");
  1187.  
  1188.  
  1189. init();
  1190. glutIdleFunc(idleScene);
  1191. glutDisplayFunc(renderScene);
  1192. glutReshapeFunc(changeSize);
  1193. glutMouseFunc(mouse);
  1194. glutMotionFunc(motion);
  1195. glutKeyboardFunc(Keys);
  1196. glutSpecialFunc(SKeys);
  1197. glutKeyboardUpFunc(KeysUp);
  1198. glutSpecialUpFunc(SKeysUp);
  1199.  
  1200.  
  1201. glEnable(GL_DEPTH_TEST);
  1202.  
  1203. glutMainLoop();
  1204.  
  1205. return 1;
  1206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement