PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/js/DemoBox2D/DemoBox2DEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 81 lines | 38 code | 9 blank | 34 comment | 2 complexity | 61555c48e8e7d798d4d8d4a6f025e576 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. DemoBox2D.Box2DEntity
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. DemoBox2D
  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 DAG2RAD = 0.0174532925;
  16. var RAD2DEG = 57.2957795;
  17. DemoBox2D.Box2DEntity = function (anEntityid, aClientid) {
  18. DemoBox2D.Box2DEntity.superclass.constructor.call(this, anEntityid, aClientid);
  19. };
  20. DemoBox2D.Box2DEntity.prototype = {
  21. b2Body: null, // Reference to Box2D body
  22. radius: 1,
  23. entityType: DemoBox2D.Constants.ENTITY_TYPES.BOX,
  24. /**
  25. * @inheritDoc
  26. */
  27. updateView: function () {
  28. if (!this.view) return;
  29. this.view.x = this.position.x - this.radius;
  30. this.view.y = this.position.y - this.radius;
  31. this.view.setRotation(this.lastReceivedEntityDescription.rotation * DAG2RAD);
  32. },
  33. /**
  34. * @inheritDoc
  35. */
  36. updatePosition: function (speedFactor, gameClock, gameTick) {
  37. this.position.x = this.b2Body.m_xf.position.x * DemoBox2D.Constants.PHYSICS_SCALE;
  38. this.position.y = this.b2Body.m_xf.position.y * DemoBox2D.Constants.PHYSICS_SCALE;
  39. this.rotation = this.b2Body.GetAngle();
  40. },
  41. /**
  42. * @inheritDoc
  43. */
  44. constructEntityDescription: function () {
  45. // Send the regular entity description, but also send 'radius' and a rounded version 'rotation'
  46. return DemoBox2D.Box2DEntity.superclass.constructEntityDescription.call(this) + ',' + this.radius + "," + ~~(this.rotation * RAD2DEG);
  47. },
  48. /**
  49. * @inheritDoc
  50. */
  51. dealloc: function () {
  52. if (this.b2Body) {
  53. // Destroy box2d body -
  54. }
  55. DemoBox2D.Box2DEntity.superclass.dealloc.call(this);
  56. },
  57. ///// ACCESSORS
  58. /**
  59. * Set the Box2D body that represents this entity
  60. * @param aBox2dBody
  61. */
  62. setBox2DBody: function (aBox2dBody) {
  63. this.b2Body = aBox2dBody;
  64. },
  65. getBox2DBody: function () {
  66. return this.b2Body
  67. }
  68. };
  69. // extend RealtimeMultiplayerGame.model.GameEntity
  70. RealtimeMultiplayerGame.extend(DemoBox2D.Box2DEntity, RealtimeMultiplayerGame.model.GameEntity, null);
  71. })();