PageRenderTime 19ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. (function () {
  13. BubbleDots.namespace("BubbleDots.traits");
  14. var RATE = 0.2;
  15. BubbleDots.traits.ChaseTrait = function () {
  16. BubbleDots.traits.ChaseTrait.superclass.constructor.call(this);
  17. };
  18. BubbleDots.traits.ChaseTrait.prototype = {
  19. displayName: "ChaseTrait", // Unique string name for this Trait
  20. chaseSpeed: 0.01,
  21. radius: 40,
  22. target: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
  23. offset: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
  24. /**
  25. * @inheritDoc
  26. */
  27. attach: function (anEntity) {
  28. BubbleDots.traits.ChaseTrait.superclass.attach.call(this, anEntity);
  29. this.intercept(['updatePosition']);
  30. },
  31. /**
  32. * @inheritDoc
  33. */
  34. execute: function () {
  35. RATE += 0.3;
  36. this.radius = Math.random() * 20 + 10;
  37. this.offset = new RealtimeMultiplayerGame.model.Point(Math.cos(RATE) * this.radius, Math.sin(RATE) * -this.radius);
  38. this.chaseSpeed = Math.random() * 0.02 + 0.001;
  39. BubbleDots.traits.ChaseTrait.superclass.execute.call(this);
  40. },
  41. /**
  42. * Intercepted properties
  43. */
  44. updatePosition: function (speedFactor, gameClock, gameTick) {
  45. var trait = this.getTraitWithName("ChaseTrait");
  46. // Move towards the target position overtime
  47. var delta = trait.target.position.subtractClone(this.position);
  48. delta.x += trait.offset.x;
  49. delta.y += trait.offset.y;
  50. delta.multiply(trait.chaseSpeed);
  51. this.acceleration.translatePoint(delta);
  52. // Call the original handleAcceleration
  53. trait.interceptedProperties._data.updatePosition.call(this, speedFactor, gameClock, gameTick);
  54. },
  55. ///// ACCESSORS
  56. /**
  57. * Set the target this object will follow
  58. * @param {RealtimeMultiplayerGame.model.GameEntity} aTarget
  59. */
  60. setTarget: function (aTarget) {
  61. this.target = aTarget;
  62. }
  63. };
  64. // Extend BaseTrait
  65. RealtimeMultiplayerGame.extend(BubbleDots.traits.ChaseTrait, RealtimeMultiplayerGame.controller.traits.BaseTrait);
  66. })();