PageRenderTime 43ms CodeModel.GetById 15ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/js/DemoCircles/DemoClientGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 144 lines | 75 code | 22 blank | 47 comment | 4 complexity | 973ea68d9227067151a2022af3ecfb1f MD5 | raw file
  1/**
  2 File:
  3 DemoServerGame
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 DemoApp
  8 Abstract:
  9 This is a concrete server instance of our game
 10 Basic Usage:
 11 DemoServerGame = new DemoApp.DemoServerGame();
 12 DemoServerGame.start();
 13 DemoServerGame.explodeEveryone();
 14 Version:
 15 1.0
 16 */
 17(function () {
 18
 19    DemoApp.DemoClientGame = function () {
 20        DemoApp.DemoClientGame.superclass.constructor.call(this);
 21
 22        this.startGameClock();
 23        return this;
 24    };
 25
 26    DemoApp.DemoClientGame.prototype = {
 27        setupView: function () {
 28            this.view = new DemoApp.DemoView();
 29            this.view.insertIntoHTMLElementWithId("gamecontainer");
 30
 31            DemoApp.DemoClientGame.superclass.setupView.call(this);
 32        },
 33
 34        /**
 35         * @inheritDoc
 36         */
 37        tick: function () {
 38            DemoApp.DemoClientGame.superclass.tick.call(this);
 39            this.view.stats.update();
 40            this.view.update(this.gameClockReal);
 41        },
 42
 43        /**
 44         * @inheritDoc
 45         */
 46        createEntityFromDesc: function (entityDesc) {
 47
 48            var diameter = entityDesc.radius * 2;
 49
 50            // Create a view via CAAT
 51            var aCircleView = new CAAT.ShapeActor();
 52            aCircleView.create();
 53            aCircleView.setSize(diameter, diameter);
 54            aCircleView.setFillStyle("#" + CAAT.Color.prototype.hsvToRgb((entityDesc.entityid * 15) % 360, 40, 99).toHex()); // Random color
 55            aCircleView.setLocation(entityDesc.x, entityDesc.y); // Place in the center of the screen, use the director's width/height
 56
 57            var newEntity = null;
 58
 59            var isOwnedByMe = entityDesc.clientid == this.netChannel.clientid;
 60            // If this is a player entity
 61            if (entityDesc.entityType & DemoApp.Constants.ENTITY_TYPES.PLAYER_ENTITY) {
 62                newEntity = new DemoApp.PlayerEntity(entityDesc.entityid, entityDesc.clientid);
 63
 64                // If it is a player entity and it's my player entity - attach a KeyboardInputTrait to it
 65                if (isOwnedByMe) {
 66                    newEntity.addTraitAndExecute(new RealtimeMultiplayerGame.controller.traits.KeyboardInputTrait());
 67                    this.clientCharacter = newEntity;
 68                }
 69            } else {
 70                newEntity = new DemoApp.CircleEntity(entityDesc.entityid, entityDesc.clientid);
 71            }
 72
 73            newEntity.position.set(entityDesc.x, entityDesc.y);
 74            newEntity.setView(aCircleView);
 75
 76            this.fieldController.addEntity(newEntity);
 77        },
 78
 79        /**
 80         * Called by the ClientNetChannel, it sends us an array containing tightly packed values and expects us to return a meaningful object
 81         * It is left up to each game to implement this function because only the game knows what it needs to send.
 82         * However the 4 example projects in RealtimeMultiplayerNodeJS offer should be used ans examples
 83         *
 84         * @param {Array} entityDescAsArray An array of tightly packed values
 85         * @return {Object} An object which will be returned to you later on tied to a specific entity
 86         */
 87        parseEntityDescriptionArray: function (entityDescAsArray) {
 88            var entityDescription = {};
 89
 90            // It is left upto each game to implement this function because only the game knows what it needs to send.
 91            // However the 4 example projects in RealtimeMultiplayerNodeJS offer this an example
 92            entityDescription.entityid = entityDescAsArray[0];
 93            entityDescription.clientid = entityDescAsArray[1];
 94            entityDescription.entityType = +entityDescAsArray[2];
 95            entityDescription.x = +entityDescAsArray[3];
 96            entityDescription.y = +entityDescAsArray[4];
 97            entityDescription.radius = +entityDescAsArray[5];
 98            entityDescription.color = entityDescAsArray[6];
 99            return entityDescription;
100        },
101
102        /**
103         * @inheritDoc
104         */
105        netChannelDidConnect: function (messageData) {
106            DemoApp.DemoClientGame.superclass.netChannelDidConnect.call(this, messageData);
107            DemoApp.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            DemoApp.DemoClientGame.superclass.netChannelDidDisconnect.call(this, messageData);
116            DemoApp.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(DemoApp.DemoClientGame, RealtimeMultiplayerGame.AbstractClientGame, null);
144})()