Hey all,
So I’m implementing multiplayer movement of other characters in my game now, I have the server getting new players logging in and storing them in a local copy of the player. My question now is how to do the movement updating. I know how to do it, just create a packet and send it over the server to everyone to update. But, I don’t want to be sending a huge number of packets every second for every player. That just seems like it will overload my server and crash.
My question to you all is: how could I go about doing this efficiently?
I’m posting a snippet of my movement code below:
public void updatePlayer(int delta)
{
boolean isWalking = false;
float xChange = 0, yChange = 0;
Input input = gc.getInput();
int runMod = 1;
if (input.isKeyDown(Input.KEY_LSHIFT))
{
runMod = 2;
}
if(input.isKeyDown(Input.KEY_A))
{
//LEFT
sprite = walkingAnimLeft;
sprite.start();
xChange -= speed * runMod * delta;
isWalking = true;
facing = "left";
}
if(input.isKeyDown(Input.KEY_D))
{
//RIGHT
sprite = walkingAnimRight;
sprite.start();
xChange += speed * runMod * delta;
isWalking = true;
facing = "right";
}
if(input.isKeyDown(Input.KEY_W))
{
//UP
sprite = walkingAnimUp;
sprite.start();
yChange -= speed * runMod * delta;
isWalking = true;
facing = "up";
}
if(input.isKeyDown(Input.KEY_S))
{
//DOWN
sprite = walkingAnimDown;
sprite.start();
yChange += speed * runMod * delta;
isWalking = true;
facing = "down";
}
if (input.isKeyPressed(input.KEY_ENTER))
{
if (chat.isChatting())
{
chat.sendText();
}
else
{
chat.setChatting(true);
}
//chat.setChatting();
}
if(input.isKeyPressed(input.KEY_E))
{
toggleEquip();
}
//If the player is not walking, stop the animation
if (isWalking && !currentMap.blocked(x+xChange, y+yChange, facing))
{
x += xChange;
y += yChange;
//Here's where I would put it, but as you can see updatePlayer is called all the time, so I'd be sending a packet all the time for each player.
}
if (!isWalking)
{
sprite.setCurrentFrame(1);
sprite.stop();
}
isWalking = false;
}
I was thinking of using some sort of timer and basically lumping the movements into one packet. I.e., aggregating movement updates across X milliseconds into one packet and sending those at once. But this would require a timer of some sort to count every, 20 milliseconds, let’s say. The problem there is, I would need the timer to be accurate on each and every client. And no, I haven’t just tried sending it every update, because I’m scared of it crashing hardcore.
Any advice?