Game.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "Game.h"
  2. Game::Game(int gametype)
  3. {
  4. GameType = gametype;
  5. rotateTrajLeft = InertialTrajProcess();
  6. rotateTrajRight = InertialTrajProcess();
  7. leftFootStep = FootStep();
  8. rightFootStep = FootStep();
  9. rotateMatrix[0] = 1.0f;
  10. rotateMatrix[1] = 0.0f;
  11. rotateMatrix[2] = 0.0f;
  12. rotateMatrix[3] = 1.0f;
  13. leftStepRate = 0;
  14. rightStepRate = 0;
  15. leftRate = 0;
  16. rightRate = 0;
  17. interaction = Interaction();
  18. InteractionCMD = -1;
  19. if (GameType == RUNGAME)
  20. {
  21. rungame = RunGame();
  22. }
  23. else if (GameType == DANCEGAME)
  24. {
  25. dancegame = DanceGame();
  26. }
  27. else if (GameType == ORIGINTRAJ)
  28. {
  29. originTraj = OriginTraj();
  30. }
  31. else if (GameType == H5GAME)
  32. {
  33. jumpH5game = jumpH5();
  34. }
  35. else if (GameType == MONSTER)
  36. {
  37. monsterH5 = MonsterH5();
  38. }
  39. }
  40. int Game::getInteractionCMD()
  41. {
  42. return InteractionCMD;
  43. }
  44. void Game::GameProcess(int timeStamp, int* right_pos, int* right_att, int *right_acc, int right_zupt, int right_press,
  45. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  46. int jump, int down, int rssi, int girl_shoes)
  47. {
  48. leftFootStep.stepCal(timeStamp, left_pos, left_zupt);
  49. rightFootStep.stepCal(timeStamp, right_pos, right_zupt);
  50. interaction.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  51. left_pos, left_att, left_acc, left_zupt, left_press,
  52. jump, down, rssi);
  53. InteractionCMD = interaction.get_motion_state();
  54. if (GameType == RUNGAME)
  55. {
  56. //�ܿ���Ϸ������
  57. if (left_zupt)
  58. {
  59. rotateTrajLeft.ResetHeading(left_att[0]);
  60. }
  61. rotateTrajLeft.TrajRotate(left_pos);
  62. if (right_zupt)
  63. {
  64. rotateTrajRight.ResetHeading(right_att[0]);
  65. }
  66. rotateTrajRight.TrajRotate(right_pos);
  67. rungame.Process(right_pos, right_att, right_zupt, left_pos, left_att, left_zupt, jump, down, girl_shoes);
  68. //�ܿ���Ϸ��������ȡ
  69. rungame.getResult(resultMatrix);
  70. }
  71. else if (GameType == DANCEGAME)
  72. {
  73. dancegame.Process(right_pos, right_att, right_zupt, right_press , left_pos, left_att, left_zupt, left_press, jump, down, rssi);
  74. dancegame.getResult(resultMatrix);
  75. }
  76. else if (GameType == ORIGINTRAJ)
  77. {
  78. originTraj.Process(right_pos, right_att, right_zupt, left_pos, left_att, left_zupt);
  79. }
  80. else if (GameType == H5GAME)
  81. {
  82. if (left_zupt)
  83. {
  84. rotateTrajLeft.ResetHeading(left_att[0]);
  85. }
  86. rotateTrajLeft.TrajRotate(left_pos);
  87. if (right_zupt)
  88. {
  89. rotateTrajRight.ResetHeading(right_att[0]);
  90. }
  91. rotateTrajRight.TrajRotate(right_pos);
  92. jumpH5game.Process(right_pos, right_att, right_acc, right_press, right_zupt,
  93. left_pos, left_att, left_acc, left_press, left_zupt, jump, timeStamp, rssi);
  94. jumpH5game.getResult(resultMatrix);
  95. }
  96. else if (GameType == MONSTER)
  97. {
  98. monsterH5.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  99. left_pos, left_att, left_acc, left_zupt, left_press,
  100. jump, down, rssi);
  101. monsterH5.getResult(resultMatrix);
  102. }
  103. /*
  104. if (foot_data_file.is_open()&& GameType == DANCEGAME)
  105. {
  106. foot_data_file << timeStamp << " " << dancegame.getGamePos(LEFT_FOOT, 0) << " " << dancegame.getGamePos(LEFT_FOOT, 1) << " " << left_zupt <<
  107. " " << dancegame.getGamePos(RIGHT_FOOT, 0) << " " << dancegame.getGamePos(RIGHT_FOOT, 1) << " " << right_zupt << " " << rssi
  108. << " " << bingo << std::endl;
  109. bingo = 0;
  110. }
  111. */
  112. }
  113. void Game::RunGameProcess(int* right_pos, int* right_att, int right_zupt, int* left_pos, int* left_att, int left_zupt, int jump, int down, int girl_shoes)
  114. {
  115. rungame.Process( right_pos, right_att, right_zupt, left_pos, left_att, left_zupt, jump, down, girl_shoes);
  116. }
  117. void Game::getGameResult(int* matrix)
  118. {
  119. memcpy(matrix, resultMatrix, 4 * sizeof(int));
  120. }
  121. int Game::getStepStatus(int left_or_right)
  122. {
  123. if (left_or_right == LEFT_FOOT)
  124. return leftFootStep.getStepStatus();
  125. else if (left_or_right == RIGHT_FOOT)
  126. return rightFootStep.getStepStatus();
  127. else
  128. return -1;
  129. }
  130. int Game::getStepFreq( int left_or_right)
  131. {
  132. if (left_or_right == LEFT_FOOT)
  133. return leftFootStep.getStepFreq();
  134. else if (left_or_right == RIGHT_FOOT)
  135. return rightFootStep.getStepFreq();
  136. else
  137. return -1;
  138. }
  139. int Game::getStepCount(int left_or_right)
  140. {
  141. if (left_or_right == LEFT_FOOT)
  142. return leftFootStep.getStepCount();
  143. else if (left_or_right == RIGHT_FOOT)
  144. return rightFootStep.getStepCount();
  145. else
  146. return -1;
  147. }
  148. float Game::getGamePos(int left_or_right, int index)
  149. {
  150. if (index < 0 || index >2)
  151. return -1;
  152. if (GameType == RUNGAME)
  153. {
  154. return -1;
  155. }
  156. else if(GameType == DANCEGAME)
  157. {
  158. return dancegame.getGamePos(left_or_right, index);
  159. }
  160. else if (GameType == ORIGINTRAJ)
  161. {
  162. return originTraj.getGamePos(left_or_right, index);
  163. }
  164. else
  165. {
  166. return -1;
  167. }
  168. }
  169. string Game::getVersion()
  170. {
  171. return GAME_VERSION;
  172. }
  173. //����Ь�ӵ�����
  174. void Game::GameProcess(uint8_t* buf, int len) {
  175. right_pos_data[0] = int32_t(buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0);
  176. right_pos_data[1] = int32_t(buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7] << 0);
  177. right_pos_data[2] = int32_t(buf[8] << 24 | buf[9] << 16 | buf[10] << 8 | buf[11] << 0);
  178. left_pos_data[0] = int32_t(buf[12] << 24 | buf[13] << 16 | buf[14] << 8 | buf[15] << 0);
  179. left_pos_data[1] = int32_t(buf[16] << 24 | buf[17] << 16 | buf[18] << 8 | buf[19] << 0);
  180. left_pos_data[2] = int32_t(buf[20] << 24 | buf[21] << 16 | buf[22] << 8 | buf[23] << 0);
  181. right_att_data[0] = int16_t(buf[24] << 8 | buf[25] << 0);
  182. right_att_data[1] = int16_t(buf[26] << 8 | buf[27] << 0);
  183. right_att_data[2] = int16_t(buf[28] << 8 | buf[29] << 0);
  184. left_att_data[0] = int16_t(buf[30] << 8 | buf[31] << 0);
  185. left_att_data[1] = int16_t(buf[32] << 8 | buf[33] << 0);
  186. left_att_data[2] = int16_t(buf[34] << 8 | buf[35] << 0);
  187. right_acc_data[0] = int16_t(buf[36] << 8 | buf[37] << 0);
  188. right_acc_data[1] = int16_t(buf[38] << 8 | buf[39] << 0);
  189. right_acc_data[2] = int16_t(buf[40] << 8 | buf[41] << 0);
  190. left_acc_data[0] = int16_t(buf[42] << 8 | buf[43] << 0);
  191. left_acc_data[1] = int16_t(buf[44] << 8 | buf[45] << 0);
  192. left_acc_data[2] = int16_t(buf[46] << 8 | buf[47] << 0);
  193. girl_shoes = (buf[48] & 0x16) >> 4;
  194. s_zupt = (buf[48] & 0x08) >> 3;
  195. h_zupt = (buf[48] & 0x04) >> 2;
  196. down = (buf[48] & 0x02) >> 1;
  197. jump = (buf[48] & 0x01) >> 0;
  198. rssi = (int)buf[49];
  199. posTimeStamp = buf[50];
  200. //std::cout << "package_time : " << posTimeStamp << std::endl;
  201. right_press = uint16_t(buf[51] << 8 | buf[52] << 0);
  202. left_press = uint16_t(buf[53] << 8 | buf[54] << 0);
  203. GameProcess(posTimeStamp,
  204. right_pos_data, right_att_data, right_acc_data, s_zupt, (int)right_press,
  205. left_pos_data, left_att_data, left_acc_data, h_zupt, (int)left_press,
  206. jump, down, rssi, girl_shoes);
  207. }
  208. //��Ϸ��ʼ��ʱ�򴥷��Ķ���
  209. void Game::start(string path_root)
  210. {
  211. string game_name;
  212. if (GameType == RUNGAME)
  213. {
  214. game_name = "rungame";
  215. }
  216. else if (GameType == DANCEGAME)
  217. {
  218. game_name = "dancegame";
  219. }
  220. stringstream ss;
  221. ss << time(NULL);
  222. foot_data_file.open(path_root + '/'+game_name + "_" + ss.str() + ".txt", fstream::out);
  223. }
  224. void Game::end()
  225. {
  226. foot_data_file.close();
  227. }
  228. void Game::isBingo()
  229. {
  230. bingo = 1;
  231. }