PageRenderTime 172ms CodeModel.GetById 70ms app.highlight 15ms RepoModel.GetById 66ms app.codeStats 1ms

/js/BubbleDots/traits/PerlinNoiseTrait.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 54 lines | 25 code | 8 blank | 21 comment | 0 complexity | 8587a0d75b18249b893df401d588fcfd MD5 | raw file
 1/**
 2 File:
 3 PerlinNoiseTrait.js
 4 Created By:
 5 Mario Gonzalez
 6 Project    :
 7 RealtimeMultiplayerNodeJS
 8 Abstract:
 9 Applies perlin noise to an objects velocity
10 Basic Usage:
11
12 */
13(function () {
14    BubbleDots.namespace("BubbleDots.traits");
15
16    var NOISE_SEED = Math.random() * 1000;
17
18    BubbleDots.traits.PerlinNoiseTrait = function () {
19        BubbleDots.traits.PerlinNoiseTrait.superclass.constructor.call(this);
20    };
21
22    BubbleDots.traits.PerlinNoiseTrait.prototype = {
23        displayName: "PerlinNoiseTraitTrait",					// Unique string name for this Trait
24
25        /**
26         * @inheritDoc
27         */
28        attach: function (anEntity) {
29            BubbleDots.traits.PerlinNoiseTrait.superclass.attach.call(this, anEntity);
30            this.intercept(['updatePosition']);
31        },
32
33        /**
34         * Intercepted properties
35         */
36        updatePosition: function (speedFactor, gameClock, gameTick) {
37            // Call the original handleAcceleration
38            var trait = this.getTraitWithName("PerlinNoiseTraitTrait");
39
40            // Modify velocity using perlin noise
41            var theta = 0.008;
42            var noise = RealtimeMultiplayerGame.model.noise(this.position.x * theta, this.position.y * theta, NOISE_SEED + gameTick * 0.001);
43            var angle = noise * Math.PI * 12.566370614359172; // PI * 4
44            var speed = 0.05;
45            this.acceleration.x += Math.cos(angle) * speed;//- 0.1;
46            this.acceleration.y += Math.sin(angle) * speed;
47
48            trait.interceptedProperties._data.updatePosition.call(this, speedFactor, gameClock, gameTick);
49        }
50    };
51
52    // Extend BaseTrait
53    RealtimeMultiplayerGame.extend(BubbleDots.traits.PerlinNoiseTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
54})();