PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/js/BubbleDots/BubbleDotsClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 135 lines | 69 code | 20 blank | 46 comment | 3 complexity | ffd895aef61ca235d5bac127bfbce19e MD5 | raw file
  1/**
  2 File:
  3 DemoServerGame
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 BubbleDots
  8 Abstract:
  9 This is a concrete server instance of our game
 10 Basic Usage:
 11 DemoServerGame = new BubbleDots.DemoServerGame();
 12 DemoServerGame.start();
 13 DemoServerGame.explodeEveryone();
 14 Version:
 15 1.0
 16 */
 17(function () {
 18
 19    BubbleDots.DemoClientGame = function () {
 20        BubbleDots.DemoClientGame.superclass.constructor.call(this);
 21
 22        this.startGameClock();
 23        return this;
 24    };
 25
 26    BubbleDots.DemoClientGame.prototype = {
 27        setupView: function (images) {
 28            this.view = new BubbleDots.DemoView(images);
 29            this.view.insertIntoHTMLElementWithId("gamecontainer");
 30
 31            BubbleDots.DemoClientGame.superclass.setupView.call(this);
 32        },
 33
 34        /**
 35         * @inheritDoc
 36         */
 37        tick: function () {
 38            BubbleDots.DemoClientGame.superclass.tick.call(this);
 39            this.view.stats.update();
 40            this.view.update(this.gameClockReal);
 41            this.view.textfield.setText("Ping: " + this.netChannel.getLatency());
 42        },
 43
 44        /**
 45         * @inheritDoc
 46         */
 47        createEntityFromDesc: function (entityDesc) {
 48
 49            // Create a new BubbleDots entity
 50            var newEntity = new BubbleDots.CircleEntity(entityDesc.entityid, entityDesc.clientid);
 51            newEntity.position.set(entityDesc.x, entityDesc.y);
 52
 53            var entityView = this.view.createEntityView(entityDesc);
 54            newEntity.setView(entityView);
 55
 56            this.fieldController.addEntity(newEntity);
 57
 58            // Our own character
 59            if (entityDesc.clientid == this.netChannel.getClientid() && entityDesc.entityType & BubbleDots.Constants.ENTITY_TYPES.PLAYER_ENTITY) {
 60                this.setupClientPlayer(newEntity);
 61                this.view.setFocusCharacter(entityView);
 62            }
 63        },
 64
 65        /**
 66         * Called when the player that represents this user is created
 67         * @param anEntity
 68         */
 69        setupClientPlayer: function (anEntity) {
 70            anEntity.addTraitAndExecute(new RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait());
 71            this.clientCharacter = anEntity;
 72        },
 73
 74        /**
 75         * @inheritDoc
 76         */
 77        netChannelDidConnect: function (messageData) {
 78            BubbleDots.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
 79            BubbleDots.DemoClientGame.prototype.log("DemoClientGame: Joining Game");
 80            this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
 81        },
 82
 83        /**
 84         * @inheritDoc
 85         */
 86        netChannelDidDisconnect: function (messageData) {
 87            BubbleDots.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
 88            BubbleDots.DemoClientGame.prototype.log("DemoClientGame: netChannelDidDisconnect"); // Display disconnect
 89        },
 90
 91        /**
 92         * An array containing values received from the entity
 93         * @param {String} singleWorldUpdate
 94         */
 95        parseEntityDescriptionArray: function (entityDescAsArray) {
 96            var entityDescription = {};
 97            // It is up to the user to make sure that their objects are following a certain order
 98            // We do this because we need the performance of sending the tiniest strings possible
 99            entityDescription.entityid = entityDescAsArray[0];
100            entityDescription.clientid = entityDescAsArray[1];
101            entityDescription.entityType = +entityDescAsArray[2];
102            entityDescription.x = +entityDescAsArray[3];
103            entityDescription.y = +entityDescAsArray[4];
104            entityDescription.scale = +entityDescAsArray[5];
105            entityDescription.color = entityDescAsArray[6];
106            return entityDescription;
107        },
108
109
110        /**
111         * This function logs something to the right panel
112         * @param {Object} An object in the form of: { message: ['Client', 'domReady'] }
113         */
114        log: (function () {
115            var message = function (message) {
116                var el = document.createElement('p');
117                el.innerHTML = '<b>' + esc(message) + ':</b> ';
118
119                // Log if possible
120                console.log(message);
121                document.getElementsByTagName('aside')[0].appendChild(el);
122                document.getElementsByTagName('aside')[0].scrollTop = 1000000;
123            };
124
125            var esc = function (msg) {
126                return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
127            };
128
129            return message;
130        })()
131    };
132
133    // extend RealtimeMultiplayerGame.AbstractClientGame
134    RealtimeMultiplayerGame.extend(BubbleDots.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
135})();