PageRenderTime 87ms CodeModel.GetById 32ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 0ms

/js/DemoHelloWorld/DemoView.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 103 lines | 45 code | 11 blank | 47 comment | 0 complexity | ec7146d08c9ec56bda1c4e1949221163 MD5 | raw file
  1/**
  2 File:
  3 AbstractServerGame.js
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 RealtimeMultiplayerNodeJS
  8 Abstract:
  9 This class is the base Game controller in RealtimeMultiplayerGame on the server side.
 10 It provides things such as dropping players, and contains a ServerNetChannel
 11 Basic Usage:
 12 [This class is not instantiated! - below is an example of using this class by extending it]
 13
 14 (function(){
 15		MyGameClass = function() {
 16			return this;
 17 		}
 18
 19		RealtimeMultiplayerGame.extend(MyGameClass, RealtimeMultiplayerGame.AbstractServerGame, null);
 20	};
 21 Version:
 22 1.0
 23 */
 24(function () {
 25    DemoHelloWorld.DemoView = function () {
 26        this.setupCAAT();
 27        this.setupStats();
 28    };
 29
 30    DemoHelloWorld.DemoView.prototype = {
 31        // Properties
 32        caatDirector: null,				// CAAT Director instance
 33        caatScene: null,				// CAAT Scene instance
 34        stats: null,				// Stats.js instance
 35
 36        // Methods
 37        setupCAAT: function () {
 38            this.caatScene = new CAAT.Scene(); // Create a scene, all directors must have at least one scene - this is where all your stuff goes
 39            this.caatScene.create();	// Notice we call create when creating this, and ShapeActor below. Both are Actors
 40            this.caatScene.setFillStyle('#000000');
 41
 42            this.caatDirector = new CAAT.Director().initialize(DemoHelloWorld.Constants.GAME_WIDTH, DemoHelloWorld.Constants.GAME_HEIGHT); // Create the director instance
 43            this.caatDirector.addScene(this.caatScene); // Immediately add the scene once it's created
 44        },
 45
 46        /**
 47         * Updates our current view, passing along the current actual time (via Date().getTime());
 48         * @param {Number} gameClockReal The current actual time, according to the game
 49         */
 50        update: function (gameClockReal) {
 51            var delta = gameClockReal - this.caatDirector.timeline;
 52            this.caatDirector.render(delta);
 53            this.caatDirector.timeline = gameClockReal;
 54        },
 55
 56        /**
 57         * Creates a Stats.js instance and adds it to the page
 58         */
 59        setupStats: function () {
 60            var container = document.createElement('div');
 61            this.stats = new Stats();
 62            this.stats.domElement.style.position = 'absolute';
 63            this.stats.domElement.style.top = '0px';
 64            container.appendChild(this.stats.domElement);
 65            document.body.appendChild(container);
 66        },
 67
 68        /**
 69         * Add an entity from the view - For now this method definitely has some symantic issues because it's being passed a concrete view instead of figuring out what the view is
 70         * @param anEntityView
 71         */
 72        addEntity: function (anEntityView) {
 73            console.log("Adding Entity To CAAT", anEntityView);
 74            this.caatScene.addChild(anEntityView);
 75        },
 76
 77        /**
 78         * Remove an entity from the view - For now this method definitely has some symantic issues because it's being passed a concrete view instead of figuring out what the view is
 79         * @param anEntityView
 80         */
 81        removeEntity: function (anEntityView) {
 82            console.log("Removing Entity From CAAT", anEntityView);
 83            this.caatScene.removeChild(anEntityView);
 84        },
 85
 86        /**
 87         * Insert the CAATDirector canvas into an HTMLElement
 88         * @param {String} id An HTMLElement id
 89         */
 90        insertIntoHTMLElementWithId: function (id) {
 91            document.getElementById(id).appendChild(this.caatDirector.canvas);
 92        },
 93
 94        /**
 95         * @inheritDoc
 96         */
 97        dealloc: function () {
 98            this.director.destroy();
 99        }
100    };
101})();
102
103