Commits

Ivan Vučica committed 61482ac

Added ball sync.

Comments (0)

Files changed (3)

 var pongGame_velocityBallX;
 var pongGame_velocityBallY;
 
-function pongGame_start(fullJid)
+function pongGame_start(fullJid, isInitiator)
 {
     pongGame_talkTo = fullJid;
     $('#invitations').slideUp().hide();
     
     pongGame_sendUpdate();
     pongGame_velocityBallX = 50;
-    pongGame_velocityBallY = 100;
+    pongGame_velocityBallY = (isInitiator ? 100 : -100);
     pongGame_positionBallX = $('#playfield')[0].offsetWidth/2;
     pongGame_positionBallY = $('#playfield')[0].offsetHeight/2;    
     
     pongGame_needToSendOnMove = false;
     var packet = new zxmpp.packet(zxmpp);
     
-    var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "update");
+    var updateNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "update");
 
     var iq = new zxmpp.stanzaIq(zxmpp);
     iq.appendIqToPacket(packet, /*id base name*/"pongupdate", "set", pongGame_talkTo);
     
     var contentnode = packet.xml.createTextNode(paddle1X);
-    invitationNode.appendChild(contentnode);
+    updateNode.appendChild(contentnode);
     
-    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    var replyHandler = function pongGame_sendUpdateReplyHandler(zxmpp, original, response)
     {
         if(response.type == "error")
             console.error("Got error");
     };
     
     iq.onReply.push(replyHandler);
-    packet.iqXML.appendChild(invitationNode);
+    packet.iqXML.appendChild(updateNode);
     
     packet.send("poll");
 
 ////////////////////////////////////////////////////////
 
 
+function pongGame_sendBallUpdate()
+{
+    // manually called on a collision
+    
+    
+    var data = {
+        'posx': pongGame_positionBallX,
+        'posy': pongGame_positionBallY,
+        'velx': pongGame_velocityBallX,
+        'vely': pongGame_velocityBallY
+    };
+    
+    var packet = new zxmpp.packet(zxmpp);
+    
+    var ballUpdateNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "ballupdate");
+    
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /*id base name*/"pongball", "set", pongGame_talkTo);
+    
+    var contentnode = packet.xml.createTextNode(JSON.stringify(data));
+    ballUpdateNode.appendChild(contentnode);
+    
+    var replyHandler = function pongGame_sendBallUpdateReplyHandler(zxmpp, original, response)
+    {
+        if(response.type == "error")
+            console.error("Got error");
+        /*
+        else if (response.type == "result")
+        {
+            pongGame_sendUpdate();
+        }
+         */
+    };
+    
+    iq.onReply.push(replyHandler);
+    packet.iqXML.appendChild(ballUpdateNode);
+    
+    packet.send("poll");
+    
+}
+function pongGame_getBallUpdate(iqstanza, xml)
+{
+    var data = JSON.parse(xml.textContent);
+    pongGame_positionBallX = data['posx'];
+    pongGame_positionBallY = $('#playfield')[0].offsetHeight - data['posy'];
+    pongGame_velocityBallX = data['velx'];
+    pongGame_velocityBallY = data['vely'] * -1;
+}
+
+
+
+/////////////////////////////////////////////////////
+
 function pongGame_localGameUpdate(timeDelta)
 {
     pongGame_ballUpdate(timeDelta);
 
     if(pongGame_positionBallY + $('#ball')[0].offsetHeight/2 > $('#paddle1')[0].offsetTop)
     {
-        pongGame_velocityBallY *= -1;
         
-        var diff = (pongGame_positionBallY + $('#ball')[0].offsetHeight/2) - $('#paddle1')[0].offsetTop;
-        pongGame_positionBallY = $('#paddle1')[0].offsetTop - diff;
+        if(pongGame_positionBallX + $('#ball')[0].offsetWidth/2 > $('#paddle1')[0].offsetLeft && 
+           pongGame_positionBallX - $('#ball')[0].offsetWidth/2 < $('#paddle1')[0].offsetLeft + $('#paddle1')[0].offsetWidth)
+
+        {
+            pongGame_velocityBallY *= -1;
+            pongGame_velocityBallY *= 1.1;
+            pongGame_velocityBallX = (pongGame_velocityBallY * (-0.5 + Math.random()));
+            
+            var diff = (pongGame_positionBallY + $('#ball')[0].offsetHeight/2) - $('#paddle1')[0].offsetTop;
+            pongGame_positionBallY = $('#paddle1')[0].offsetTop - diff;
+        }
+        else
+        {
+            pongGame_velocityBallX = 50;
+            pongGame_velocityBallY = -100;
+            pongGame_positionBallX = $('#playfield')[0].offsetWidth/2;
+            pongGame_positionBallY = $('#playfield')[0].offsetHeight/2;
+        }
+        
+        pongGame_sendBallUpdate();
+        
     }
+    /*
     if(pongGame_positionBallY - $('#ball')[0].offsetHeight/2 < $('#paddle2')[0].offsetTop + $('#paddle2')[0].offsetHeight)
     {
-        pongGame_velocityBallY *= -1;
+        if(pongGame_positionBallX + $('#ball')[0].offsetWidth/2 > $('#paddle2')[0].offsetLeft && 
+           pongGame_positionBallX - $('#ball')[0].offsetWidth/2 < $('#paddle2')[0].offsetLeft + $('#paddle2')[0].offsetWidth)
+        {
+            pongGame_velocityBallY *= -1;
+            
+            var diff = pongGame_positionBallY - $('#ball')[0].offsetHeight/2 - ($('#paddle2')[0].offsetTop + $('#paddle2')[0].offsetHeight); // negative number
+            pongGame_positionBallY = $('#paddle2')[0].offsetTop + $('#paddle2')[0].offsetHeight - diff;
+        }
+        else
+        {
+            pongGame_velocityBallX = 50;
+            pongGame_velocityBallY = 100;
+            pongGame_positionBallX = $('#playfield')[0].offsetWidth/2;
+            pongGame_positionBallY = $('#playfield')[0].offsetHeight/2;
+        }
         
-        var diff = pongGame_positionBallY - $('#ball')[0].offsetHeight/2 - ($('#paddle2')[0].offsetTop + $('#paddle2')[0].offsetHeight); // negative number
-        pongGame_positionBallY = $('#paddle2')[0].offsetTop + $('#paddle2')[0].offsetHeight - diff;
+        pongGame_sendBallUpdate();
     }
     
+     */
     if(pongGame_positionBallX + $('#ball')[0].offsetWidth/2 > $('#playfield')[0].offsetWidth)
     {
         pongGame_velocityBallX *= -1;
 
 
 
-

pong-invitations.js

     
     zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
 
-    pongGame_start(fullJid);
+    var isInitiator = true;
+    pongGame_start(fullJid, isInitiator);
 }
 function pongInvitation_receiveDecline(fullJid)
 {
     var iq = new zxmpp.stanzaIq(zxmpp);
     iq.appendIqToPacket(packet, /*id base name*/"pongaccept", "set", fullJid);
     
-    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    var replyHandler = function pongInvitation_sendAcceptReplyHandler(zxmpp, original, response)
     {
         if(response.type == "error")
             pongInvitation_receiveError(original.to);
             
             zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
 
-            pongGame_start(fullJid);
+            var isInitiator = false;
+            pongGame_start(fullJid, isInitiator);
             
             for(var invite in pongInvitations)
             {
     zxmpp.addIqParser("accept#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqAccept);
     zxmpp.addIqParser("decline#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqDecline);
     zxmpp.addIqParser("update#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqUpdate);
+    zxmpp.addIqParser("ballupdate#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqBallUpdate);
 
     zxmpp.main(cfg, user, password);
     zxmpp.setOwnPresence("avail", "Working on TVZ Pong", 1);
     return true;
 }
 
+function pongZXMPP_onIqBallUpdate(zxmpp, iqstanza, xml)
+{
+    pongGame_getBallUpdate(iqstanza, xml);
+    
+    // send confirmation
+    var packet = new zxmpp.packet(zxmpp);
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /* id */false, /* type */ "result", /* destination */iqstanza.from, iqstanza.id);
+    packet.send("poll");
+    return true;
+}
+
 
 function pongZXMPP_onPacket(sender, packet)
 {