/** * 添加一个旁观玩家到房间中 * * @param pPlayer 要添加的玩家 * @param roomIndex 玩家要进入的房间的索引 * @param chairIndex 玩家要进入的房间的椅子的索引 */ bool RoomManager::AddLookOnPlayer(Player *pPlayer,int roomIndex,int chairIndex) { if(pPlayer == NULL || m_RoomList.empty()) return false; // 首先检测玩家当前的状态,如果玩家处于准备,等待,游戏中,掉线等状态时说明玩家已经在房间中,不能进入新的房间 if(pPlayer->GetState() != PLAYERSTATE_NORAML) return false; // 玩家要进入指定房间,指定的座位 Room *pRoom = GetRoomById(roomIndex); if(pRoom == NULL) return false; // 如果房间已满或者房间状态处于游戏中的话,是进不了房间的 if(/*pRoom->IsFull() ||*/ pRoom->GetRoomState() != ROOMSTATE_GAMING) return false; // 检测房间中指定索引的玩家是否存在,如果存在是不能加入房间的 if(!pRoom->GetPlayer(chairIndex)) return false; // 更新一下房间中的用户 pRoom->UpdateOnlinePlayers(); pPlayer->SetRoomId(pRoom->GetID()); pPlayer->SetRoomIndex(pRoom->AddLookOnPlayer(pPlayer,chairIndex)); pPlayer->SetLookOn(true); return true; }
/** * 改变一个玩家的游戏房间 * * @param pPlayer 要改变游戏房间的玩家 */ bool RoomManager::ChangeGameRoom(Player *pPlayer) { if(pPlayer == NULL || m_RoomList.empty()) return false; // 首先检测玩家当前的状态,如果玩家处于准备,等待,游戏中,掉线等状态时说明玩家已经在房间中,不能进入新的房间 if(pPlayer->GetState() != PLAYERSTATE_NORAML) return false; // 得到原来玩家所在的房间 Room *pSrcRoom = GetRoomById(pPlayer->GetRoomId()); if(pSrcRoom == NULL || pSrcRoom->GetRoomState() != ROOMSTATE_WAITING) return false; // 向房间中其它用户发送用户离开消息 CMolMessageOut out(IDD_MESSAGE_FRAME); out.writeShort(IDD_MESSAGE_LEAVE_ROOM); out.writeShort(pPlayer->GetID()); ServerPlayerManager.SendMsgToEveryone(out); // 先处理用户在原来房间中离开消息 pSrcRoom->OnProcessLeaveRoomMsg(pPlayer->GetRoomIndex()); // 第一遍搜索当前房间中已经存在玩家在那里等待的房间 bool state = false; m_RoomLock.Acquire(); std::list<Room*>::iterator iter = m_RoomList.begin(); for(;iter != m_RoomList.end();iter++) { if((*iter)) { //(*iter)->UpdateAllPlayers(); // 如果玩家不能满足房间要求的最少金币值,就不能进入房间 if((*iter)->GetLastMoney() > pPlayer->GetMoney()) continue; if(!(*iter)->IsFull() && (*iter)->GetPlayerCount() > 0 && (*iter)->GetRoomState() != ROOMSTATE_GAMING) { // 找一个与原来不一样的房间 if(pSrcRoom->GetID() != (*iter)->GetID()) { //PlayerState oldstate = pPlayer->GetState(); // 设置玩家状态为改变房间状态,这样可以防止服务器将玩家强行掉线 //pPlayer->SetState(PLAYERSTATE_CHANGEROOM); // 先从原来的房间中删除这个玩家 pSrcRoom->ClearPlayer(pPlayer); // 更新一下房间中的用户 (*iter)->UpdateOnlinePlayers(); pPlayer->SetRoomId((*iter)->GetID()); //pPlayer->SetState(oldstate); int playerIndex = (*iter)->AddPlayer(pPlayer); pPlayer->SetRoomIndex(playerIndex); state = true; break; } } } } m_RoomLock.Release(); if(state) return state; // 第二遍搜索当前房间中没有坐满的房间或者空房间 state = false; m_RoomLock.Acquire(); iter = m_RoomList.begin(); for(;iter != m_RoomList.end();iter++) { if((*iter)) { //(*iter)->UpdateAllPlayers(); // 如果玩家不能满足房间要求的最少金币值,就不能进入房间 if((*iter)->GetLastMoney() > pPlayer->GetMoney()) continue; if(!(*iter)->IsFull() && (*iter)->GetRoomState() != ROOMSTATE_GAMING) { // 找一个与原来不一样的房间 if(pSrcRoom->GetID() != (*iter)->GetID()) { //PlayerState oldstate = pPlayer->GetState(); // 设置玩家状态为改变房间状态,这样可以防止服务器将玩家强行掉线 //pPlayer->SetState(PLAYERSTATE_CHANGEROOM); // 先从原来的房间中删除这个玩家 pSrcRoom->ClearPlayer(pPlayer); // 更新一下房间中的用户 (*iter)->UpdateOnlinePlayers(); if(pPlayer == NULL) { state = false; break; } pPlayer->SetRoomId((*iter)->GetID()); //pPlayer->SetState(oldstate); int playerIndex = (*iter)->AddPlayer(pPlayer); pPlayer->SetRoomIndex(playerIndex); state = true; break; } } } } m_RoomLock.Release(); return state; }
/** * 添加一个玩家到房间中 * * @param pPlayer 要添加的玩家 * @param roomIndex 玩家要进入的房间的索引 * @param chairIndex 玩家要进入的房间的椅子的索引 */ bool RoomManager::AddPlayer(Player *pPlayer,int roomIndex,int chairIndex) { if(pPlayer == NULL || m_RoomList.empty()) return false; // 首先检测玩家当前的状态,如果玩家处于准备,等待,游戏中,掉线等状态时说明玩家已经在房间中,不能进入新的房间 if(pPlayer->GetState() != PLAYERSTATE_NORAML) return false; // 如果roomIndex等于-1,chairIndex等于-1表示当前玩家不需要排队进入房间 if(roomIndex == -1 && chairIndex == -1) { // 第一遍搜索当前房间中已经存在玩家在那里等待的房间 bool state = false; m_RoomLock.Acquire(); std::list<Room*>::iterator iter = m_RoomList.begin(); for(;iter != m_RoomList.end();iter++) { if((*iter)) { //(*iter)->UpdateAllPlayers(); // 如果玩家不能满足房间要求的最少金币值,就不能进入房间 if((*iter)->GetLastMoney() > pPlayer->GetMoney()) continue; // 如果当前不是百人房间,那么房间状态必须不在游戏状态下 if(CMolServerDlg::getSingleton().GetServerSet()->TableCount > 1) { if((*iter)->GetRoomState() == ROOMSTATE_GAMING) { state = false; continue; } } if(!(*iter)->IsFull() && (*iter)->GetPlayerCount() > 0) { // 更新一下房间中的用户 (*iter)->UpdateOnlinePlayers(); if(pPlayer == NULL) { state = false; break; } //char str[1024]; //sprintf(str,"玩家%d进入房间%d,ID为%d,房间中人数:%d",pPlayer->GetConnectID(),(*iter)->GetID(),pPlayer->GetRoomIndex(),(*iter)->GetPlayerCount()); //LOG_ERRORR(str); pPlayer->SetRoomId((*iter)->GetID()); int playerIndex = (*iter)->AddPlayer(pPlayer); pPlayer->SetRoomIndex(playerIndex); state = true; break; } } } m_RoomLock.Release(); if(state) return state; // 第二遍搜索当前房间中没有坐满的房间或者空房间 state = false; m_RoomLock.Acquire(); iter = m_RoomList.begin(); for(;iter != m_RoomList.end();iter++) { if((*iter)) { //(*iter)->UpdateAllPlayers(); // 如果玩家不能满足房间要求的最少金币值,就不能进入房间 if((*iter)->GetLastMoney() > pPlayer->GetMoney()) continue; // 如果当前不是百人房间,那么房间状态必须不在游戏状态下 if(CMolServerDlg::getSingleton().GetServerSet()->TableCount > 1) { if((*iter)->GetRoomState() == ROOMSTATE_GAMING) { state = false; continue; } } if(!(*iter)->IsFull()) { // 更新一下房间中的用户 (*iter)->UpdateOnlinePlayers(); if(pPlayer == NULL) { state = false; break; } pPlayer->SetRoomId((*iter)->GetID()); int playerIndex = (*iter)->AddPlayer(pPlayer); pPlayer->SetRoomIndex(playerIndex); state = true; break; } } } m_RoomLock.Release(); return state; } else { // 玩家要进入指定房间,指定的座位 Room *pRoom = GetRoomById(roomIndex); if(pRoom == NULL) return false; // 如果房间已满或者房间状态处于游戏中的话,是进不了房间的 if(pRoom->IsFull() || pRoom->GetRoomState() == ROOMSTATE_GAMING) return false; // 检测房间中指定索引的玩家是否存在,如果存在是不能加入房间的 if(pRoom->GetPlayer(chairIndex)) return false; // 更新一下房间中的用户 pRoom->UpdateOnlinePlayers(); pPlayer->SetRoomId(pRoom->GetID()); int playerIndex = pRoom->AddPlayer(pPlayer,chairIndex); pPlayer->SetRoomIndex(playerIndex); return true; } return true; }
void StageState::Input() { if ((InputManager::GetInstance().KeyPress(SDLK_ESCAPE)) || (InputManager::GetInstance().ShouldQuit())){ PauseState *state = new PauseState(); Game::GetInstance().Push(state); } if (InputManager::GetInstance().KeyPress(SDLK_c)){ Point tile(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); tile = tileMap.GetTile(tile.x, tile.y); CreateCharacter(tile.x, tile.y); } if (InputManager::GetInstance().KeyPress(SDLK_d)){ DestroyCharacter(objectArray.size()-1); } if (InputManager::GetInstance().KeyPress(SDLK_RETURN)){ CheatState *state = new CheatState(this, data); Game::GetInstance().Push(state); } if(InputManager::GetInstance().MouseRelease(SDL_BUTTON_LEFT)){ switch (action){ case NONE: ptile = tileMap.GetTile(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); p = tileMap.GetTileCenter(ptile); //objectArray[0]->AddObjective(p.x, p.y, ptile); SelectCharacter(); for (int i = 0; i < roomArray.size(); i++){ roomArray[i]->EditRoom(false); for (int j = 0; j < objectArray.size(); j++){ if (objectArray[j]->Is("Wall")){ objectArray[j]->Editing(false); } } } break; case GUI_A: if(gui.BuildIconPressed()){ click.Play(1); action = GUI_ROOM; gui.SetState(ROOMS); } else{ if (gui.DestroyIconPressed()){ click.Play(1); action = DESTROY_ROOM; } else{ if (gui.BuyIconPressed()){ click.Play(1); action = BUY; } else{ if (gui.EditIconPressed()){ click.Play(1); subGuiEdit.SetPositionSubGUI(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY()); action = SUB_GUI_EDIT; subGuiEdit.SetState(EDIT); } else { action = NONE; } } } } break; case SUB_GUI_EDIT: if (subGuiEdit.DoorIconPressed()){ click.Play(1); cout << "editando porta..." << endl; action = EDIT_DOOR; } else{ if (subGuiEdit.WallIconPressed()){ click.Play(1); cout << "editando parede..." << endl; action = EDIT_WALL; } else { action = NONE; } } break; case TILE_SELECT: break; case GUI_ROOM: if (!roomSheet.IsInside(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())){ action = NONE; } if (roomSheet.IsBuy(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())){ action = TILE_SELECT; roomAttributes = roomSheet.GetAttributes(); selectionBox.SetSprite(roomAttributes.tileSprite); } gui.SetState(BASIC); break; case CONSTRUCT_ROOM: { action = NONE; Point aux; if (selectionBox.begin.y > selectionBox.end.y) { aux = selectionBox.begin; selectionBox.begin = selectionBox.end; selectionBox.end = aux; } if ((selectionBox.begin.x > selectionBox.end.x) && (selectionBox.begin.y < selectionBox.end.y)) { aux = selectionBox.begin; selectionBox.begin.x = selectionBox.end.x; selectionBox.end.x = aux.x; } RoomAttributes asd; asd.cost = 50; asd.tileSprite = "img/tileset/tile_madeira.png"; asd.type = "Pirate"; Room *newRoom = new Room(selectionBox.begin, selectionBox.end, &tileMap, &objectArray, roomArray.size(), roomAttributes); data->money -= newRoom->cost; roomArray.emplace_back(newRoom); vector<int> heuristc = occupancyMap.CreateHeuristic(&tileMap, roomArray[roomArray.size() - 1]->GetDoor()); heuristicsArray.emplace(newRoom->GetID(),heuristc); obstacleMap = occupancyMap.Update(&tileMap, &objectArray); //Point tileC = tileMap.GetTile(objectArray[0]->box.x + objectArray[0]->box.w / 2, objectArray[0]->box.y + objectArray[0]->box.h); //vector<int> path = PathAStar(tileC.x, tileC.y, newRoom->GetID()); //objectArray[0]->AddObjective(path); } break; case DESTROY_ROOM: for (int i = 0; i < roomArray.size(); i++){ p = tileMap.GetTile(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); if (roomArray[i]->IsInside(p)){ heuristicsArray.erase(roomArray[i]->GetID()); cout << "Destroying..." << endl; DestroyRoom(roomArray[i]->GetID()); roomArray.erase(roomArray.begin() + i); i = roomArray.size(); } } action = NONE; break; case BUY: if (!buySheet.IsInside(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())){ action = NONE; } if (buySheet.IsBuy(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())){ Point Tiles = Point(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); Tiles = tileMap.GetTileCenterFromScreen(Tiles); Object* object = new Object(Tiles.x, Tiles.y, buySheet.GetAttributes(), tileMap); selectedObject = objectArray.size(); objectArray.emplace_back(object); action = EDIT_OBJECT; } //data->money -= 100; break; case AREA_SELECT: break; case EDIT_DOOR: for (int i = 0; i < roomArray.size(); i++){ p = tileMap.GetTile(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); if (roomArray[i]->IsInside(p)){ roomArray[i]->EditRoom(true); for (int j = 0; j < objectArray.size(); j++){ if (objectArray[j]->Is("Wall")){ if ((p.x == objectArray[j]->tile.x) && (p.y == objectArray[j]->tile.y)) roomArray[i]->SetDoor(p.x, p.y, roomArray[i]->GetID(), &objectArray); } } for (int j = 0; j < objectArray.size(); j++){ if (objectArray[j]->Is("Wall")){ if (objectArray[j]->roomID == roomArray[i]->GetID()) { objectArray[j]->Editing(true); if (objectArray[j]->tile.DisPoints(objectArray[j]->tile, roomArray[i]->GetDoor()) == 0) { objectArray.erase(objectArray.begin() + j); } } } } vector<int> heuristc = occupancyMap.CreateHeuristic(&tileMap, roomArray[i]->GetDoor()); heuristicsArray[roomArray[i]->GetID()] = heuristc; obstacleMap = occupancyMap.Update(&tileMap, &objectArray); obstacleMap = occupancyMap.CleanTile(&tileMap, roomArray[i]->GetDoor()); } } action = NONE; break; case EDIT_WALL: for (int i = 0; i < roomArray.size(); i++){ p = tileMap.GetTile(InputManager::GetInstance().GetMouseX() - Camera::pos.x, InputManager::GetInstance().GetMouseY() - Camera::pos.y); if (roomArray[i]->IsInside(p)){ roomArray[i]->EditRoom(true); for (int j = 0; j < objectArray.size(); j++){ if (objectArray[j]->Is("Wall")){ if (objectArray[j]->roomID == roomArray[i]->GetID()) { objectArray[j]->Editing(true); } } } } } action = NONE; break; case EDIT_OBJECT: if (objectArray[selectedObject]->SettlePos(obstacleMap)){ obstacleMap = occupancyMap.Update(&tileMap, &objectArray); action = NONE; } default: break; } } if(InputManager::GetInstance().MousePress(SDL_BUTTON_RIGHT)){ if (sheet.GetRender() || objSheet.GetRender()){ if (!sheet.GetBox().IsInside(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())){ sheet.SetRender(false); Camera::Unfollow(); } if (!objSheet.GetBox().IsInside(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY())) objSheet.SetRender(false); } else{ switch (action) { case NONE: gui.SetPosition(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY()); action = GUI_A; break; case TILE_SELECT: break; case CONSTRUCT_ROOM: break; case DESTROY_ROOM: break; case AREA_SELECT: break; case GUI_A: gui.SetPosition(InputManager::GetInstance().GetMouseX(), InputManager::GetInstance().GetMouseY()); break; case SUB_GUI_EDIT: action = NONE; break; default: break; } } } if(InputManager::GetInstance().IsMouseDown(SDL_BUTTON_LEFT)){ switch (action){ case NONE: break; case TILE_SELECT: action = CONSTRUCT_ROOM; break; case CONSTRUCT_ROOM: break; case DESTROY_ROOM: break; case AREA_SELECT: break; default: break; } } if(InputManager::GetInstance().MouseRelease(SDL_BUTTON_RIGHT)){ switch (action){ case NONE: //action = TILE_SELECT; break; case TILE_SELECT: break; case CONSTRUCT_ROOM: break; case DESTROY_ROOM: break; case AREA_SELECT: break; default: break; } } }