#include "Game.h" Game::Game(int gametype) { GameType = gametype; rotateTrajLeft = InertialTrajProcess(); rotateTrajRight = InertialTrajProcess(); leftFootStep = FootStep(); rightFootStep = FootStep(); publicmotion = publicMotion(); rotateMatrix[0] = 1.0f; rotateMatrix[1] = 0.0f; rotateMatrix[2] = 0.0f; rotateMatrix[3] = 1.0f; leftStepRate = 0; rightStepRate = 0; leftRate = 0; rightRate = 0; last_down = 0; last_jump = 0; down_count = 0; jump_count = 0; last_left_step = 0; last_right_step = 0; down_wait = 0; avoid_down_between_jump = 0; interaction = Interaction(); InteractionCMD = -1; shoes_down = 0; if (GameType == RUNGAME) { rungame = RunGame(); } else if (GameType == DANCEGAME) { dancegame = DanceGame(); } else if (GameType == ORIGINTRAJ) { originTraj = OriginTraj(); } else if (GameType == MONSTER) { monsterH5 = MonsterH5(); } else if (GameType == RUINS) { ruinsExplore = RuinsExplore(); } else if (GameType == CYCLE) { tricycle = Tricycle(); } else if (GameType == BIGFOOT) { bigfoot = BigFoot(); } else if (GameType == TESTGAME) { testgame = testGame(); } else if (GameType == H5GAME) { h5 = H5(); } } int Game::getInteractionCMD() { return InteractionCMD; } void Game::processCalorieData(int down, int jump) { if (down) { down_wait = 20; } if (jump) { avoid_down_between_jump = 20; } if (avoid_down_between_jump > 0 && h_zupt == 0 && s_zupt == 0) { avoid_down_between_jump = 20; } if (avoid_down_between_jump > 0 && down == 1) { avoid_down_between_jump = 20; } if (avoid_down_between_jump > 0) { down_wait = 0; down = 0; last_down = 0; } if (down_wait > 0) { down = 1; down_wait --; } if (avoid_down_between_jump) { avoid_down_between_jump--; } if (down == 0 && last_down == 1) { down_count++; std::cout << "蹲的次数 : " << down_count << endl; } if (last_jump == 0 && jump == 1) { jump_count++; std::cout << "跳的次数 : " << jump_count << endl; } last_down = down; last_jump = jump; } int Game::getGameDownCount() { return down_count; } int Game::getGameJumpCount() { return jump_count; } int Game::onlineDownEstimate() { if (posTimeStamp % 8 != 0) { return 0; } } void Game::GameProcess(int timeStamp, int* right_pos, int* right_att, int *right_acc, int right_zupt, int right_press, int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press, int jump, int down, int rssi, int girl_shoes) { //计算步频 leftFootStep.stepCal(timeStamp, left_pos, left_zupt, left_step); rightFootStep.stepCal(timeStamp, right_pos, right_zupt, right_step); std::cout << "left_step count is " << getStepCount(LEFT_FOOT) << endl; //计算卡路里 interaction.Process(gameTime, right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi); InteractionCMD = interaction.get_motion_state(); ////统计游戏里面的蹲跳 processCalorieData(down, jump); if (GameType == RUNGAME) { //跑酷游戏处理函数 rungame.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi); //跑酷游戏处理结果获取 rungame.getResult(resultMatrix); } else if (GameType == DANCEGAME) { dancegame.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi); dancegame.getResult(resultMatrix); } else if (GameType == ORIGINTRAJ) { originTraj.Process(right_pos, right_att, right_zupt, left_pos, left_att, left_zupt); } else if (GameType == MONSTER) { monsterH5.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, rssi); monsterH5.getResult(resultMatrix); } else if (GameType == RUINS) { ruinsExplore.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); //跑酷游戏处理结果获取 ruinsExplore.getResult(resultMatrix); } else if (GameType == CYCLE) { if (left_zupt) { rotateTrajLeft.ResetHeading(left_att[0]); } rotateTrajLeft.TrajRotate(left_pos); if (right_zupt) { rotateTrajRight.ResetHeading(right_att[0]); } rotateTrajRight.TrajRotate(right_pos); tricycle.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); tricycle.getResult(resultMatrix); left_att[0] = resultMatrix[0]; resultMatrix[0] = -1; resultMatrix[1] = -1; } else if (GameType == BIGFOOT) { bigfoot.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); //跑酷游戏处理结果获取 bigfoot.getResult(resultMatrix); } else if (GameType == TESTGAME) { testgame.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); testgame.getResult(resultMatrix); } else if (GameType == H5GAME) { h5.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); h5.getResult(resultMatrix); } /* if (foot_data_file.is_open()&& GameType == DANCEGAME) { foot_data_file << timeStamp << " " << dancegame.getGamePos(LEFT_FOOT, 0) << " " << dancegame.getGamePos(LEFT_FOOT, 1) << " " << left_zupt << " " << dancegame.getGamePos(RIGHT_FOOT, 0) << " " << dancegame.getGamePos(RIGHT_FOOT, 1) << " " << right_zupt << " " << rssi << " " << bingo << std::endl; bingo = 0; } */ } void Game::RunGameProcess(int* right_pos, int* right_att, int* right_acc, int right_zupt, int right_press, int* left_pos, int* left_att, int* left_acc, int left_zupt, int left_press, int jump, int down, int girl_shoes) { rungame.Process(right_pos, right_att, right_acc, right_zupt, right_press, left_pos, left_att, left_acc, left_zupt, left_press, jump, down, girl_shoes); } void Game::getGameResult(int* matrix) { memcpy(matrix, resultMatrix, 4 * sizeof(int)); } int Game::getStepStatus(int left_or_right) { if (left_or_right == LEFT_FOOT) return leftFootStep.getStepStatus(); else if (left_or_right == RIGHT_FOOT) return rightFootStep.getStepStatus(); else return -1; } int Game::getStepFreq( int left_or_right) { if (left_or_right == LEFT_FOOT) return leftFootStep.getStepFreq(); else if (left_or_right == RIGHT_FOOT) return rightFootStep.getStepFreq(); else return -1; } int Game::getStepCount(int left_or_right) { if (left_or_right == LEFT_FOOT) return leftFootStep.getStepCount(); else if (left_or_right == RIGHT_FOOT) return rightFootStep.getStepCount(); else return -1; } float Game::getGamePos(int left_or_right, int index) { if (index < 0 || index >2) return -1; if (GameType == RUNGAME) { return rungame.getGamePos(left_or_right, index); } else if(GameType == DANCEGAME) { return dancegame.getGamePos(left_or_right, index); } else if (GameType == ORIGINTRAJ) { return originTraj.getGamePos(left_or_right, index); } else { return -1; } } string Game::getVersion() { return GAME_VERSION; } string Game::getGameDataStr() { string data_str_temp = ""; //先记录右边 data_str_temp = data_str; data_str = ""; return data_str_temp; } void Game::setGameDataStr() { if (data_str.size() != 0) { data_str = ""; } data_str += (to_string(right_pos_data[0]) + "," + to_string(right_pos_data[1]) + "," + to_string(right_pos_data[2]) + ","); data_str += (to_string(right_att_data[0]) + "," + to_string(right_att_data[1]) + "," + to_string(right_att_data[2]) + ","); data_str += (to_string(right_acc_data[0]) + "," + to_string(right_acc_data[1]) + "," + to_string(right_acc_data[2]) + ","); data_str += (to_string(right_front_mag) + "," + to_string(right_back_mag) + "," + to_string(s_zupt) + ","); data_str += (to_string(left_pos_data[0]) + "," + to_string(left_pos_data[1]) + "," + to_string(left_pos_data[2]) + ","); data_str += (to_string(left_att_data[0]) + "," + to_string(left_att_data[1]) + "," + to_string(left_att_data[2]) + ","); data_str += (to_string(left_acc_data[0]) + "," + to_string(left_acc_data[1]) + "," + to_string(left_acc_data[2]) + ","); data_str += (to_string(left_front_mag) + "," + to_string(left_back_mag) + "," + to_string(h_zupt) + ","); data_str += (to_string(shoes_down) + "," + to_string(jump) + "," + to_string(rssi) + "," + to_string(posTimeStamp) + ","); data_str += (to_string(right_step) + "," + to_string(left_step) + "," + to_string(down) ); } void Game::getFootAtt(int* left_att, int* right_att) { memcpy(right_att, right_att_data, 3 * sizeof(int)); memcpy(left_att, left_att_data, 3 * sizeof(int)); } //解析鞋子的数据 void Game::GameProcessBuf(uint8_t* buf, int len) { static int last_game_time; right_pos_data[0] = int32_t(buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0); right_pos_data[1] = int32_t(buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7] << 0); right_pos_data[2] = int32_t(buf[8] << 24 | buf[9] << 16 | buf[10] << 8 | buf[11] << 0); left_pos_data[0] = int32_t(buf[12] << 24 | buf[13] << 16 | buf[14] << 8 | buf[15] << 0); left_pos_data[1] = int32_t(buf[16] << 24 | buf[17] << 16 | buf[18] << 8 | buf[19] << 0); left_pos_data[2] = int32_t(buf[20] << 24 | buf[21] << 16 | buf[22] << 8 | buf[23] << 0); right_att_data[0] = int16_t(buf[24] << 8 | buf[25] << 0); right_att_data[1] = int16_t(buf[26] << 8 | buf[27] << 0); right_att_data[2] = int16_t(buf[28] << 8 | buf[29] << 0); left_att_data[0] = int16_t(buf[30] << 8 | buf[31] << 0); left_att_data[1] = int16_t(buf[32] << 8 | buf[33] << 0); left_att_data[2] = int16_t(buf[34] << 8 | buf[35] << 0); right_acc_data[0] = int16_t(buf[36] << 8 | buf[37] << 0); right_acc_data[1] = int16_t(buf[38] << 8 | buf[39] << 0); right_acc_data[2] = int16_t(buf[40] << 8 | buf[41] << 0); left_acc_data[0] = int16_t(buf[42] << 8 | buf[43] << 0); left_acc_data[1] = int16_t(buf[44] << 8 | buf[45] << 0); left_acc_data[2] = int16_t(buf[46] << 8 | buf[47] << 0); girl_shoes = (buf[48] & 0x16) >> 4; s_zupt = (buf[48] & 0x08) >> 3; h_zupt = (buf[48] & 0x04) >> 2; shoes_down = (buf[48] & 0x02) >> 1; jump = (buf[48] & 0x01) >> 0; rssi = (int) buf[49]; posTimeStamp = buf[50]; if (lastTimeStamp == -1) { lastTimeStamp = posTimeStamp; return; } if (posTimeStamp - lastTimeStamp > - 100 && posTimeStamp - lastTimeStamp <=0) { std::cout << "receive old shoes package ( lastTimeStamp : " << lastTimeStamp << " posTimeStamp : " << posTimeStamp << " )" < 59) { right_step = uint32_t(buf[59] << 24 | buf[60] << 16 | buf[61] << 8 | buf[62] << 0); left_step = uint32_t(buf[63] << 24 | buf[64] << 16 | buf[65] << 8 | buf[66] << 0); } else { right_step = 0; left_step = 0; } GameProcess(gameTime, right_pos_data, right_att_data, right_acc_data, s_zupt, (int)right_front_mag, left_pos_data, left_att_data, left_acc_data, h_zupt, (int)left_front_mag, jump, shoes_down, rssi, girl_shoes); setGameDataStr(); } //游戏开始的时候触发的动作 void Game::start(string path_root) { string game_name; if (GameType == RUNGAME) { game_name = "rungame"; } else if (GameType == DANCEGAME) { game_name = "dancegame"; } stringstream ss; ss << time(NULL); foot_data_file.open(path_root + '/'+game_name + "_" + ss.str() + ".txt", fstream::out); } void Game::end() { foot_data_file.close(); } void Game::isBingo() { bingo = 1; }