I have a class "Networking" with two threads:
- the first one sends packets in outgoing list;
- the second receive packets and i have a "handle" method to manage each package.
Handle method:
1 2 3 4 5 6 7 8
| if(packet instanceof PlayerMovePacket) { if(isServer) players[1].incomingCommands.add(new PlayerMoveCommand(((PlayerMovePacket) packet).getDir())); else players[0].incomingCommands.add(new PlayerMoveCommand(((PlayerMovePacket) packet).getDir())); } else if(packet instanceof PlayerFirePacket) { if(isServer) players[1].incomingCommands.add(new PlayerFireCommand(((PlayerFirePacket) packet).getFire())); else players[0].incomingCommands.add(new PlayerFireCommand(((PlayerFirePacket) packet).getFire())); } |
Then, in the Player class i process all commands in "incomingCommands" list and for each command i move the player in that direction.
Do you need anything else?
Thank you!