PageRenderTime 148ms CodeModel.GetById 90ms app.highlight 13ms RepoModel.GetById 40ms app.codeStats 0ms

/js/BubbleDots/traits/GravityTrait.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 59 lines | 26 code | 8 blank | 25 comment | 0 complexity | 728a6d09050cb0e682971047786bd21f MD5 | raw file
 1/**
 2 File:
 3 GravityTrait.js
 4 Created By:
 5 Mario Gonzalez
 6 Project    :
 7 RealtimeMultiplayerNodeJS
 8 Abstract:
 9 This trait will cause an entity to chase a target
10 Basic Usage:
11
12 */
13(function () {
14    BubbleDots.namespace("BubbleDots.traits");
15    var RATE = 0.2;
16    BubbleDots.traits.GravityTrait = function () {
17        BubbleDots.traits.GravityTrait.superclass.constructor.call(this);
18        this._force = BubbleDots.traits.GravityTrait.prototype.DEFAULT_FORCE;
19    };
20
21    BubbleDots.traits.GravityTrait.prototype = {
22        displayName: "GravityTrait",					// Unique string name for this Trait
23
24        _force: 0,
25
26        DEFAULT_FORCE: 0.21,
27
28        /**
29         * @inheritDoc
30         */
31        attach: function (anEntity) {
32            BubbleDots.traits.GravityTrait.superclass.attach.call(this, anEntity);
33            this.intercept(['updatePosition']);
34        },
35
36        /**
37         * Intercepted properties
38         */
39        updatePosition: function (speedFactor, gameClock, gameTick) {
40            var trait = this.getTraitWithName("GravityTrait");
41
42            this.acceleration.y += trait._force * speedFactor;
43            // Call the original handleAcceleration
44            trait.interceptedProperties._data.updatePosition.call(this, speedFactor, gameClock, gameTick);
45        },
46
47        ///// ACCESSORS
48        /**
49         * Set the gravitational force
50         * @param {Number} force Strength of gravity in Y axis
51         */
52        setForce: function (force) {
53            this._force = force
54        }
55    };
56
57    // Extend BaseTrait
58    RealtimeMultiplayerGame.extend(BubbleDots.traits.GravityTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
59})();