PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/js/BubbleDots/traits/ChaseTrait.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 76 lines | 38 code | 9 blank | 29 comment | 0 complexity | cf836b5d58b6c6d44154c301090acf7f MD5 | raw file
 1/**
 2 File:
 3 ChaseTrait.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.ChaseTrait = function () {
17        BubbleDots.traits.ChaseTrait.superclass.constructor.call(this);
18    };
19
20    BubbleDots.traits.ChaseTrait.prototype = {
21        displayName: "ChaseTrait",					// Unique string name for this Trait
22        chaseSpeed: 0.01,
23        radius: 40,
24        target: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
25        offset: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
26
27        /**
28         * @inheritDoc
29         */
30        attach: function (anEntity) {
31            BubbleDots.traits.ChaseTrait.superclass.attach.call(this, anEntity);
32            this.intercept(['updatePosition']);
33        },
34
35        /**
36         * @inheritDoc
37         */
38        execute: function () {
39            RATE += 0.3;
40            this.radius = Math.random() * 20 + 10;
41            this.offset = new RealtimeMultiplayerGame.model.Point(Math.cos(RATE) * this.radius, Math.sin(RATE) * -this.radius);
42            this.chaseSpeed = Math.random() * 0.02 + 0.001;
43            BubbleDots.traits.ChaseTrait.superclass.execute.call(this);
44        },
45
46        /**
47         * Intercepted properties
48         */
49        updatePosition: function (speedFactor, gameClock, gameTick) {
50            var trait = this.getTraitWithName("ChaseTrait");
51
52            // Move towards the target position overtime
53            var delta = trait.target.position.subtractClone(this.position);
54            delta.x += trait.offset.x;
55            delta.y += trait.offset.y;
56
57            delta.multiply(trait.chaseSpeed);
58            this.acceleration.translatePoint(delta);
59
60            // Call the original handleAcceleration
61            trait.interceptedProperties._data.updatePosition.call(this, speedFactor, gameClock, gameTick);
62        },
63
64        ///// ACCESSORS
65        /**
66         * Set the target this object will follow
67         * @param {RealtimeMultiplayerGame.model.GameEntity} aTarget
68         */
69        setTarget: function (aTarget) {
70            this.target = aTarget;
71        }
72    };
73
74    // Extend BaseTrait
75    RealtimeMultiplayerGame.extend(BubbleDots.traits.ChaseTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
76})();