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

/js/DemoCircles/CircleEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 134 lines | 79 code | 18 blank | 37 comment | 2 complexity | 02174bf2b0ae46d8c2992545b8721dbe 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. DemoApp.CircleEntity
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. DemoApp
  8. Abstract:
  9. This is the base entity for the demo game
  10. Basic Usage:
  11. Version:
  12. 1.0
  13. */
  14. (function () {
  15. var nOffset = Math.random() * 2000;
  16. DemoApp.CircleEntity = function (anEntityid, aClientid) {
  17. DemoApp.CircleEntity.superclass.constructor.call(this, anEntityid, aClientid);
  18. this.setColor("FFFFFF");
  19. this.velocity = new RealtimeMultiplayerGame.model.Point(0, 0);
  20. this.acceleration = new RealtimeMultiplayerGame.model.Point(0, 0);
  21. return this;
  22. };
  23. DemoApp.CircleEntity.prototype = {
  24. radius: DemoApp.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: DemoApp.Constants.ENTITY_TYPES.GENERIC_CIRCLE,
  29. /**
  30. * Update the entity's view - this is only called on the clientside
  31. */
  32. updateView: function () {
  33. if (!this.view) return;
  34. this.view.x = this.position.x - this.radius;
  35. this.view.y = this.position.y - this.radius;
  36. var diameter = this.lastReceivedEntityDescription.radius * 2;
  37. this.view.setSize(diameter, diameter);
  38. this.view.setFillStyle("#" + this.lastReceivedEntityDescription.color);
  39. },
  40. /**
  41. * Update position of this entity - this is only called on the serverside
  42. * @param {Number} speedFactor A number signifying how much faster or slower we are moving than the target framerate
  43. * @param {Number} gameClock Current game time in seconds (zero based)
  44. * @param {Number} gameTick Current game tick (incrimented each frame)
  45. */
  46. updatePosition: function (speedFactor, gameClock, gameTick) {
  47. // Modify velocity using perlin noise
  48. var theta = 0.008;
  49. var noise = RealtimeMultiplayerGame.model.noise(nOffset + this.position.x * theta, nOffset + this.position.y * theta, gameTick * 0.003);
  50. var angle = noise * 12;
  51. var speed = 0.2;
  52. this.acceleration.x += Math.cos(angle) * speed - 0.3;
  53. this.acceleration.y -= Math.sin(angle) * speed;
  54. this.velocity.translatePoint(this.acceleration);
  55. this.velocity.limit(5);
  56. this.velocity.multiply(0.9);
  57. this.acceleration.set(0, 0);
  58. this.collisionCircle.position.translatePoint(this.velocity);
  59. this.position = this.collisionCircle.position.clone();
  60. },
  61. tempColor: function () {
  62. var that = this;
  63. clearTimeout(this.timeout);
  64. this.color = "FF0000";
  65. this.timeout = setTimeout(function () {
  66. that.setColor(that.originalColor);
  67. }, 50);
  68. },
  69. /**
  70. * Deallocate memory
  71. */
  72. dealloc: function () {
  73. this.collisionCircle.dealloc();
  74. this.collisionCircle = null;
  75. DemoApp.CircleEntity.superclass.dealloc.call(this);
  76. },
  77. constructEntityDescription: function () {
  78. return DemoApp.CircleEntity.superclass.constructEntityDescription.call(this) + ',' + this.radius + ',' + this.color;
  79. },
  80. ///// ACCESSORS
  81. /**
  82. * Set the CollisionCircle for this game entity.
  83. * @param aCollisionCircle
  84. */
  85. setCollisionCircle: function (aCollisionCircle) {
  86. this.collisionCircle = aCollisionCircle;
  87. this.collisionCircle.setDelegate(this);
  88. this.collisionCircle.setPosition(this.position.clone());
  89. this.collisionCircle.setRadius(this.radius);
  90. this.collisionCircle.collisionMask = 1;
  91. this.collisionCircle.collisionGroup = 1;
  92. },
  93. getCollisionCircle: function () {
  94. return this.collisionCircle
  95. },
  96. /**
  97. * Set the color of this entity, a property originalColor is also stored
  98. * @param aColor
  99. */
  100. setColor: function (aColor) {
  101. if (!this.originalColor) {
  102. this.originalColor = aColor;
  103. }
  104. this.color = aColor;
  105. },
  106. getColor: function () {
  107. return this.color
  108. },
  109. getOriginalColor: function () {
  110. return this.originalColor
  111. }
  112. };
  113. // extend RealtimeMultiplayerGame.model.GameEntity
  114. RealtimeMultiplayerGame.extend(DemoApp.CircleEntity, RealtimeMultiplayerGame.model.GameEntity, null);
  115. })();