Esempio n. 1
0
void Game::onPlayersData(sf::Packet & packet)
{
	std::cout << "players data received!\n";
	Entity::Type myType;
	packet >> myType;


	if (myType == Entity::Type::Zombie)
	{
		Entity::ID myID;
		packet >> myID;
		Zombie * me = mGameWorld.createEntity<Zombie>(myID, myType);
		mGameWorld.setPlayerEntity(me->getID());
		mPlayer.setEntity(me);


		sf::Int32 humanCount;
		packet >> humanCount;
		std::cout << "You are the zombie! human count: " << humanCount << std::endl;


		for (sf::Int32 i = 0; i < humanCount; ++i)
		{
			Entity::ID id;
			packet >> id;
			Human * h = mGameWorld.createEntity<Human>(id, Entity::Type::Human);
			mGameWorld.addEntity(h->getID());
		}
	}
Esempio n. 2
0
void Game::initializePlayers()
{
	//send game map data
	//send players data


	sf::Packet * packet = new sf::Packet;
	*packet << Sv::GameMapData << mGameWorld.mGameMap.getName();
	const std::vector<Entity *> & walls = mGameWorld.getEntitiesOfType(Entity::Type::Wall);
	*packet << sf::Int32(walls.size());
	for (Entity * e : walls)
	{
		*packet << e->getID();
		*packet << e->getPosition().x << e->getPosition().y << e->getSize().x << e->getSize().y;
	}
	pushPacket(nullptr, packet, true);

	//========================================//
	std::vector<Peer *> players;
	for (auto & player : mPlayers)
		players.push_back(player.first);

	//choose a zombie
	Zombie * z = mGameWorld.createEntity<Zombie>(Entity::Type::Zombie);
	Peer * zombiePlayer = players[thor::random(0u, players.size() - 1)];
	mGameWorld.addEntity(z->getID());
	mGameWorld.mEntitiesByPeer[zombiePlayer] = z->getID();


	//remove the zombie player from players vector
	players.erase(std::remove(players.begin(), players.end(), zombiePlayer), players.end());


	//rest are human
	for (Peer * human : players)
	{
		Human * h = mGameWorld.createEntity<Human>(Entity::Type::Human);
		mGameWorld.mEntitiesByPeer[human] = h->getID();
		mGameWorld.addEntity(h->getID());
	}


	//@zombie
	sf::Packet * packet2 = new sf::Packet;               //zombie id, other human player size
	*packet2 << Sv::PlayersData << Entity::Type::Zombie << z->getID() << sf::Int32(players.size());
	for (Peer * human : players)
	{
		//id, 
		*packet2 << mGameWorld.mEntitiesByPeer[human];
	}
	pushPacket(zombiePlayer, packet2);

	std::cout << "Zombie ID: " << z->getID() << "Human count: " << players.size() << std::endl;
	
	//@human
	for (Peer * human : players)
	{
		sf::Packet * packet3 = new sf::Packet;        //      zombie id   player id  other human player size
		*packet3 << Sv::PlayersData << Entity::Type::Human << z->getID() << mGameWorld.mEntitiesByPeer[human] << sf::Int32(players.size() - 1);

		//insert other's players info
		for (Peer * human2 : players)
		{  //other player's id
			if (human2 != human)
				*packet3 << mGameWorld.mEntitiesByPeer[human2];
		}
		pushPacket(human, packet3);
	}

	sf::Packet * packet4 = new sf::Packet;
	*packet4 << Sv::StartGame;
	pushPacket(nullptr, packet4, true);
}