Game.cpp 6.4 KB

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