cardStageSelect.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "cardMenu.h"
  2. #include "GL/glut.h"
  3. #include "cardStageSelect.h"
  4. #include "cardGame.h"
  5. #include <stdlib.h>
  6. #include "string3d.h"
  7. #include <stdio.h>
  8. //Consntructor
  9. CardStageSelect::CardStageSelect(char gIsSurvival, int gp1, int gp2)
  10. {
  11. //Parse file with stages
  12. FILE *file = fopen("mod/stages", "r");
  13. //Failed to open file
  14. if (!file)
  15. {
  16. printf("Error opening file: mod/stages\n");
  17. return;
  18. }
  19. //Buffer
  20. char buffer[256];
  21. //Start parsing
  22. while (fscanf(file, "%s", buffer) != EOF)
  23. {
  24. //Found comment - skip line
  25. if (buffer[0] == '#')
  26. fscanf(file, "%*[^\n]"); else
  27. //Found stage
  28. if (buffer[0] == '~')
  29. {
  30. //Get stage's path
  31. fscanf(file, "%s", buffer);
  32. //Create stage's mesh
  33. Mesh stage;
  34. stage.load(buffer, 0);
  35. //Calculate stage's position
  36. int i = stages.size();
  37. stage.loc.x = (i & 1) == 0 ? -15.0f : 15.0f;
  38. stage.loc.z = (i/2) *(15.0f);
  39. //Stage has obstacles
  40. if (fscanf(file, "%s", buffer) != EOF && buffer[0] == '{')
  41. while (1)
  42. {
  43. //Get path
  44. fscanf(file, "%s", buffer);
  45. //No more obstacles
  46. if (buffer[0] == '}')
  47. break;
  48. //Load obstacle
  49. Mesh ob;
  50. ob.load(buffer, 0);
  51. //Get obstacle's position
  52. fscanf(file, "%f %f", &ob.loc.x, &ob.loc.z);
  53. //Add obstacle to stage's mesh
  54. stage.add(ob);
  55. }
  56. //Stage has environment objects
  57. if (fscanf(file, "%s", buffer) != EOF && buffer[0] == '{')
  58. while (1)
  59. {
  60. //Get path
  61. fscanf(file, "%s", buffer);
  62. //No more environment objects
  63. if (buffer[0] == '}')
  64. break;
  65. //Load env object
  66. Mesh m;
  67. m.load(buffer, 0);
  68. //Get objects's position
  69. fscanf(file, "%f %f", &m.loc.x, &m.loc.z);
  70. //Add obstacle to stage's mesh
  71. stage.add(m);
  72. }
  73. //Add stage to vector
  74. stages.push_back(stage);
  75. }
  76. }
  77. //Close file
  78. fclose(file);
  79. //Init selection
  80. prevSel = selection = 0;
  81. //Animation length
  82. sAnim.max = 15;
  83. //Data to pass on
  84. isSurvival = gIsSurvival;
  85. p1 = gp1;
  86. p2 = gp2;
  87. }
  88. void CardStageSelect::input(unsigned char key, bool pressed)
  89. {
  90. switch (key)
  91. {
  92. //Space
  93. case ' ':
  94. //Start game
  95. if (pressed) anim.startFromStart();
  96. break;
  97. //Move selection upwards
  98. case 'w':
  99. if (pressed && selection >= 2)
  100. {
  101. //Save previous selection (for animation)
  102. prevSel = selection;
  103. //Move selection
  104. selection -= 2;
  105. //Start animation
  106. sAnim.startFromStart();
  107. }
  108. break;
  109. //Move selection downwards
  110. case 's':
  111. if (pressed && stages.size() > 2 && selection < stages.size() -2)
  112. {
  113. //Save previous selection (for animation)
  114. prevSel = selection;
  115. //Move selection
  116. selection += 2;
  117. //Start new animation
  118. sAnim.startFromStart();
  119. }
  120. break;
  121. //Move selection left/right
  122. case 'a':
  123. case 'd':
  124. if (pressed)
  125. {
  126. //New selection is shorter that stage array
  127. if ( (selection ^ 1) < stages.size() )
  128. {
  129. //Save previous selection (for animation)
  130. prevSel = selection;
  131. //Flips first bit, this flips from odd to even and vice versa
  132. selection ^= 1;
  133. //Start new animation
  134. sAnim.startFromStart();
  135. }
  136. }
  137. break;
  138. }
  139. }
  140. void CardStageSelect::update()
  141. {
  142. //Animation changed
  143. if (anim.update())
  144. //Animation reached end
  145. if (anim.isAtEnd())
  146. {
  147. //Save data as local, so they can be passed after `delete`
  148. char isSurv = isSurvival;
  149. int player1 = p1;
  150. int player2 = p2;
  151. int stage = selection;
  152. //Delete menu
  153. delete card;
  154. //Start character select screen
  155. card = new CardGame( isSurv, player1, player2, stage );
  156. }
  157. //Update selection animation
  158. if (sAnim.update())
  159. //It stopped - update previous selection
  160. if (sAnim.isAtEnd())
  161. prevSel = selection;
  162. }
  163. void CardStageSelect::draw()
  164. {
  165. //Card's animation
  166. Card::draw();
  167. //Move camera
  168. glTranslatef(0.0f, 0.0f, -25.0f);
  169. glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
  170. /* If selection animation is running, move camera to the interpolated
  171. * location of previous and current location, otherwise move camera
  172. * to current selection
  173. */
  174. if (sAnim.isRunning())
  175. glTranslatef(-stages[prevSel].loc.x
  176. +sAnim.interpolate(stages[prevSel].loc.x -stages[selection].loc.x),
  177. 0.0f,
  178. -stages[prevSel].loc.z
  179. +sAnim.interpolate(stages[prevSel].loc.z -stages[selection].loc.z));
  180. else
  181. glTranslatef(-stages[selection].loc.x, 0.0f, -stages[selection].loc.z);
  182. //Draw stages
  183. for (unsigned int i = 0; i < stages.size(); i++)
  184. if (selection == i)
  185. {
  186. //Seperate X and Y
  187. float tempX = ((rand() +1) %100) /1000.0f;
  188. float tempY = ((rand() +1) %100) /1000.0f;
  189. //Apply offset
  190. stages[i].loc.x += tempX;
  191. stages[i].loc.y += tempY;
  192. //Draw choice
  193. stages[i].draw();
  194. //Cancel offset
  195. stages[i].loc.x -= tempX;
  196. stages[i].loc.y -= tempY;
  197. }
  198. else
  199. stages[i].draw();
  200. }