1 /**
  2  * The Render Engine
  3  * Network Position Component
  4  *
  5  * @fileoverview A simple component which uses the provided socket
  6  *    to transmit the location of the game object to the server.
  7  *
  8  * @author: Brett Fattori (brettf@renderengine.com)
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.components.logic.NetworkClientPosition",
 37     "requires":[
 38         "R.components.Logic"
 39     ]
 40 });
 41 
 42 /**
 43  * @class A very simple example component which sends the game object's
 44  *    client position in the world using a web socket.  You will need a
 45  *    valid socket and a running server to use this component.
 46  *
 47  * @param name {String} The name of the component
 48  * @param [priority=1.0] {Number} The priority of the component
 49  * @extends R.components.Logic
 50  * @constructor
 51  * @description Create a notifier component
 52  */
 53 R.components.logic.NetworkClientPosition = function () {
 54     "use strict";
 55     return R.components.Logic.extend(/** @scope R.components.logic.NetworkClientPosition.prototype */{
 56 
 57         socket:null,
 58         nextInterval:0,
 59         interval:0,
 60 
 61         constructor:function (socket, interval) {
 62             this.socket = null;
 63             this.nextInterval = 0;
 64             this.interval = interval || 20;
 65         },
 66 
 67         execute:function (renderContext, time, dt) {
 68             // Like to keep this from flooding the server
 69             if (time > this.nextInterval) {
 70                 // Get the position of the game object
 71                 var pos = this.getGameObject().getPosition();
 72 
 73                 // Range on either axis is -32767 - 32767
 74                 var signX = pos.x < 0 ? 1 : 0, signY = pos.y < 0 ? 1 : 0;
 75                 var cPos = Math.abs(pos.x) << 16;
 76                 cPos += Math.abs(pos.y) + ((signX << 31) + (signY << 15));
 77 
 78                 // Encode in 4 bytes
 79                 var sPos = String.fromCharCode((cPos & 0xff000000) >> 24,
 80                     (cPos & 0xff0000) >> 16,
 81                     (cPos & 0xff00) >> 8,
 82                     (cPos & 0xff));
 83 
 84                 // Transmit
 85                 this.socket.send("p:" + sPos);
 86                 this.nextInterval = time + this.interval;
 87             }
 88         }
 89     });
 90 };
 91