PageRenderTime 173ms CodeModel.GetById 111ms app.highlight 8ms RepoModel.GetById 52ms app.codeStats 0ms

/js/BubbleDots/entities/CircleEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 163 lines | 95 code | 24 blank | 44 comment | 2 complexity | 33e5319ecdc4357107adaa37201cbfda MD5 | raw file
  1/**
  2 File:
  3 BubbleDots.CircleEntity
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 BubbleDots
  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    BubbleDots.CircleEntity = function (anEntityid, aClientid) {
 17        BubbleDots.CircleEntity.superclass.constructor.call(this, anEntityid, aClientid);
 18
 19        this.entityType = BubbleDots.Constants.ENTITY_TYPES.CANDY_ENTITY;
 20        this.originalColor = null;
 21        this.velocity = new RealtimeMultiplayerGame.model.Point(0, 0);
 22        this.acceleration = new RealtimeMultiplayerGame.model.Point(0, 0);
 23        return this;
 24    };
 25
 26    BubbleDots.CircleEntity.prototype = {
 27        radius: BubbleDots.Constants.ENTITY_DEFAULT_RADIUS,
 28        velocity: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
 29        acceleration: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
 30        collisionCircle: null,										// An instance of RealtimeMultiplayerGame.modules.circlecollision.PackedCircle
 31        entityType: null,
 32        color: "2",
 33        originalColor: "2",
 34        _tween: null,
 35
 36        // Movement properties
 37        velocityMax: 8.0,
 38        velocityDamping: 0.98,
 39
 40
 41        /**
 42         * Update the entity's view - this is only called on the clientside
 43         */
 44        updateView: function () {
 45            if (!this.view) return;
 46
 47            this.view.x = this.position.x;// - this.radius;
 48            this.view.y = this.position.y;// - this.radius;
 49            this.view.setScale(this.lastReceivedEntityDescription.scale * 0.01, this.lastReceivedEntityDescription.scale * 0.01);
 50            return;
 51
 52            var diameter = this.lastReceivedEntityDescription.radius * 2;
 53            this.view.setSize(diameter, diameter);
 54            this.view.setFillStyle("#" + this.lastReceivedEntityDescription.color); // Random color
 55        },
 56
 57        /**
 58         * Update position of this entity - this is only called on the serverside
 59         * @param {Number} speedFactor    A number signifying how much faster or slower we are moving than the target framerate
 60         * @param {Number} gameClock    Current game time in seconds (zero based)
 61         * @param {Number} gameTick        Current game tick (incrimented each frame)
 62         */
 63        updatePosition: function (speedFactor, gameClock, gameTick) {
 64            this.handleAcceleration(speedFactor, gameClock, gameTick);
 65        },
 66
 67        handleAcceleration: function (speedFactor, gameClock, gameTick) {
 68            this.velocity.translatePoint(this.acceleration);
 69//			this.velocity.limit(this.velocityMax);
 70            this.velocity.multiply(this.velocityDamping);
 71
 72            this.collisionCircle.position.translatePoint(this.velocity);
 73            this.position = this.collisionCircle.position.clone();
 74
 75            this.acceleration.set(0, 0);
 76        },
 77
 78        /**
 79         * Called when this object has collided with another
 80         * @param a        Object A in the collision pair, note this may be this object
 81         * @param b        Object B in the collision pair, note this may be this object
 82         * @param collisionNormal    A vector describing the collision
 83         */
 84        onCollision: function (a, b, collisionNormal) {
 85        },
 86
 87        tempColor: function () {
 88            var that = this;
 89
 90            clearTimeout(this.timeout);
 91            this.color = "1";
 92            this.timeout = setTimeout(function () {
 93                that.setColor(that.originalColor);
 94            }, 50);
 95        },
 96
 97        /**
 98         * Deallocate memory
 99         */
100        dealloc: function () {
101            this.collisionCircle.dealloc();
102            this.collisionCircle = null;
103
104            BubbleDots.CircleEntity.superclass.dealloc.call(this);
105        },
106
107        constructEntityDescription: function () {
108            var entityDesc = BubbleDots.CircleEntity.superclass.constructEntityDescription.call(this);
109            entityDesc += ',' + ~~(this.scale * 100);
110            entityDesc += ',' + this.color;
111
112            return entityDesc;
113        },
114
115        ///// ACCESSORS
116        /**
117         * Set the CollisionCircle for this game entity.
118         * @param aCollisionCircle
119         */
120        setCollisionCircle: function (aCollisionCircle) {
121            this.collisionCircle = aCollisionCircle;
122            this.collisionCircle.collisionMask = 1;
123            this.collisionCircle.collisionGroup = 2;
124            this.collisionCircle.setDelegate(this);
125            this.collisionCircle.setPosition(this.position.clone());
126            this.collisionCircle.setRadius(this.radius);
127
128        },
129        getCollisionCircle: function () {
130            return this.collisionCircle
131        },
132
133        /**
134         * Set the color of this entity, a property originalColor is also stored
135         * @param aColor
136         */
137        setColor: function (aColor) {
138            if (!this.originalColor) {
139                this.originalColor = aColor;
140            }
141
142            this.color = aColor;
143        },
144        getColor: function () {
145            return this.color
146        },
147        getOriginalColor: function () {
148            return this.originalColor
149        },
150        setRadius: function (aRadius) {
151            this.radius = aRadius;
152            this.collisionCircle.setRadius(this.radius);
153            this.scale = this.radius / BubbleDots.Constants.ENTITY_DEFAULT_RADIUS;
154        },
155        getRadius: function () {
156            return this.radius;
157        }
158
159    };
160
161    // extend RealtimeMultiplayerGame.model.GameEntity
162    RealtimeMultiplayerGame.extend(BubbleDots.CircleEntity, RealtimeMultiplayerGame.model.GameEntity, null);
163})();