mobile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function Mobile() {
  2. this.id = 0;
  3. this.position = {x: 0, y: 0};
  4. this.targetPosition = {x: 0, y: 0};
  5. this.width = 32;
  6. this.height = 32;
  7. this.maxSpeed = 2;
  8. this.turnRate = Math.PI / 64;
  9. this.targetAngle = 0;
  10. this.angleRadians = 0;
  11. this.color = "cyan";
  12. this.name = "Mobile";
  13. this.spriteCanvas = null;
  14. this.spriteContext = null;
  15. this.isAccelerating = false;
  16. this.sprite = "human1";
  17. this.isDead = false;
  18. this.health = 5;
  19. this.maxHealth = 5;
  20. this.seen = [];
  21. this.init = function(mobileData) {
  22. for(var key in mobileData){
  23. if(!this.hasOwnProperty(key)) { continue; }
  24. this[key] = mobileData[key];
  25. }
  26. }
  27. this.update = function(delta) {
  28. this.angleRadians = system.angleTo(this.position, this.targetPosition);
  29. if(this.targetPosition.x != this.position.x) {
  30. if(Math.abs(this.position.x - this.targetPosition.x) < this.maxSpeed) {
  31. this.position.x = this.targetPosition.x;
  32. this.targetAngle = this.angleRadians;
  33. } else {
  34. this.targetAngle = this.angleRadians;
  35. this.position.x += this.maxSpeed * Math.cos(this.angleRadians) * delta;
  36. }
  37. }
  38. if(this.targetPosition.y != this.position.y) {
  39. if(Math.abs(this.position.y - this.targetPosition.y) < this.maxSpeed) {
  40. this.position.y = this.targetPosition.y;
  41. this.targetAngle = this.angleRadians;
  42. } else {
  43. this.targetAngle = this.angleRadians;
  44. this.position.y += (this.maxSpeed) * Math.sin(this.angleRadians) * delta;
  45. }
  46. }
  47. }
  48. this.preRender = function(context, angle) {
  49. if(angle == null) {
  50. angle = 0;
  51. }
  52. context.fillStyle = this.color;
  53. context.strokeStyle = this.color;
  54. context.lineWidth = 4;
  55. context.save();
  56. context.translate(0.5, 0.5);
  57. //context.rotate(angle);
  58. /*context.fillStyle = "rgba(0,0,0,0.1)";
  59. system.ellipse(context, 0, 8, 16, 4);
  60. context.fill();*/
  61. context.drawImage(system.assets.getSprite(this.sprite), 0, 0, 16, 16, 0, 0, this.width, this.height);
  62. /*context.beginPath();
  63. context.moveTo(this.width / 2, 0);
  64. context.lineTo(-(this.width / 2), -(this.height / 3));
  65. context.lineTo(-(this.width / 2), this.height / 3);
  66. context.lineTo(this.width / 2, 0);
  67. context.fill();*/
  68. context.restore();
  69. }
  70. this.draw = function(context) {
  71. if(this.spriteContext == null) {
  72. this.spriteCanvas = document.createElement("canvas");
  73. this.spriteCanvas.width = this.width;
  74. this.spriteCanvas.height = this.height;
  75. this.spriteContext = this.spriteCanvas.getContext("2d");
  76. this.preRender(this.spriteContext);
  77. }
  78. if(this.isDead) {
  79. return;
  80. }
  81. context.save();
  82. context.translate(this.position.x - (this.width / 2), this.position.y - (this.height / 2));
  83. //context.rotate(this.targetAngle);
  84. if(this.isAccelerating) {
  85. context.fillStyle = "#7777FF";
  86. context.beginPath();
  87. context.moveTo(-this.width - 5, 0);
  88. context.lineTo(-this.width / 2, 3);
  89. context.lineTo(-this.width / 2, -3);
  90. context.lineTo(-this.width - 5, 0);
  91. context.fill();
  92. }
  93. context.drawImage(this.spriteCanvas, 0, 0);
  94. //bounds box
  95. /*context.strokeStyle = "#FF7733";
  96. context.beginPath();
  97. context.moveTo(0, 0);
  98. context.lineTo(this.width, 0);
  99. context.lineTo(this.width, this.height);
  100. context.lineTo(0, this.height);
  101. context.lineTo(0, 0);
  102. context.stroke();*/
  103. context.strokeStyle = "#00FF00";
  104. if(this.isTakingDamage) {
  105. context.strokeStyle = "#FF0000";
  106. this.isTakingDamage = false;
  107. }
  108. context.restore();
  109. /*context.save();
  110. context.translate(this.position.x, this.position.y);
  111. context.font = "16px sans-serif";
  112. context.textAlign = "center";
  113. context.textBaseline = "middle";
  114. context.fillStyle = "#DDDDDD";
  115. context.strokeStyle = "#333333";
  116. context.lineWidth = 2;
  117. //var message = this.player.playerName + " " + this.player.id;
  118. //var textMetrics = context.measureText(message);
  119. //var textWidth = textMetrics.width;
  120. //context.fillText(message, 0, 0);
  121. var message = parseInt(this.position.x) + ", " + parseInt(this.position.y);
  122. var textMetrics = context.measureText(message);
  123. var textWidth = textMetrics.width;
  124. context.fillText(message, 0, 0);
  125. var message = parseInt(this.targetPosition.x) + ", " + parseInt(this.targetPosition.y);
  126. var textMetrics = context.measureText(message);
  127. var textWidth = textMetrics.width;
  128. context.fillText(message, 0, 16);
  129. context.restore();*/
  130. context.save();
  131. context.translate(this.position.x, this.position.y);
  132. context.font = "16px sans-serif";
  133. context.textAlign = "center";
  134. context.textBaseline = "middle";
  135. context.fillStyle = "#DDDDDD";
  136. context.strokeStyle = "#182208";
  137. context.lineWidth = 2;
  138. var textMetrics = context.measureText(this.name);
  139. var textWidth = textMetrics.width;
  140. context.strokeText(this.name, 0, -(this.height));
  141. context.fillText(this.name, 0, -(this.height));
  142. context.restore();
  143. }
  144. };