PageRenderTime 145ms CodeModel.GetById 29ms RepoModel.GetById 2ms app.codeStats 0ms

/js/BubbleDots/entities/CircleEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 163 lines | 95 code | 24 blank | 44 comment | 2 complexity | 33e5319ecdc4357107adaa37201cbfda 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. BubbleDots.CircleEntity
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. BubbleDots
  8. Abstract:
  9. This is the base entity for the demo game
  10. Basic Usage:
  11. Version:
  12. 1.0
  13. */
  14. (function () {
  15. BubbleDots.CircleEntity = function (anEntityid, aClientid) {
  16. BubbleDots.CircleEntity.superclass.constructor.call(this, anEntityid, aClientid);
  17. this.entityType = BubbleDots.Constants.ENTITY_TYPES.CANDY_ENTITY;
  18. this.originalColor = null;
  19. this.velocity = new RealtimeMultiplayerGame.model.Point(0, 0);
  20. this.acceleration = new RealtimeMultiplayerGame.model.Point(0, 0);
  21. return this;
  22. };
  23. BubbleDots.CircleEntity.prototype = {
  24. radius: BubbleDots.Constants.ENTITY_DEFAULT_RADIUS,
  25. velocity: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
  26. acceleration: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
  27. collisionCircle: null, // An instance of RealtimeMultiplayerGame.modules.circlecollision.PackedCircle
  28. entityType: null,
  29. color: "2",
  30. originalColor: "2",
  31. _tween: null,
  32. // Movement properties
  33. velocityMax: 8.0,
  34. velocityDamping: 0.98,
  35. /**
  36. * Update the entity's view - this is only called on the clientside
  37. */
  38. updateView: function () {
  39. if (!this.view) return;
  40. this.view.x = this.position.x;// - this.radius;
  41. this.view.y = this.position.y;// - this.radius;
  42. this.view.setScale(this.lastReceivedEntityDescription.scale * 0.01, this.lastReceivedEntityDescription.scale * 0.01);
  43. return;
  44. var diameter = this.lastReceivedEntityDescription.radius * 2;
  45. this.view.setSize(diameter, diameter);
  46. this.view.setFillStyle("#" + this.lastReceivedEntityDescription.color); // Random color
  47. },
  48. /**
  49. * Update position of this entity - this is only called on the serverside
  50. * @param {Number} speedFactor A number signifying how much faster or slower we are moving than the target framerate
  51. * @param {Number} gameClock Current game time in seconds (zero based)
  52. * @param {Number} gameTick Current game tick (incrimented each frame)
  53. */
  54. updatePosition: function (speedFactor, gameClock, gameTick) {
  55. this.handleAcceleration(speedFactor, gameClock, gameTick);
  56. },
  57. handleAcceleration: function (speedFactor, gameClock, gameTick) {
  58. this.velocity.translatePoint(this.acceleration);
  59. // this.velocity.limit(this.velocityMax);
  60. this.velocity.multiply(this.velocityDamping);
  61. this.collisionCircle.position.translatePoint(this.velocity);
  62. this.position = this.collisionCircle.position.clone();
  63. this.acceleration.set(0, 0);
  64. },
  65. /**
  66. * Called when this object has collided with another
  67. * @param a Object A in the collision pair, note this may be this object
  68. * @param b Object B in the collision pair, note this may be this object
  69. * @param collisionNormal A vector describing the collision
  70. */
  71. onCollision: function (a, b, collisionNormal) {
  72. },
  73. tempColor: function () {
  74. var that = this;
  75. clearTimeout(this.timeout);
  76. this.color = "1";
  77. this.timeout = setTimeout(function () {
  78. that.setColor(that.originalColor);
  79. }, 50);
  80. },
  81. /**
  82. * Deallocate memory
  83. */
  84. dealloc: function () {
  85. this.collisionCircle.dealloc();
  86. this.collisionCircle = null;
  87. BubbleDots.CircleEntity.superclass.dealloc.call(this);
  88. },
  89. constructEntityDescription: function () {
  90. var entityDesc = BubbleDots.CircleEntity.superclass.constructEntityDescription.call(this);
  91. entityDesc += ',' + ~~(this.scale * 100);
  92. entityDesc += ',' + this.color;
  93. return entityDesc;
  94. },
  95. ///// ACCESSORS
  96. /**
  97. * Set the CollisionCircle for this game entity.
  98. * @param aCollisionCircle
  99. */
  100. setCollisionCircle: function (aCollisionCircle) {
  101. this.collisionCircle = aCollisionCircle;
  102. this.collisionCircle.collisionMask = 1;
  103. this.collisionCircle.collisionGroup = 2;
  104. this.collisionCircle.setDelegate(this);
  105. this.collisionCircle.setPosition(this.position.clone());
  106. this.collisionCircle.setRadius(this.radius);
  107. },
  108. getCollisionCircle: function () {
  109. return this.collisionCircle
  110. },
  111. /**
  112. * Set the color of this entity, a property originalColor is also stored
  113. * @param aColor
  114. */
  115. setColor: function (aColor) {
  116. if (!this.originalColor) {
  117. this.originalColor = aColor;
  118. }
  119. this.color = aColor;
  120. },
  121. getColor: function () {
  122. return this.color
  123. },
  124. getOriginalColor: function () {
  125. return this.originalColor
  126. },
  127. setRadius: function (aRadius) {
  128. this.radius = aRadius;
  129. this.collisionCircle.setRadius(this.radius);
  130. this.scale = this.radius / BubbleDots.Constants.ENTITY_DEFAULT_RADIUS;
  131. },
  132. getRadius: function () {
  133. return this.radius;
  134. }
  135. };
  136. // extend RealtimeMultiplayerGame.model.GameEntity
  137. RealtimeMultiplayerGame.extend(BubbleDots.CircleEntity, RealtimeMultiplayerGame.model.GameEntity, null);
  138. })();