PageRenderTime 75ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 23ms app.codeStats 0ms

/js/DemoHelloWorld/DemoClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 152 lines | 72 code | 21 blank | 59 comment | 0 complexity | 567b5203fa213f57dd47aecc9e8152f9 MD5 | raw file
  1/**
  2 File:
  3 DemoServerGame
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 DemoHelloWorld
  8 Abstract:
  9 This is a concrete server instance of our game
 10 Basic Usage:
 11 DemoServerGame = new DemoHelloWorld.DemoServerGame();
 12 DemoServerGame.start();
 13 DemoServerGame.explodeEveryone();
 14 Version:
 15 1.0
 16 */
 17(function () {
 18
 19    DemoHelloWorld.DemoClientGame = function () {
 20        DemoHelloWorld.DemoClientGame.superclass.constructor.call(this);
 21        this.startGameClock();
 22        return this;
 23    };
 24
 25    DemoHelloWorld.DemoClientGame.prototype = {
 26        setupView: function () {
 27            this.view = new DemoHelloWorld.DemoView();
 28            this.view.insertIntoHTMLElementWithId("gamecontainer");
 29            DemoHelloWorld.DemoClientGame.superclass.setupView.call(this);
 30        },
 31
 32        /**
 33         * @inheritDoc
 34         */
 35        tick: function () {
 36            DemoHelloWorld.DemoClientGame.superclass.tick.call(this);
 37            this.view.stats.update();
 38            this.view.update(this.gameClockReal);
 39        },
 40
 41        /**
 42         * @inheritDoc
 43         */
 44        createEntityFromDesc: function (entityDesc) {
 45
 46            var diameter = entityDesc.radius * 2;
 47
 48            // Create a view via CAAT
 49            var aCircleView = new CAAT.ShapeActor();
 50            aCircleView.create();
 51            aCircleView.setSize(diameter, diameter);
 52            aCircleView.setFillStyle("#" + CAAT.Color.prototype.hsvToRgb((entityDesc.entityid * 15) % 360, 40, 99).toHex()); // Random color
 53            aCircleView.setLocation(entityDesc.x, entityDesc.y); // Place in the center of the screen, use the director's width/height
 54
 55            // Create the entity and add it to the fieldcontroller
 56            var newEntity = new DemoHelloWorld.CircleEntity(entityDesc.entityid, entityDesc.clientid);
 57            newEntity.position.set(entityDesc.x, entityDesc.y);
 58            newEntity.setView(aCircleView);
 59
 60            this.fieldController.addEntity(newEntity);
 61        },
 62
 63        /**
 64         * Called by the ClientNetChannel, it sends us an array containing tightly packed values and expects us to return a meaningful object
 65         * It is left up to each game to implement this function because only the game knows what it needs to send.
 66         * However the 4 example projects in RealtimeMultiplayerNodeJS offer should be used ans examples
 67         *
 68         * @param {Array} entityDescAsArray An array of tightly packed values
 69         * @return {Object} An object which will be returned to you later on tied to a specific entity
 70         */
 71        parseEntityDescriptionArray: function (entityDescAsArray) {
 72            var entityDescription = {};
 73
 74            // It is left upto each game to implement this function because only the game knows what it needs to send.
 75            // However the 4 example projects in RealtimeMultiplayerNodeJS offer this an example
 76            entityDescription.entityid = +entityDescAsArray[0];
 77            entityDescription.clientid = +entityDescAsArray[1];
 78            entityDescription.entityType = +entityDescAsArray[2];
 79            entityDescription.x = +entityDescAsArray[3];
 80            entityDescription.y = +entityDescAsArray[4];
 81            entityDescription.radius = +entityDescAsArray[5];
 82
 83            return entityDescription;
 84        },
 85
 86        /**
 87         * @inheritDoc
 88         */
 89        netChannelDidConnect: function (messageData) {
 90            DemoHelloWorld.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
 91            this.joinGame("Player" + this.netChannel.getClientid()); // Automatically join the game with some name
 92        },
 93
 94        /**
 95         * @inheritDoc
 96         */
 97        netChannelDidReceiveMessage: function (messageData) {
 98            DemoHelloWorld.DemoClientGame.superclass.netChannelDidReceiveMessage.call(this, messageData);
 99            // Do some stuff here
100        },
101
102        /**
103         * @inheritDoc
104         */
105        netChannelDidDisconnect: function (messageData) {
106            DemoHelloWorld.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
107            // Do some stuff here
108        },
109
110        /**
111         * @inheritDoc
112         */
113        joinGame: function (aNickname) {
114            // This is called when the user has decided to join the game, in our HelloWorld example this happens automatically
115            // In a regular game situation, this might happen after a user picked their chracter for example
116            this.nickname = aNickname;
117            // Create a 'join' message and queue it in ClientNetChannel
118            this.netChannel.addMessageToQueue(true, RealtimeMultiplayerGame.Constants.CMDS.PLAYER_JOINED, { nickname: this.nickname });
119        },
120
121        /**
122         * @inheritDoc
123         */
124        log: (function () {
125            var message = function (message) {
126                var el = document.createElement('p');
127                el.innerHTML = '<b>' + esc(message) + ':</b> ';
128
129                // Log if possible
130                console.log(message);
131                document.getElementsByTagName('aside')[0].appendChild(el);
132                document.getElementsByTagName('aside')[0].scrollTop = 1000000;
133            };
134
135            var esc = function (msg) {
136                return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
137            };
138
139            return message;
140        })(),
141
142        /**
143         * Deallocate memory from your game, and let the superclass do the same
144         */
145        dealloc: function () {
146            DemoHelloWorld.DemoClientGame.superclass.dealloc.call(this);
147        }
148    };
149
150    // extend RealtimeMultiplayerGame.AbstractClientGame
151    RealtimeMultiplayerGame.extend(DemoHelloWorld.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
152})()