Hello, guys.
This is more of a code-design question, but it's tightly connected to networking, that's why I decided to post the question here.
I'm wondering how to deal with packets when I receive them and how to pass them to the correct player. Right now I have only one player and I do the packet receiving like this:
void NetworkManager::serverReceivePackets( Player& player )
{
for( packet = peer->Receive(); packet; peer->DeallocatePacket( packet ), packet = peer->Receive() )
{
if( packet->data[ 0 ] == PLAYER_MOVE_FORWARD )
{ player.mainCam.setCurrentPosition( player.mainCam.getCurrentPosition() - player.moveHelperCam.getDirection() ); }
else if( packet->data[ 0 ] == PLAYER_MOVE_BACKWARD )
{ player.mainCam.setCurrentPosition( player.mainCam.getCurrentPosition() + player.moveHelperCam.getDirection() ); }
else if( packet->data[ 0 ] == PLAYER_MOVE_LEFT )
{ player.mainCam.setCurrentPosition( player.mainCam.getCurrentPosition() + player.mainCam.getDirectionRight() ); }
else if( packet->data[ 0 ] == PLAYER_MOVE_RIGHT )
{ player.mainCam.setCurrentPosition( player.mainCam.getCurrentPosition() - player.mainCam.getDirectionRight() ); }
else if( packet->data[ 0 ] == ID_CONNECTION_REQUEST_ACCEPTED )
{
printf( "Connection request accepted.\n" );
}
else if( packet->data[ 0 ] == ID_NEW_INCOMING_CONNECTION )
{
printf( "New connection incoming\n" );
}
}
}
This way of using the received packets is very problematic due to two reasons:
The first reason is that I update the simulation logic in my NetworkManager class, which is not good in my opinion because the logic is scattered all over the place. The logic should only be updated when I call player.updateMovement(), and not in class NetworkManager's functions, because it gets really hard for me to follow what's happening.
Second reason is that I get a circular dependency because I need to pass the Player object as an argument to the NetworkManager in order to update its position, and then I need to pass a NetworkManager object as an argument to some of the Player's functions.
I need somehow to store all the received data in the NetworkManager without updating anything or passing any objects as arguments.
Then I need to use that stored data and pass it into the correct Player object.( I want to have 2 or more players )
Guys,
First, thanks for taking the time to read this. ^_^
Second, what do you suggest? :huh: