Jump to content

Phaser Multiplayer problem


YellowRabbit
 Share

Recommended Posts

Hello, I am creating a simple, multiplayer, platform game in Phaser.

I'm looking the best solution for lag.

At the begining I sent to clients current positions of players. It was't be good solution.

Next, I sent to clients velocities. In this solution I saw misalignment effect. One player saw own character at another postion than second player.

Now I use queque of movement. This solution is good, but I think code can be better. Maybe someone has any ideas?

 function distance(x1, y1, x2, y2) {
     var dx = x1 - x2;
     var dy = y1 - y2;
     return Math.sqrt(dx * dx + dy * dy);
 }

 function update() {
  
         if (player.body.velocity.x != 0 && !(leftButton.isDown) && !(rightButton.isDown)) {
             player.body.velocity.x = 0;
             client.ws.send("VELX0|" + parseInt(player.body.x) + "|" + parseInt(player.body.y));
         }
         if (player.body.velocity.x == 0 && !(leftButton.isDown) && (player.body.touching.down) && !(rightButton.isDown)) {
             player.body.velocity.x = 0;
             client.ws.send("VELX0|" + parseInt(player.body.x) + "|" + parseInt(player.body.y));
         }
     
         if (leftButton.isDown && player.body.velocity.x != -300) {
             player.body.velocity.x = -300;
             client.ws.send("VELX-300|" + parseInt(player.body.x) + "|" + parseInt(player.body.y));
         }      
 
        if (rightButton.isDown && player.body.velocity.x != 300) {
                 player.body.velocity.x = 300;
                 client.ws.send("VELX300|" + parseInt(player.body.x) + "|" + parseInt(player.body.y));
        }
              
         if (upButton.isDown && player.body.velocity.y==0 && player.body.touching.down) {
             player.body.velocity.y = -500;
             client.ws.send("VELY-500|" + parseInt(player.body.x) + "|" + parseInt(player.body.y));
         }
       
         if (Object.keys(playersList).length > 1) {
             if (moves.length > 0) {
                 i = moves.shift();
				
                 if (distance(player2.x, player2.y, parseInt(i["x"]), parseInt(i["y"])) <= 10 || moves.length >= 1) {
                     if (i.move === "VELX") {
                         player2.body.x = i["x"];
                         player2.body.y = i["y"];
                         player2.body.velocity.x = i["value"];

                     }
                     if (i.move === "VELY") {
                         player2.body.x = i["x"];
                         player2.body.y = i["y"];
                         player2.body.velocity.y = i["value"];


                     }
                 }
                 else moves.unshift(i);
             } 
         }
     }
 }
 game = new Phaser.Game(900, 580, Phaser.AUTO, 'phaser', {
     preload: preload
     , create: create
     , update: update
 });

 function Client() {}
 Client.prototype.openConnection = function () {
     this.ws = new WebSocket("ws://127.0.0.1/ws/");
     this.connected = false;
     this.ws.onmessage = this.onMessage.bind(this);
     this.ws.onerror = this.displayError.bind(this);
     this.ws.onopen = this.connectionOpen.bind(this);
 };
 Client.prototype.connectionOpen = function () {
     this.connected = true;
 };
 Client.prototype.onMessage = function (message) {
 
     if (message.data.substring(0, 4) == "VELX") {
             z = {
                 x: parseInt(message.data.substring(6).split("|")[2])
                 , y: parseInt(message.data.substring(6).split("|")[3])
                 , move: "VELX"
                 , value: parseInt(message.data.substring(6).split("|")[1])
             }
             moves.push(z);
         
     }
   
     if (message.data.substring(0, 4) == "VELY") {
             z = {
                 x: parseInt(message.data.substring(6).split("|")[2])
                 , y: parseInt(message.data.substring(6).split("|")[3])
                 , move: "VELY"
                 , value: parseInt(message.data.substring(6).split("|")[1])
             }
             moves.push(z);
         
     }
 };
 Client.prototype.displayError = function (err) {
     console.log('Websocketerror: ' + err);
 };

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...