PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/js/DemoBox2D/DemoBox2DView.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 145 lines | 68 code | 17 blank | 60 comment | 3 complexity | 5c05df2f7b588b4139b19dc89f98d376 MD5 | raw file
  1/**
  2 File:
  3 DemoBox2DView.js
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 RealtimeMultiplayerNodeJS
  8 Abstract:
  9 This class represents the view object in the Box2D Demo of RealtimeMultiplayerNodeJS
 10 It uses the CAAT canvas library to draw the entities
 11 Basic Usage:
 12 [Inside DemoBox2DClientGame]
 13 setupView: function() {
 14		this.view = new DemoBox2D.DemoView();
 15		this.view.insertIntoHTMLElementWithId( "gamecontainer" );
 16		DemoBox2D.DemoClientGame.superclass.setupView.call( this );
 17	}
 18 Version:
 19 1.0
 20 */
 21(function () {
 22    DemoBox2D.DemoView = function (aDelegate) {
 23        this.setDelegate(aDelegate);
 24        this.setupCAAT();
 25        this.setupMouseEvents();
 26        this.setupStats();
 27    };
 28
 29    DemoBox2D.DemoView.prototype = {
 30        // Properties
 31        caatDirector: null,				// CAAT Director instance
 32        caatScene: null,				// CAAT Scene instance
 33        stats: null,				// Stats.js instance
 34        delegate: null,				// This is the object that will handle some MouseEvents we recieve from CAAT
 35        // Methods
 36        setupCAAT: function () {
 37
 38            // Create a scene, all directors must have at least one scene - this is where all your stuff goes
 39            this.caatScene = new CAAT.Scene();
 40            this.caatScene.create();
 41            this.caatScene.setFillStyle('#000000');
 42
 43            // Create the director instance, and immediately add the scene once it's created
 44            this.caatDirector = new CAAT.Director().initialize(DemoBox2D.Constants.GAME_WIDTH, DemoBox2D.Constants.GAME_HEIGHT);
 45            this.caatDirector.addScene(this.caatScene); //
 46        },
 47
 48        /**
 49         * Setup MouseEvent which we will funnel to the delegate
 50         * @param delegate
 51         */
 52        setupMouseEvents: function (delegate) {
 53            var that = this;
 54            this.caatScene.mouseDown = function (mouseEvent) {
 55                that.delegate.onViewMouseDown(mouseEvent);
 56            };
 57        },
 58
 59        /**
 60         * Updates our current view, passing along the current actual time (via Date().getTime());
 61         * @param {Number} gameClockReal The current actual time, according to the game
 62         */
 63        update: function (gameClockReal) {
 64            var delta = gameClockReal - this.caatDirector.timeline;
 65            this.caatDirector.render(delta);
 66            this.caatDirector.timeline = gameClockReal;
 67        },
 68
 69        /**
 70         * Creates a Stats.js instance and adds it to the page
 71         */
 72        setupStats: function () {
 73            var container = document.createElement('div');
 74            this.stats = new Stats();
 75            this.stats.domElement.style.position = 'absolute';
 76            this.stats.domElement.style.top = '0px';
 77            container.appendChild(this.stats.domElement);
 78            document.body.appendChild(container);
 79        },
 80
 81        /**
 82         * Creats / Adds an entity into canvas via CAAT
 83         * // TODO: Currently the entity is already created - however technically this is the function that should make it!
 84         * @param anEntityView
 85         */
 86        addEntity: function (anEntityView) {
 87            this.caatScene.addChild(anEntityView);
 88            anEntityView.mouseEnabled = false;
 89        },
 90
 91        /**
 92         * Removes an entity via CAAT
 93         * @param anEntityView
 94         */
 95        removeEntity: function (anEntityView) {
 96            this.caatScene.removeChild(anEntityView);
 97        },
 98
 99        /**
100         * Insert the CAATDirector canvas into an HTMLElement
101         * @param {String} id An HTMLElement id
102         */
103        insertIntoHTMLElementWithId: function (id) {
104            document.getElementById(id).appendChild(this.caatDirector.canvas);
105        },
106
107        // Memory
108        dealloc: function () {
109            this.director.destroy();
110        },
111
112        // Accessors
113        /**
114         * Checks that an object contains the required methods and sets it as the delegate for this DemoBox2D.DemoView instance
115         * @param {DemoBox2D.DemoViewDelegateProtocol} aDelegate A delegate that conforms to DemoBox2D.DemoViewDelegateProtocol
116         */
117        setDelegate: function (aDelegate) {
118            var theInterface = DemoBox2D.DemoViewDelegateProtocol;
119            for (var member in theInterface) {
120                if ((typeof aDelegate[member] != typeof theInterface[member])) {
121                    console.log("object failed to implement interface member " + member);
122                    return false;
123                }
124            }
125
126            // Checks passed
127            this.delegate = aDelegate;
128        }
129    };
130
131    // Override
132    RealtimeMultiplayerGame.extend(DemoBox2D.DemoView, RealtimeMultiplayerGame.AbstractGameView, null);
133
134
135    /**
136     * This is the object that will handle some MouseEvents we recieve from CAAT
137     * This is strictly specific to our Box2D demo, and is pretty bare but seems symantically correct following the structure RealtimeMultiplayerNodeJS has in place
138     */
139    DemoBox2D.DemoViewDelegateProtocol = {
140        onViewMouseDown: function (mouseEvent) {
141        }
142    };
143})();
144
145