Juckniz

Untitled

May 29th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. /**
  2. * Clase Bullet que construye un Bullet
  3. * @class Bullet
  4. * @constructors
  5. * @this {Bullet}
  6. * @param {Number} x Posicion X en el que se encuentra
  7. * @param {Number} y Posicion Y en el que se encuentra
  8. * @param {Enemy} Enemy enemigo que tiene seleccionado
  9. * @param {Number} damage dano que inflije la bala
  10. * @param {Number} index indice para sacar la informacion del json
  11. * @param {String} direccion direccion que va a disprar
  12. * @returns {Bullet}
  13. */
  14. function Bullet(x, y, Enemy, damage, index, direccion) {
  15. /* * Posicion X en el que se encuentra. Por defecto 0.
  16. * @type Number
  17. */
  18. this.x = (x == null) ? 0 : x;
  19. /* * Posicion Y en el que se encuentra. Por defecto 0.
  20. * @type Number
  21. */
  22. this.y = (y == null) ? 0 : y;
  23. /* * Posicion X inicial coje el valor de X al construir el objeto.
  24. * @type Number
  25. */
  26. this.xi = this.x;
  27. /* * Posicion Y inicial coje el valor de Y al construir el objeto.
  28. * @type Number
  29. */
  30. this.yi = this.y;
  31. /**
  32. * Direccion donde va disparar "bot" || "top" || "left" || "right"
  33. * solo se usara si bulletType es "lineal" o "area".
  34. * @type String
  35. */
  36. this.direccion = direccion;
  37. /**
  38. * Tipo de la bala coje el valor del json tipotower.
  39. * @type String
  40. */
  41. this.bulletType = tipotower[index].bulletType;
  42. /**
  43. * Anchura de la bala coje el valor del json tipotower.
  44. * @type Number
  45. */
  46. this.width = tipotower[index].bulletSize;
  47. /**
  48. * Altura de la bala coje el valor del json tipotower.
  49. * @type Number
  50. *
  51. */
  52. this.height = tipotower[index].bulletSize;
  53. /**
  54. * Dano de la bala por defecto 0.
  55. * @type Number
  56. */
  57. this.damage = (damage == null) ? 0 : damage;
  58. /**
  59. * Imagen de la bala coje la src del json tipotower.
  60. * @type Image
  61. */
  62. this.img = new Image();
  63. this.img.src = tipotower[index].imgbullet;
  64. /**
  65. * Enemigo que tiene seleccionado solo lo utilizara si el bulletType
  66. * es "target"
  67. * @type Enemy
  68. */
  69. this.Enemy = Enemy;
  70. /**
  71. * Velocidad de la bala coje el valor del json tipotower.
  72. * @type Number
  73. */
  74. this.velocity = tipotower[index].velocity;
  75. this.radio = tipotower[index].radio;
  76. this.countradio = 0;
  77.  
  78. /**
  79. * Metodo que comprueba si se ha chocado con otro cuerpo
  80. * @param {type} rect
  81. * @returns {boolean}
  82. */
  83. this.intersects = function(rect) {
  84. if (rect != null) {
  85. return(this.x <= rect.x + rect.width &&
  86. this.x + this.width > rect.x &&
  87. this.y <= rect.y + rect.height &&
  88. this.y + this.height > rect.y);
  89. }
  90. }
  91.  
  92. /**
  93. * Metodo para mover la bala. Actualiza su posicion.
  94. * Si bulletType es target seguira al Enemy que tiene.
  95. * Si bulletType es lineal o area la bala mirara que direccion tiene
  96. * y se movera en esa direccion.
  97. * @returns {void}
  98. */
  99. this.move = function() {
  100. if (this.bulletType == "target") {
  101. var dx = Enemy.x + Enemy.width / 2 - this.x;
  102. var dy = Enemy.y + Enemy.height / 2 - this.y;
  103. var angle = Math.atan(dy / dx);
  104. var dxf = Math.cos(angle) * this.velocity;
  105. var dyf = Math.sin(angle) * this.velocity;
  106.  
  107. if (dx < 0) {
  108. dxf = dxf * -1;
  109. dyf = dyf * -1;
  110. }
  111.  
  112. this.x += dxf;
  113. this.y += dyf;
  114.  
  115. } else if (this.bulletType == "lineal" || this.bulletType == "area") {
  116. switch (this.direccion) {
  117. case "top":
  118. this.y -= this.velocity;
  119. break;
  120. case "left":
  121. this.x -= this.velocity;
  122. break;
  123. case "bot":
  124. this.y += this.velocity;
  125. break;
  126. case "right":
  127. this.x += this.velocity;
  128. break;
  129. }
  130. this.countradio += this.velocity;
  131.  
  132. }
  133.  
  134.  
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment