cardCharSelect.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4. #include "cardCharSelect.h"
  5. #include "cardStageSelect.h"
  6. #include "cardGame.h"
  7. #include "brick.h"
  8. CardCharSelect::CardCharSelect(char gIsSurvival)
  9. {
  10. /* Bricks */
  11. //printf("data/heroes\n");
  12. //Parse file with stats
  13. FILE *file = fopen("mod/heroes", "r");
  14. //Failed to open file
  15. if (!file)
  16. {
  17. printf("Error opening file: mod/heroes\n");
  18. return;
  19. }
  20. //Buffer
  21. char buffer[256];
  22. //Start parsing
  23. while (fscanf(file, "%s", buffer) != EOF)
  24. {
  25. //Found comment - skip line
  26. if ( buffer[0] == '#' )
  27. fscanf(file, "%*[^\n]"); else
  28. //Found hero
  29. if ( buffer[0] == '~' )
  30. {
  31. //Position on array
  32. int pos = brick.size();
  33. //Current brick
  34. Brick b;
  35. //Load its graphic
  36. fscanf(file, "%s", buffer);
  37. b.load(buffer, ((pos & 1) == 1 ? 1 : 0));
  38. //Hp
  39. int hp;
  40. fscanf(file, "%d", &hp);
  41. vecHp.push_back(hp);
  42. //Sp
  43. /*int sp;
  44. fscanf(file, "%d", &sp);
  45. vecSp.push_back(sp);*/
  46. //Speed and damage
  47. fscanf(file, "%f", &b.speed);
  48. //fscanf(file, "%hhu", &b.damage);
  49. fscanf(file, "%hhu", &b.damage);
  50. //Position, even numbers to the left
  51. if ((pos & 1) == 0)
  52. b.loc.x = -5.0f;//translateSet(-5.0f, -(pos/2) *1.5f, 0.0f);
  53. //Odd number to the right
  54. else
  55. b.loc.x = 5.0f;//b.translateSet( 5.0f, -(pos/2) *1.5f, 0.0f);
  56. b.loc.y = -(pos/2) *1.5f;
  57. //Add brick to array
  58. brick.push_back(b);
  59. }
  60. }
  61. //Close file
  62. fclose(file);
  63. /* Bars */
  64. health.load("obj/stats_board.ply", 0);
  65. health.loc.y = 3.0f -(0 *2.5f);
  66. health.loc.z = -5.0f;
  67. health.max = 255;
  68. /*
  69. special.load("obj/stats_board.ply", 0);
  70. special.loc.y = 4.0f -(1 *2.5f);
  71. special.loc.z = -5.0f;
  72. special.max = 255;
  73. */
  74. speed.load("obj/stats_board.ply", 0);
  75. speed.loc.y = 3.0f -(1 *2.5f);
  76. speed.loc.z = -5.0f;
  77. speed.max = 8;
  78. damage.load("obj/stats_board.ply", 0);
  79. damage.loc.y = 3.0f -(2 *2.5f);
  80. damage.loc.z = -5.0f;
  81. damage.max = 255;
  82. //Start selection from first brick
  83. prevSel = selection = 0;
  84. sAnim.max = 5;
  85. statsToBars();
  86. //Font
  87. font.setColorFront(0.9f, 0.327f, 0.689f);
  88. font.setColorBack (0.26f, 0.121f, 0.256f);
  89. font.setAlign(1);
  90. //Data to pass on
  91. isSurvival = gIsSurvival;
  92. }
  93. void CardCharSelect::input(unsigned char key, bool pressed)
  94. {
  95. //Do not accept input if animation is running
  96. if (!anim.isRunning())
  97. //According to key
  98. switch (key)
  99. {
  100. //Left/right
  101. case 'a':
  102. case 'd':
  103. //Pressed
  104. if (pressed)
  105. {
  106. //Current selection is left
  107. if ((selection & 1) == 0)
  108. {
  109. //There is character on right
  110. if (brick.size() > 0 && selection < brick.size()-1)
  111. {
  112. //Move right and change shown stats
  113. selection++;
  114. statsToBars();
  115. }
  116. }
  117. //Current selection is right
  118. else
  119. {
  120. //Move selection leftwards (There is always left)
  121. selection--;
  122. statsToBars();
  123. }
  124. }
  125. break;
  126. //Up
  127. case 'w':
  128. if (pressed)
  129. {
  130. //Selection is greater/equal to 2 (so it can move upwards)
  131. if (selection >= 2)
  132. {
  133. /* Save previous selection (for animation),
  134. * move selection upwards and start animation
  135. */
  136. prevSel = selection;
  137. selection -= 2;
  138. sAnim.startFromStart();
  139. statsToBars();
  140. }
  141. }
  142. break;
  143. //Down
  144. case 's':
  145. if (pressed)
  146. {
  147. /* There are more than 2 bricks (so at least 2 rows)
  148. * and selection is not on the last one
  149. */
  150. if (brick.size() > 2
  151. && selection < brick.size()-2)
  152. {
  153. /* Save previous selection (for animation),
  154. * move selection downwards and start animation
  155. */
  156. prevSel = selection;
  157. selection += 2;
  158. sAnim.startFromStart();
  159. statsToBars();
  160. }
  161. }
  162. break;
  163. //Space
  164. case '\040':
  165. //Start animation
  166. anim.startFromStart();
  167. break;
  168. }
  169. }
  170. void CardCharSelect::update()
  171. {
  172. //Animation updated
  173. if (anim.update())
  174. //Animation ended
  175. if (anim.isAtEnd())
  176. {
  177. char isSurv = isSurvival;
  178. int sel = selection;
  179. int sel2 = rand() %brick.size();
  180. //Delete this menu and give selection to next menu
  181. delete card;
  182. card = new CardStageSelect(isSurv, sel, sel2);
  183. }
  184. //Update selection animation
  185. sAnim.update();
  186. }
  187. void CardCharSelect::draw()
  188. {
  189. //Animation's drawing
  190. Card::draw();
  191. //Camera movement
  192. glTranslatef(0.0f, -1.0f, -10.0f);
  193. //Draw bars
  194. health .draw();
  195. //special.draw();
  196. speed .draw();
  197. damage .draw();
  198. //Texts
  199. glPushMatrix();
  200. glTranslatef(health.loc.x -2.0f, health.loc.y, health.loc.z);
  201. glScalef(1.3f, 1.3f, 1.3f);
  202. font.draw("hp");
  203. glPopMatrix();
  204. /*
  205. glPushMatrix();
  206. glTranslatef(special.loc.x -2.0f, special.loc.y, special.loc.z);
  207. glScalef(1.3f, 1.3f, 1.3f);
  208. font.draw("sp");
  209. glPopMatrix();
  210. */
  211. glPushMatrix();
  212. glTranslatef(speed.loc.x -2.0f, speed.loc.y, speed.loc.z);
  213. glScalef(1.3f, 1.3f, 1.3f);
  214. font.draw("spd");
  215. glPopMatrix();
  216. glPushMatrix();
  217. glTranslatef(damage.loc.x -2.0f, damage.loc.y, damage.loc.z);
  218. glScalef(1.3f, 1.3f, 1.3f);
  219. font.draw("dmg");
  220. glPopMatrix();
  221. //More camera movement (so it doesn't affect bars and text)
  222. if (sAnim.isRunning())
  223. glTranslatef(0, -(brick[prevSel].loc.y
  224. +sAnim.interpolate(brick[selection].loc.y -brick[prevSel].loc.y)), 0);
  225. //glTranslatef(0, -(brick[prevSel].y()
  226. // +sAnim.interpolate(brick[selection].y() -brick[prevSel].y())), 0);
  227. else
  228. glTranslatef(0, -brick[selection].loc.y, 0);
  229. //glTranslatef(0, -brick[selection].y(), 0);
  230. glRotatef(15.0f, 1.0f, 0.0f, 0.0f);
  231. //Draw bricks
  232. unsigned int i;
  233. for (i = 0; i < brick.size(); i++)
  234. {
  235. //Selected brick, quake effect
  236. if (selection == i)
  237. {
  238. //Seperate X and Y
  239. float tempX = ((rand() +1) %100) /1000.0f;
  240. float tempY = ((rand() +1) %100) /1000.0f;
  241. //Apply offset
  242. //brick[i].translateAdd(tempX, tempY, 0.0f);
  243. brick[i].loc.x += tempX;
  244. brick[i].loc.y += tempY;
  245. //Draw choice
  246. brick[i].draw();
  247. //Cancel offset
  248. //brick[i].translateAdd(-tempX, -tempY, 0.0f);
  249. brick[i].loc.x -= tempX;
  250. brick[i].loc.y -= tempY;
  251. }
  252. //Other non selected bricks
  253. else brick[i].draw();
  254. }
  255. }
  256. //Apply current selected hero's stats to bars
  257. void CardCharSelect::statsToBars()
  258. {
  259. //Health
  260. health.cur = vecHp[selection];
  261. //special.cur = vecSp[selection];
  262. speed.cur = brick[selection].speed *10;
  263. if (brick[selection].damage < 60)
  264. damage.cur = brick[selection].damage *255 /60;
  265. else
  266. damage.cur = 255;
  267. }