touchControls.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. class TouchControls {
  2. constructor(canvas, inputHandler) {
  3. this.canvas = canvas;
  4. this.input = inputHandler;
  5. this.touchZones = {
  6. left: { x: 0, y: 0, width: 0, height: 0 },
  7. right: { x: 0, y: 0, width: 0, height: 0 },
  8. jump: { x: 0, y: 0, width: 0, height: 0 },
  9. crouch: { x: 0, y: 0, width: 0, height: 0 }
  10. };
  11. this.lastTapTime = 0;
  12. this.doubleTapDelay = 300; // ms
  13. this.lastTapSide = null;
  14. this.setupTouchZones();
  15. this.addTouchListeners();
  16. }
  17. setupTouchZones() {
  18. // Usar dimensiones del canvas directamente
  19. const width = this.canvas.width;
  20. const height = this.canvas.height;
  21. // Zona izquierda (1/3 izquierdo, parte inferior)
  22. this.touchZones.left = {
  23. x: 0,
  24. y: height * 0.4, // Desde 40% hacia abajo
  25. width: width * 0.35,
  26. height: height * 0.6
  27. };
  28. // Zona derecha (1/3 derecho, parte inferior)
  29. this.touchZones.right = {
  30. x: width * 0.65,
  31. y: height * 0.4,
  32. width: width * 0.35,
  33. height: height * 0.6
  34. };
  35. // Zona de salto (mitad superior)
  36. this.touchZones.jump = {
  37. x: 0,
  38. y: 0,
  39. width: width,
  40. height: height * 0.4
  41. };
  42. // Zona de agacharse (centro inferior)
  43. this.touchZones.crouch = {
  44. x: width * 0.35,
  45. y: height * 0.8,
  46. width: width * 0.3,
  47. height: height * 0.2
  48. };
  49. }
  50. addTouchListeners() {
  51. // Prevenir comportamiento por defecto del navegador
  52. this.canvas.addEventListener('touchstart', (e) => {
  53. e.preventDefault();
  54. this.handleTouchStart(e);
  55. }, { passive: false });
  56. this.canvas.addEventListener('touchend', (e) => {
  57. e.preventDefault();
  58. this.handleTouchEnd(e);
  59. }, { passive: false });
  60. this.canvas.addEventListener('touchmove', (e) => {
  61. e.preventDefault();
  62. }, { passive: false });
  63. // Redimensionar zonas cuando cambie el tamaño
  64. window.addEventListener('resize', () => {
  65. this.setupTouchZones();
  66. });
  67. }
  68. getTouchPosition(touch) {
  69. const rect = this.canvas.getBoundingClientRect();
  70. const scaleX = this.canvas.width / rect.width;
  71. const scaleY = this.canvas.height / rect.height;
  72. return {
  73. x: (touch.clientX - rect.left) * scaleX,
  74. y: (touch.clientY - rect.top) * scaleY
  75. };
  76. }
  77. isInZone(pos, zone) {
  78. return pos.x >= zone.x &&
  79. pos.x <= zone.x + zone.width &&
  80. pos.y >= zone.y &&
  81. pos.y <= zone.y + zone.height;
  82. }
  83. handleTouchStart(e) {
  84. for (let touch of e.touches) {
  85. const pos = this.getTouchPosition(touch);
  86. console.log('Touch at:', pos.x, pos.y);
  87. console.log('Zones:', this.touchZones);
  88. if (this.isInZone(pos, this.touchZones.jump)) {
  89. console.log('Jump zone touched');
  90. // Simular tecla W presionada
  91. this.input.keys.W = true;
  92. this.input.keys.w = true;
  93. } else if (this.isInZone(pos, this.touchZones.crouch)) {
  94. console.log('Crouch zone touched');
  95. // Simular tecla ArrowDown presionada
  96. this.input.keys.ArrowDown = true;
  97. this.input.keys.s = true;
  98. this.input.keys.S = true;
  99. } else if (this.isInZone(pos, this.touchZones.left)) {
  100. console.log('Left zone touched');
  101. // Simular tecla A presionada
  102. this.input.keys.A = true;
  103. this.input.keys.a = true;
  104. this.checkDoubleTap('left');
  105. } else if (this.isInZone(pos, this.touchZones.right)) {
  106. console.log('Right zone touched');
  107. // Simular tecla D presionada
  108. this.input.keys.D = true;
  109. this.input.keys.d = true;
  110. this.checkDoubleTap('right');
  111. }
  112. }
  113. }
  114. handleTouchEnd(e) {
  115. // Liberar todas las teclas cuando se levanta el dedo
  116. this.input.keys.A = false;
  117. this.input.keys.a = false;
  118. this.input.keys.D = false;
  119. this.input.keys.d = false;
  120. this.input.keys.W = false;
  121. this.input.keys.w = false;
  122. this.input.keys.ArrowDown = false;
  123. this.input.keys.s = false;
  124. this.input.keys.S = false;
  125. console.log('Touch ended - all keys released');
  126. }
  127. checkDoubleTap(side) {
  128. const now = Date.now();
  129. if (this.lastTapSide === side &&
  130. now - this.lastTapTime < this.doubleTapDelay) {
  131. // Doble tap detectado - activar dash direccional
  132. this.input.keys.Shift = true;
  133. // Liberar Shift después de un frame
  134. setTimeout(() => {
  135. this.input.keys.Shift = false;
  136. }, 50);
  137. this.lastTapTime = 0; // Resetear para evitar múltiples activaciones
  138. } else {
  139. this.lastTapTime = now;
  140. this.lastTapSide = side;
  141. }
  142. }
  143. // Método para dibujar las zonas táctiles (debug)
  144. drawTouchZones(ctx) {
  145. if (!this.showDebug) return;
  146. ctx.save();
  147. ctx.globalAlpha = 0.3;
  148. // Zona izquierda
  149. ctx.fillStyle = '#ff0000';
  150. ctx.fillRect(
  151. this.touchZones.left.x,
  152. this.touchZones.left.y,
  153. this.touchZones.left.width,
  154. this.touchZones.left.height
  155. );
  156. // Zona derecha
  157. ctx.fillStyle = '#00ff00';
  158. ctx.fillRect(
  159. this.touchZones.right.x,
  160. this.touchZones.right.y,
  161. this.touchZones.right.width,
  162. this.touchZones.right.height
  163. );
  164. // Zona de salto
  165. ctx.fillStyle = '#0000ff';
  166. ctx.fillRect(
  167. this.touchZones.jump.x,
  168. this.touchZones.jump.y,
  169. this.touchZones.jump.width,
  170. this.touchZones.jump.height
  171. );
  172. // Zona de agacharse
  173. ctx.fillStyle = '#ffff00';
  174. ctx.fillRect(
  175. this.touchZones.crouch.x,
  176. this.touchZones.crouch.y,
  177. this.touchZones.crouch.width,
  178. this.touchZones.crouch.height
  179. );
  180. ctx.restore();
  181. }
  182. toggleDebug() {
  183. this.showDebug = !this.showDebug;
  184. }
  185. }
  186. // Exportar para uso global
  187. window.TouchControls = TouchControls;