PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/js/DemoBox2D/DemoBox2DClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 144 lines | 72 code | 21 blank | 51 comment | 0 complexity | df1be3c318b642206d29cd888e518b7f MD5 | raw file
  1/**
  2 File:
  3 DemoBox2DClientGame.js
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 DemoBox2D
  8 Abstract:
  9 This is the client/browser side of the DemoBox2D app within RealtimeMultiplayerNodeJS
 10 Basic Usage:
 11 var clientGame = new DemoBox2D.DemoClientGame();
 12 Version:
 13 1.0
 14 */
 15(function () {
 16
 17    DemoBox2D.DemoClientGame = function () {
 18        DemoBox2D.DemoClientGame.superclass.constructor.call(this);
 19
 20        this.startGameClock();
 21        return this;
 22    };
 23
 24    DemoBox2D.DemoClientGame.prototype = {
 25        setupView: function () {
 26            this.view = new DemoBox2D.DemoView(this);
 27            this.view.insertIntoHTMLElementWithId("gamecontainer");
 28            this.view.delegate = this;
 29            DemoBox2D.DemoClientGame.superclass.setupView.call(this);
 30        },
 31
 32        /**
 33         * When the user clicks down, we will create a message and pass that to
 34         * @param aMouseEvent
 35         */
 36        onViewMouseDown: function (aMouseEvent) {
 37            this.netChannel.addMessageToQueue(false, RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE, { x: aMouseEvent.point.x, y: aMouseEvent.point.y });
 38        },
 39
 40        /**
 41         * @inheritDoc
 42         */
 43        tick: function () {
 44            DemoBox2D.DemoClientGame.superclass.tick.call(this);
 45            this.view.stats.update();
 46            this.view.update(this.gameClockReal);
 47        },
 48
 49        /**
 50         * @inheritDoc
 51         */
 52        createEntityFromDesc: function (entityDesc) {
 53            var diameter = entityDesc.radius * DemoBox2D.Constants.PHYSICS_SCALE;
 54            console.log(entityDesc.entityType, DemoBox2D.Constants.ENTITY_TYPES.BOX);
 55
 56            // Tell CAAT to create a circle or box depending on the info we receive
 57            var entityType = (entityDesc.entityType === DemoBox2D.Constants.ENTITY_TYPES.BOX) ? CAAT.ShapeActor.prototype.SHAPE_RECTANGLE : CAAT.ShapeActor.prototype.SHAPE_CIRCLE;
 58
 59            // Create the entity
 60            var newEntity = new DemoBox2D.Box2DEntity(entityDesc.entityid, entityDesc.clientid);
 61            newEntity.position.set(entityDesc.x, entityDesc.y);
 62
 63            // Create a view via CAAT
 64            var anEntityView = new CAAT.ShapeActor();
 65            anEntityView.create();
 66            anEntityView.setShape(entityType);
 67            anEntityView.setSize(diameter, diameter);
 68            anEntityView.setFillStyle("#" + CAAT.Color.prototype.hsvToRgb((entityDesc.entityid * 15) % 360, 40, 99).toHex()); // Random color
 69            anEntityView.setLocation(entityDesc.x, entityDesc.y); // Place in the center of the screen, use the director's width/height
 70
 71            // Set the view
 72            newEntity.setView(anEntityView);
 73
 74            // Add to the fieldcontroller
 75            this.fieldController.addEntity(newEntity);
 76        },
 77
 78        /**
 79         * Called by the ClientNetChannel, it sends us an array containing tightly packed values and expects us to return a meaningful object
 80         * It is left up to each game to implement this function because only the game knows what it needs to send.
 81         * However the 4 example projects in RealtimeMultiplayerNodeJS offer should be used ans examples
 82         *
 83         * @param {Array} entityDescAsArray An array of tightly packed values
 84         * @return {Object} An object which will be returned to you later on tied to a specific entity
 85         */
 86        parseEntityDescriptionArray: function (entityDescAsArray) {
 87            var entityDescription = {};
 88
 89            // It is left upto each game to implement this function because only the game knows what it needs to send.
 90            // However the 4 example projects in RealtimeMultiplayerNodeJS offer this an example
 91            entityDescription.entityid = +entityDescAsArray[0];
 92            entityDescription.clientid = +entityDescAsArray[1];
 93            entityDescription.entityType = +entityDescAsArray[2];
 94            entityDescription.x = +entityDescAsArray[3];
 95            entityDescription.y = +entityDescAsArray[4];
 96            entityDescription.radius = +entityDescAsArray[5];
 97            entityDescription.rotation = +entityDescAsArray[6];
 98
 99            return entityDescription;
100        },
101
102        /**
103         * @inheritDoc
104         */
105        netChannelDidConnect: function (messageData) {
106            DemoBox2D.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
107            DemoBox2D.DemoClientGame.prototype.log("DemoClientGame: Joining Game");
108            this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
109        },
110
111        /**
112         * @inheritDoc
113         */
114        netChannelDidDisconnect: function (messageData) {
115            DemoBox2D.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
116            DemoBox2D.DemoClientGame.prototype.log("DemoClientGame: netChannelDidDisconnect"); // Display disconnect
117        },
118
119        /**
120         * This function logs something to the right panel
121         * @param {Object} An object in the form of: { message: ['Client', 'domReady'] }
122         */
123        log: (function () {
124            var message = function (message) {
125                var el = document.createElement('p');
126                el.innerHTML = '<b>' + esc(message) + ':</b> ';
127
128                // Log if possible
129                console.log(message);
130                document.getElementsByTagName('aside')[0].appendChild(el);
131                document.getElementsByTagName('aside')[0].scrollTop = 1000000;
132            };
133
134            var esc = function (msg) {
135                return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
136            };
137
138            return message;
139        })()
140    };
141
142    // extend RealtimeMultiplayerGame.AbstractClientGame
143    RealtimeMultiplayerGame.extend(DemoBox2D.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
144})();