Game.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. publicmotion = publicMotion();
  10. rotateMatrix[0] = 1.0f;
  11. rotateMatrix[1] = 0.0f;
  12. rotateMatrix[2] = 0.0f;
  13. rotateMatrix[3] = 1.0f;
  14. leftStepRate = 0;
  15. rightStepRate = 0;
  16. leftRate = 0;
  17. rightRate = 0;
  18. last_down = 0;
  19. last_jump = 0;
  20. down_count = 0;
  21. jump_count = 0;
  22. last_left_step = 0;
  23. last_right_step = 0;
  24. down_wait = 0;
  25. avoid_down_between_jump = 0;
  26. interaction = Interaction();
  27. InteractionCMD = -1;
  28. shoes_down = 0;
  29. if (GameType == RUNGAME)
  30. {
  31. rungame = RunGame();
  32. }
  33. else if (GameType == DANCEGAME)
  34. {
  35. dancegame = DanceGame();
  36. }
  37. else if (GameType == ORIGINTRAJ)
  38. {
  39. originTraj = OriginTraj();
  40. }
  41. else if (GameType == MONSTER)
  42. {
  43. monsterH5 = MonsterH5();
  44. }
  45. else if (GameType == RUINS)
  46. {
  47. ruinsExplore = RuinsExplore();
  48. }
  49. else if (GameType == CYCLE)
  50. {
  51. tricycle = Tricycle();
  52. }
  53. else if (GameType == BIGFOOT)
  54. {
  55. bigfoot = BigFoot();
  56. }
  57. else if (GameType == TESTGAME)
  58. {
  59. testgame = testGame();
  60. }
  61. else if (GameType == H5GAME)
  62. {
  63. h5 = H5();
  64. }
  65. }
  66. int Game::getInteractionCMD()
  67. {
  68. return InteractionCMD;
  69. }
  70. void Game::processCalorieData(int down, int jump)
  71. {
  72. if (down)
  73. {
  74. down_wait = 20;
  75. }
  76. if (jump)
  77. {
  78. avoid_down_between_jump = 20;
  79. }
  80. if (avoid_down_between_jump > 0 && h_zupt == 0 && s_zupt == 0)
  81. {
  82. avoid_down_between_jump = 20;
  83. }
  84. if (avoid_down_between_jump > 0 && down == 1)
  85. {
  86. avoid_down_between_jump = 20;
  87. }
  88. if (avoid_down_between_jump > 0)
  89. {
  90. down_wait = 0;
  91. down = 0;
  92. last_down = 0;
  93. }
  94. if (down_wait > 0)
  95. {
  96. down = 1;
  97. down_wait --;
  98. }
  99. if (avoid_down_between_jump)
  100. {
  101. avoid_down_between_jump--;
  102. }
  103. if (down == 0 && last_down == 1)
  104. {
  105. down_count++;
  106. std::cout << "蹲的次数 : " << down_count << endl;
  107. }
  108. if (last_jump == 0 && jump == 1)
  109. {
  110. jump_count++;
  111. std::cout << "跳的次数 : " << jump_count << endl;
  112. }
  113. last_down = down;
  114. last_jump = jump;
  115. }
  116. int Game::getGameDownCount()
  117. {
  118. return down_count;
  119. }
  120. int Game::getGameJumpCount()
  121. {
  122. return jump_count;
  123. }
  124. int Game::onlineDownEstimate()
  125. {
  126. if (posTimeStamp % 8 != 0)
  127. {
  128. return 0;
  129. }
  130. }
  131. void Game::GameProcess(int timeStamp, int* right_pos, int* right_att, int *right_acc, int right_zupt, int right_press,
  132. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  133. int jump, int down, int rssi, int girl_shoes)
  134. {
  135. //计算步频
  136. leftFootStep.stepCal(timeStamp, left_pos, left_zupt, left_step);
  137. rightFootStep.stepCal(timeStamp, right_pos, right_zupt, right_step);
  138. std::cout << "left_step count is " << getStepCount(LEFT_FOOT) << endl;
  139. //计算卡路里
  140. interaction.Process(gameTime, right_pos, right_att, right_acc, right_zupt, right_press,
  141. left_pos, left_att, left_acc, left_zupt, left_press,
  142. jump, down, rssi);
  143. InteractionCMD = interaction.get_motion_state();
  144. ////统计游戏里面的蹲跳
  145. processCalorieData(down, jump);
  146. if (GameType == RUNGAME)
  147. {
  148. //跑酷游戏处理函数
  149. rungame.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  150. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi);
  151. //跑酷游戏处理结果获取
  152. rungame.getResult(resultMatrix);
  153. }
  154. else if (GameType == DANCEGAME)
  155. {
  156. dancegame.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  157. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi);
  158. dancegame.getResult(resultMatrix);
  159. }
  160. else if (GameType == ORIGINTRAJ)
  161. {
  162. originTraj.Process(right_pos, right_att, right_zupt, left_pos, left_att, left_zupt);
  163. }
  164. else if (GameType == MONSTER)
  165. {
  166. monsterH5.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  167. left_pos, left_att, left_acc, left_zupt, left_press,
  168. jump, down, rssi);
  169. monsterH5.getResult(resultMatrix);
  170. }
  171. else if (GameType == RUINS)
  172. {
  173. ruinsExplore.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  174. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  175. //跑酷游戏处理结果获取
  176. ruinsExplore.getResult(resultMatrix);
  177. }
  178. else if (GameType == CYCLE)
  179. {
  180. if (left_zupt)
  181. {
  182. rotateTrajLeft.ResetHeading(left_att[0]);
  183. }
  184. rotateTrajLeft.TrajRotate(left_pos);
  185. if (right_zupt)
  186. {
  187. rotateTrajRight.ResetHeading(right_att[0]);
  188. }
  189. rotateTrajRight.TrajRotate(right_pos);
  190. tricycle.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  191. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  192. tricycle.getResult(resultMatrix);
  193. left_att[0] = resultMatrix[0];
  194. resultMatrix[0] = -1;
  195. resultMatrix[1] = -1;
  196. }
  197. else if (GameType == BIGFOOT)
  198. {
  199. bigfoot.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  200. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  201. //跑酷游戏处理结果获取
  202. bigfoot.getResult(resultMatrix);
  203. }
  204. else if (GameType == TESTGAME)
  205. {
  206. testgame.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  207. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  208. testgame.getResult(resultMatrix);
  209. }
  210. else if (GameType == H5GAME)
  211. {
  212. h5.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  213. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  214. h5.getResult(resultMatrix);
  215. }
  216. /*
  217. if (foot_data_file.is_open()&& GameType == DANCEGAME)
  218. {
  219. foot_data_file << timeStamp << " " << dancegame.getGamePos(LEFT_FOOT, 0) << " " << dancegame.getGamePos(LEFT_FOOT, 1) << " " << left_zupt <<
  220. " " << dancegame.getGamePos(RIGHT_FOOT, 0) << " " << dancegame.getGamePos(RIGHT_FOOT, 1) << " " << right_zupt << " " << rssi
  221. << " " << bingo << std::endl;
  222. bingo = 0;
  223. }
  224. */
  225. }
  226. void Game::RunGameProcess(int* right_pos, int* right_att, int* right_acc, int right_zupt, int right_press,
  227. int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press,
  228. int jump, int down, int girl_shoes)
  229. {
  230. rungame.Process(right_pos, right_att, right_acc, right_zupt, right_press,
  231. left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes);
  232. }
  233. void Game::getGameResult(int* matrix)
  234. {
  235. memcpy(matrix, resultMatrix, 4 * sizeof(int));
  236. }
  237. int Game::getStepStatus(int left_or_right)
  238. {
  239. if (left_or_right == LEFT_FOOT)
  240. return leftFootStep.getStepStatus();
  241. else if (left_or_right == RIGHT_FOOT)
  242. return rightFootStep.getStepStatus();
  243. else
  244. return -1;
  245. }
  246. int Game::getStepFreq( int left_or_right)
  247. {
  248. if (left_or_right == LEFT_FOOT)
  249. return leftFootStep.getStepFreq();
  250. else if (left_or_right == RIGHT_FOOT)
  251. return rightFootStep.getStepFreq();
  252. else
  253. return -1;
  254. }
  255. int Game::getStepCount(int left_or_right)
  256. {
  257. if (left_or_right == LEFT_FOOT)
  258. return leftFootStep.getStepCount();
  259. else if (left_or_right == RIGHT_FOOT)
  260. return rightFootStep.getStepCount();
  261. else
  262. return -1;
  263. }
  264. float Game::getGamePos(int left_or_right, int index)
  265. {
  266. if (index < 0 || index >2)
  267. return -1;
  268. if (GameType == RUNGAME)
  269. {
  270. return rungame.getGamePos(left_or_right, index);
  271. }
  272. else if(GameType == DANCEGAME)
  273. {
  274. return dancegame.getGamePos(left_or_right, index);
  275. }
  276. else if (GameType == ORIGINTRAJ)
  277. {
  278. return originTraj.getGamePos(left_or_right, index);
  279. }
  280. else
  281. {
  282. return -1;
  283. }
  284. }
  285. string Game::getVersion()
  286. {
  287. return GAME_VERSION;
  288. }
  289. string Game::getGameDataStr()
  290. {
  291. string data_str_temp = "";
  292. //先记录右边
  293. data_str_temp = data_str;
  294. data_str = "";
  295. return data_str_temp;
  296. }
  297. void Game::setGameDataStr()
  298. {
  299. if (data_str.size() != 0)
  300. {
  301. data_str = "";
  302. }
  303. data_str += (to_string(right_pos_data[0]) + "," + to_string(right_pos_data[1]) + "," + to_string(right_pos_data[2]) + ",");
  304. data_str += (to_string(right_att_data[0]) + "," + to_string(right_att_data[1]) + "," + to_string(right_att_data[2]) + ",");
  305. data_str += (to_string(right_acc_data[0]) + "," + to_string(right_acc_data[1]) + "," + to_string(right_acc_data[2]) + ",");
  306. data_str += (to_string(right_front_mag) + "," + to_string(right_back_mag) + "," + to_string(s_zupt) + ",");
  307. data_str += (to_string(left_pos_data[0]) + "," + to_string(left_pos_data[1]) + "," + to_string(left_pos_data[2]) + ",");
  308. data_str += (to_string(left_att_data[0]) + "," + to_string(left_att_data[1]) + "," + to_string(left_att_data[2]) + ",");
  309. data_str += (to_string(left_acc_data[0]) + "," + to_string(left_acc_data[1]) + "," + to_string(left_acc_data[2]) + ",");
  310. data_str += (to_string(left_front_mag) + "," + to_string(left_back_mag) + "," + to_string(h_zupt) + ",");
  311. data_str += (to_string(shoes_down) + "," + to_string(jump) + "," + to_string(rssi) + "," + to_string(posTimeStamp) + ",");
  312. data_str += (to_string(right_step) + "," + to_string(left_step) + "," + to_string(down) );
  313. }
  314. void Game::getFootAtt(int* left_att, int* right_att)
  315. {
  316. memcpy(right_att, right_att_data, 3 * sizeof(int));
  317. memcpy(left_att, left_att_data, 3 * sizeof(int));
  318. }
  319. //解析鞋子的数据
  320. void Game::GameProcessBuf(uint8_t* buf, int len) {
  321. static int last_game_time;
  322. right_pos_data[0] = int32_t(buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0);
  323. right_pos_data[1] = int32_t(buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7] << 0);
  324. right_pos_data[2] = int32_t(buf[8] << 24 | buf[9] << 16 | buf[10] << 8 | buf[11] << 0);
  325. left_pos_data[0] = int32_t(buf[12] << 24 | buf[13] << 16 | buf[14] << 8 | buf[15] << 0);
  326. left_pos_data[1] = int32_t(buf[16] << 24 | buf[17] << 16 | buf[18] << 8 | buf[19] << 0);
  327. left_pos_data[2] = int32_t(buf[20] << 24 | buf[21] << 16 | buf[22] << 8 | buf[23] << 0);
  328. right_att_data[0] = int16_t(buf[24] << 8 | buf[25] << 0);
  329. right_att_data[1] = int16_t(buf[26] << 8 | buf[27] << 0);
  330. right_att_data[2] = int16_t(buf[28] << 8 | buf[29] << 0);
  331. left_att_data[0] = int16_t(buf[30] << 8 | buf[31] << 0);
  332. left_att_data[1] = int16_t(buf[32] << 8 | buf[33] << 0);
  333. left_att_data[2] = int16_t(buf[34] << 8 | buf[35] << 0);
  334. right_acc_data[0] = int16_t(buf[36] << 8 | buf[37] << 0);
  335. right_acc_data[1] = int16_t(buf[38] << 8 | buf[39] << 0);
  336. right_acc_data[2] = int16_t(buf[40] << 8 | buf[41] << 0);
  337. left_acc_data[0] = int16_t(buf[42] << 8 | buf[43] << 0);
  338. left_acc_data[1] = int16_t(buf[44] << 8 | buf[45] << 0);
  339. left_acc_data[2] = int16_t(buf[46] << 8 | buf[47] << 0);
  340. girl_shoes = (buf[48] & 0x16) >> 4;
  341. s_zupt = (buf[48] & 0x08) >> 3;
  342. h_zupt = (buf[48] & 0x04) >> 2;
  343. shoes_down = (buf[48] & 0x02) >> 1;
  344. jump = (buf[48] & 0x01) >> 0;
  345. rssi = (int) buf[49];
  346. posTimeStamp = buf[50];
  347. if (lastTimeStamp == -1)
  348. {
  349. lastTimeStamp = posTimeStamp;
  350. return;
  351. }
  352. if (posTimeStamp - lastTimeStamp > - 100 && posTimeStamp - lastTimeStamp <=0)
  353. {
  354. std::cout << "receive old shoes package ( lastTimeStamp : " << lastTimeStamp << " posTimeStamp : " << posTimeStamp << " )" <<endl;
  355. lastTimeStamp = posTimeStamp;
  356. return;
  357. }
  358. gameTime += (posTimeStamp - lastTimeStamp < 0 ? posTimeStamp - lastTimeStamp + 256 : posTimeStamp - lastTimeStamp);
  359. lastTimeStamp = posTimeStamp;
  360. right_front_mag = uint16_t(buf[51] << 8 | buf[52] << 0);
  361. right_back_mag = uint16_t(buf[53] << 8 | buf[54] << 0);
  362. left_front_mag = uint16_t(buf[55] << 8 | buf[56] << 0);
  363. left_back_mag = uint16_t(buf[57] << 8 | buf[58] << 0);
  364. if (len > 59)
  365. {
  366. right_step = uint32_t(buf[59] << 24 | buf[60] << 16 | buf[61] << 8 | buf[62] << 0);
  367. left_step = uint32_t(buf[63] << 24 | buf[64] << 16 | buf[65] << 8 | buf[66] << 0);
  368. }
  369. else
  370. {
  371. right_step = 0;
  372. left_step = 0;
  373. }
  374. GameProcess(gameTime,
  375. right_pos_data, right_att_data, right_acc_data, s_zupt, (int)right_front_mag,
  376. left_pos_data, left_att_data, left_acc_data, h_zupt, (int)left_front_mag,
  377. jump, shoes_down, rssi, girl_shoes);
  378. setGameDataStr();
  379. }
  380. //游戏开始的时候触发的动作
  381. void Game::start(string path_root)
  382. {
  383. string game_name;
  384. if (GameType == RUNGAME)
  385. {
  386. game_name = "rungame";
  387. }
  388. else if (GameType == DANCEGAME)
  389. {
  390. game_name = "dancegame";
  391. }
  392. stringstream ss;
  393. ss << time(NULL);
  394. foot_data_file.open(path_root + '/'+game_name + "_" + ss.str() + ".txt", fstream::out);
  395. }
  396. void Game::end()
  397. {
  398. foot_data_file.close();
  399. }
  400. void Game::isBingo()
  401. {
  402. bingo = 1;
  403. }