PageRenderTime 21ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. (function () {
  13. BubbleDots.namespace("BubbleDots.traits");
  14. var NOISE_SEED = Math.random() * 1000;
  15. BubbleDots.traits.PerlinNoiseTrait = function () {
  16. BubbleDots.traits.PerlinNoiseTrait.superclass.constructor.call(this);
  17. };
  18. BubbleDots.traits.PerlinNoiseTrait.prototype = {
  19. displayName: "PerlinNoiseTraitTrait", // Unique string name for this Trait
  20. /**
  21. * @inheritDoc
  22. */
  23. attach: function (anEntity) {
  24. BubbleDots.traits.PerlinNoiseTrait.superclass.attach.call(this, anEntity);
  25. this.intercept(['updatePosition']);
  26. },
  27. /**
  28. * Intercepted properties
  29. */
  30. updatePosition: function (speedFactor, gameClock, gameTick) {
  31. // Call the original handleAcceleration
  32. var trait = this.getTraitWithName("PerlinNoiseTraitTrait");
  33. // Modify velocity using perlin noise
  34. var theta = 0.008;
  35. var noise = RealtimeMultiplayerGame.model.noise(this.position.x * theta, this.position.y * theta, NOISE_SEED + gameTick * 0.001);
  36. var angle = noise * Math.PI * 12.566370614359172; // PI * 4
  37. var speed = 0.05;
  38. this.acceleration.x += Math.cos(angle) * speed;//- 0.1;
  39. this.acceleration.y += Math.sin(angle) * speed;
  40. trait.interceptedProperties._data.updatePosition.call(this, speedFactor, gameClock, gameTick);
  41. }
  42. };
  43. // Extend BaseTrait
  44. RealtimeMultiplayerGame.extend(BubbleDots.traits.PerlinNoiseTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
  45. })();