当前位置: 首页>>代码示例>>C++>>正文


C++ PlayerInfo::unpack方法代码示例

本文整理汇总了C++中PlayerInfo::unpack方法的典型用法代码示例。如果您正苦于以下问题:C++ PlayerInfo::unpack方法的具体用法?C++ PlayerInfo::unpack怎么用?C++ PlayerInfo::unpack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PlayerInfo的用法示例。


在下文中一共展示了PlayerInfo::unpack方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: handleLobby

void ServerPacketHandlerSystem::handleLobby()
{
	while( m_server->hasNewPackets() )
	{
		Packet packet = m_server->popNewPacket();

		char packetType;
		packetType = packet.getPacketType();

		if(packetType == (char)PacketType::ChangeStatePacket){
			ChangeStatePacket statePacket;
			statePacket.unpack(packet);

			if(statePacket.m_gameState == GameStates::INITGAME){

				ChangeStatePacket newState;
				newState.m_serverState = ServerStates::LOADING;
				m_stateSystem->setQueuedState(ServerStates::LOADING);
				m_server->broadcastPacket(newState.pack());
			}
		}
		else if(packetType == (char)PacketType::PlayerInfo){
			PlayerInfo playerInfo;
			playerInfo.unpack(packet);

			PlayerSystem* playerSystem = static_cast<PlayerSystem*>
				(m_world->getSystem(SystemType::PlayerSystem));

			vector<Entity*> connectedPlayers = playerSystem->getActiveEntities();

			//Add the entity here to be used by other systems
			Entity* newPlayer = m_world->createEntity();
			PlayerComponent* newComp = new PlayerComponent();
			if( playerInfo.playerName == "brightestmind" && playerInfo.playerID == 0){
				newComp->setAbsoluteScore(9001);
			}
			else if(playerInfo.playerName=="judas"){
				newComp->setAbsoluteScore(-9001);
			}
			newComp->m_playerName	= playerInfo.playerName;
			newComp->m_playerID		= playerInfo.playerID; //connectedPlayers.size();
			newComp->m_networkID	= packet.getSenderId();
			newPlayer->addComponent(newComp);
			m_world->addEntity(newPlayer);

			NewlyConnectedPlayerPacket connectedPlayer;
			connectedPlayer.playerName	= newComp->m_playerName;
			connectedPlayer.playerID	= newComp->m_playerID;
			connectedPlayer.score		= newComp->getScore();
			connectedPlayer.networkID	= newComp->m_networkID;
			// Broadcast the player to all clients.
			m_server->broadcastPacket(connectedPlayer.pack());

			for (unsigned int i = 0; i < connectedPlayers.size(); i++){
				NewlyConnectedPlayerPacket alreadyConnectedPlayers;
				PlayerComponent* playerComp;
				playerComp = static_cast<PlayerComponent*>
					(connectedPlayers[i]->getComponent(ComponentType::PlayerComponent));
				alreadyConnectedPlayers.playerID	= playerComp->m_playerID;
				alreadyConnectedPlayers.playerName	= playerComp->m_playerName;
				alreadyConnectedPlayers.networkID	= playerComp->m_networkID;

				// Send all the existing players to the new client.
				m_server->unicastPacket(alreadyConnectedPlayers.pack(), newComp->m_networkID);
				
				//m_server->broadcastPacket(alreadyConnectedPlayers.pack());
			}

			// Force all players to be set unready.
			memset(&m_lobbyPlayerReadyStates, 0, sizeof(m_lobbyPlayerReadyStates));
		}
		else if(packetType == (char)PacketType::ClientDisconnect){
			DisconnectPacket dcPacket;
			dcPacket.unpack(packet);

			// Remove client!
			PlayerSystem* playerSystem = static_cast<PlayerSystem*>
				(m_world->getSystem(SystemType::PlayerSystem));
			playerSystem->deletePlayerEntity(dcPacket.playerID);

			// Broadcast the dc packet back to all clients, including the one who sent it.
			m_server->broadcastPacket(packet);
			// Force all players to be set unready.
			memset(&m_lobbyPlayerReadyStates, 0, sizeof(m_lobbyPlayerReadyStates));

			m_world->getOutputLogger()->write(("Server detected a disconnect packet for player: " + toString(dcPacket.playerID) + "\n").c_str());
		}
		else if(packetType == (char)PacketType::PlayerReadyPacket){
			// Broadcast the ready packet back to all clients, including the one who sent it.
			m_server->broadcastPacket(packet);

			PlayerReadyPacket readyPacket;
			readyPacket.unpack(packet);

			m_lobbyPlayerReadyStates[readyPacket.playerId] = readyPacket.ready;
			// Check so that all players are ready!
			m_readyLobbyPlayers = 0;
			for (int i = 0; i < MAXPLAYERS; i++)
			{
				if (m_lobbyPlayerReadyStates[i])
//.........这里部分代码省略.........
开发者ID:MattiasLiljeson,项目名称:Amalgamation,代码行数:101,代码来源:ServerPacketHandlerSystem.cpp


注:本文中的PlayerInfo::unpack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。