PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/circlecollision/Circle.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 155 lines | 108 code | 22 blank | 25 comment | 3 complexity | d24783faa00235479bef36e03bf72f7b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. #### ##### ##### #### ### # # ###### ###### ## ## ##### # # ######## ## # # #####
  3. # # # # ### # # ##### ### ## ## ## # ## # # # # ## # ##### ### ###
  4. ### # # ##### #### # # # ###### ## ######### ##### ##### ##### # ## # # # # # #####
  5. -
  6. File:
  7. PackedCircle.js
  8. Created By:
  9. Mario Gonzalez
  10. Project :
  11. None
  12. Abstract:
  13. A single packed circle.
  14. Contains a reference to it's div, and information pertaining to it state.
  15. Basic Usage:
  16. http://onedayitwillmake.com/CirclePackJS/
  17. */
  18. (function() {
  19. // Retrieve the namespace
  20. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.modules.circlecollision");
  21. /**
  22. * @constructor
  23. */
  24. RealtimeMultiplayerGame.modules.circlecollision.PackedCircle = function()
  25. {
  26. this.boundsRule = RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.BOUNDS_RULE_IGNORE;
  27. this.position = new RealtimeMultiplayerGame.model.Point(0,0);
  28. this.offset = new RealtimeMultiplayerGame.model.Point(0,0);
  29. this.targetPosition = new RealtimeMultiplayerGame.model.Point(0,0);
  30. return this;
  31. };
  32. RealtimeMultiplayerGame.modules.circlecollision.PackedCircle.prototype = {
  33. id: 0,
  34. delegate: null,
  35. position: new RealtimeMultiplayerGame.model.Point(0,0),
  36. offset: new RealtimeMultiplayerGame.model.Point(0,0), // Offset from delegates position by this much
  37. radius: 0,
  38. radiusSquared: 0,
  39. targetPosition: null, // Where it wants to go
  40. targetChaseSpeed: 0.02,
  41. isFixed: false,
  42. boundsRule: 0,
  43. collisionMask: 0,
  44. collisionGroup: 0,
  45. BOUNDS_RULE_WRAP: 1, // Wrap to otherside
  46. BOUNDS_RULE_CONSTRAINT: 2, // Constrain within bounds
  47. BOUNDS_RULE_DESTROY: 4, // Destroy when it reaches the edge
  48. BOUNDS_RULE_IGNORE: 8, // Ignore when reaching bounds
  49. containsPoint: function(aPoint)
  50. {
  51. var distanceSquared = this.position.getDistanceSquared(aPoint);
  52. return distanceSquared < this.radiusSquared;
  53. },
  54. getDistanceSquaredFromPosition: function(aPosition)
  55. {
  56. var distanceSquared = this.position.getDistanceSquared(aPosition);
  57. // if it's shorter than either radius, we intersect
  58. return distanceSquared < this.radiusSquared;
  59. },
  60. intersects: function(aCircle)
  61. {
  62. var distanceSquared = this.position.getDistanceSquared(aCircle.position);
  63. return (distanceSquared < this.radiusSquared || distanceSquared < aCircle.radiusSquared);
  64. },
  65. /**
  66. * ACCESSORS
  67. */
  68. setPosition: function(aPosition)
  69. {
  70. this.position = aPosition;
  71. return this;
  72. },
  73. setDelegate: function(aDelegate)
  74. {
  75. this.delegate = aDelegate;
  76. return this;
  77. },
  78. setOffset: function(aPosition)
  79. {
  80. this.offset = aPosition;
  81. return this;
  82. },
  83. setTargetPosition: function(aTargetPosition)
  84. {
  85. this.targetPosition = aTargetPosition;
  86. return this;
  87. },
  88. setTargetChaseSpeed: function(aTargetChaseSpeed)
  89. {
  90. this.targetChaseSpeed = aTargetChaseSpeed;
  91. return this;
  92. },
  93. setIsFixed: function(value)
  94. {
  95. this.isFixed = value;
  96. return this;
  97. },
  98. setCollisionMask: function(aCollisionMask)
  99. {
  100. this.collisionMask = aCollisionMask;
  101. return this;
  102. },
  103. setCollisionGroup: function(aCollisionGroup)
  104. {
  105. this.collisionGroup = aCollisionGroup;
  106. return this;
  107. },
  108. setRadius: function(aRadius)
  109. {
  110. this.radius = aRadius;
  111. this.radiusSquared = this.radius*this.radius;
  112. return this;
  113. },
  114. initialize : function(overrides)
  115. {
  116. if (overrides)
  117. {
  118. for (var i in overrides)
  119. {
  120. this[i] = overrides[i];
  121. }
  122. }
  123. return this;
  124. },
  125. dealloc: function()
  126. {
  127. this.position = null;
  128. this.offset = null;
  129. this.delegate = null;
  130. this.targetPosition = null;
  131. }
  132. };
  133. })();