PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/js/BubbleDots/entities/PlayerEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 103 lines | 61 code | 13 blank | 29 comment | 9 complexity | dd8cf9cb54c71535ede2e481e3b1fa2c 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. var count = 0;
  16. BubbleDots.PlayerEntity = function (anEntityid, aClientid) {
  17. BubbleDots.PlayerEntity.superclass.constructor.call(this, anEntityid, aClientid);
  18. this.entityType = BubbleDots.Constants.ENTITY_TYPES.PLAYER_ENTITY;
  19. this.initThrust();
  20. };
  21. BubbleDots.PlayerEntity.prototype = {
  22. _isThrusting: false, // We need a better variable name.
  23. _thrustLevel: 0,
  24. THRUST_DECREMENT: 0.001, // How much to decrease thrust by
  25. THRUST_FORCE: 0.3, // How much force to apply every tick when applying thrust
  26. initThrust: function () {
  27. this._thrustLevel = 100.0;
  28. this._isThrusting = false;
  29. },
  30. startThrust: function () {
  31. this._isThrusting = true;
  32. // this.velocity.y *= 0.5;
  33. },
  34. applyThrust: function () {
  35. this._thrustLevel -= BubbleDots.PlayerEntity.prototype.THRUST_DECREMENT;
  36. if (this._thrustLevel > 0.0) {
  37. this.acceleration.y -= BubbleDots.PlayerEntity.prototype.THRUST_FORCE;
  38. }
  39. },
  40. stopThrust: function () {
  41. this._isThrusting = false;
  42. },
  43. /**
  44. * Update position of this entity - this is only called on the serverside
  45. * @param {Number} speedFactor A number signifying how much faster or slower we are moving than the target framerate
  46. * @param {Number} gameClock Current game time in seconds (zero based)
  47. * @param {Number} gameTick Current game tick (incrimented each frame)
  48. */
  49. updatePosition: function (speedFactor, gameClock, gameTick) {
  50. this.handleInput(speedFactor);
  51. BubbleDots.PlayerEntity.superclass.updatePosition.call(this, speedFactor, gameClock, gameTick);
  52. },
  53. handleInput: function (speedFactor) {
  54. var moveSpeed = 0.2;
  55. if (this.input.isLeft()) this.acceleration.x -= moveSpeed;
  56. if (this.input.isRight()) this.acceleration.x += moveSpeed;
  57. if (this.input.isDown()) this.acceleration.y += moveSpeed;
  58. // We're pressing up - apply thrust...
  59. // Call startThrust if we were not thrusting before
  60. if (this.input.isUp()) {
  61. if (!this._isThrusting) {
  62. this.startThrust();
  63. }
  64. this.applyThrust();
  65. } else if (this._isThrusting) {
  66. this.stopThrust();
  67. } else { // Default behavior - increase _thrustLevel
  68. this._thrustLevel += BubbleDots.PlayerEntity.prototype.THRUST_DECREMENT * 2;
  69. this._thrustLevel = Math.min(this._thrustLevel, 100);
  70. }
  71. },
  72. ///// ACCESSORS
  73. /**
  74. * Set the CollisionCircle for this game entity.
  75. * @param aCollisionCircle
  76. */
  77. setCollisionCircle: function (aCollisionCircle) {
  78. BubbleDots.PlayerEntity.superclass.setCollisionCircle.call(this, aCollisionCircle);
  79. this.collisionCircle.collisionMask = 2;
  80. this.collisionCircle.collisionGroup = 1;
  81. this.collisionCircle.isFixed = true;
  82. },
  83. setInput: function (input) {
  84. this.input = input;
  85. }
  86. };
  87. // extend RealtimeMultiplayerGame.model.GameEntity
  88. RealtimeMultiplayerGame.extend(BubbleDots.PlayerEntity, BubbleDots.CircleEntity, null);
  89. })();