PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 1ms

/js/DemoBox2D/DemoBox2DEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 81 lines | 38 code | 9 blank | 34 comment | 2 complexity | 61555c48e8e7d798d4d8d4a6f025e576 MD5 | raw file
 1/**
 2 File:
 3 DemoBox2D.Box2DEntity
 4 Created By:
 5 Mario Gonzalez
 6 Project:
 7 DemoBox2D
 8 Abstract:
 9 This is the base entity for the demo game
10 Basic Usage:
11
12 Version:
13 1.0
14 */
15(function () {
16    var DAG2RAD = 0.0174532925;
17    var RAD2DEG = 57.2957795;
18
19    DemoBox2D.Box2DEntity = function (anEntityid, aClientid) {
20        DemoBox2D.Box2DEntity.superclass.constructor.call(this, anEntityid, aClientid);
21    };
22
23    DemoBox2D.Box2DEntity.prototype = {
24        b2Body: null,												// Reference to Box2D body
25        radius: 1,
26        entityType: DemoBox2D.Constants.ENTITY_TYPES.BOX,
27
28        /**
29         * @inheritDoc
30         */
31        updateView: function () {
32            if (!this.view) return;
33            this.view.x = this.position.x - this.radius;
34            this.view.y = this.position.y - this.radius;
35
36            this.view.setRotation(this.lastReceivedEntityDescription.rotation * DAG2RAD);
37        },
38
39        /**
40         * @inheritDoc
41         */
42        updatePosition: function (speedFactor, gameClock, gameTick) {
43            this.position.x = this.b2Body.m_xf.position.x * DemoBox2D.Constants.PHYSICS_SCALE;
44            this.position.y = this.b2Body.m_xf.position.y * DemoBox2D.Constants.PHYSICS_SCALE;
45            this.rotation = this.b2Body.GetAngle();
46        },
47
48        /**
49         * @inheritDoc
50         */
51        constructEntityDescription: function () {
52            // Send the regular entity description, but also send 'radius' and a rounded version 'rotation'
53            return DemoBox2D.Box2DEntity.superclass.constructEntityDescription.call(this) + ',' + this.radius + "," + ~~(this.rotation * RAD2DEG);
54        },
55
56        /**
57         * @inheritDoc
58         */
59        dealloc: function () {
60            if (this.b2Body) {
61                // Destroy box2d body -
62            }
63            DemoBox2D.Box2DEntity.superclass.dealloc.call(this);
64        },
65
66        ///// ACCESSORS
67        /**
68         * Set the Box2D body that represents this entity
69         * @param aBox2dBody
70         */
71        setBox2DBody: function (aBox2dBody) {
72            this.b2Body = aBox2dBody;
73        },
74        getBox2DBody: function () {
75            return this.b2Body
76        }
77    };
78
79    // extend RealtimeMultiplayerGame.model.GameEntity
80    RealtimeMultiplayerGame.extend(DemoBox2D.Box2DEntity, RealtimeMultiplayerGame.model.GameEntity, null);
81})();