touchControls.js 5.7 KB

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