player.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. class Player {
  2. constructor(x, y, spriteSheet) {
  3. this.x = x;
  4. this.y = y;
  5. this.width = 24;
  6. this.height = 34;
  7. this.speed = 0.25;
  8. this.direction = 'right';
  9. // Animation
  10. this.frameIndex = 0;
  11. this.frameTimer = 0;
  12. this.idleFrameInterval = 60; // Más lento para idle
  13. this.walkFrameInterval = 15; // Más rápido para caminar
  14. this.isMoving = false;
  15. // Sprite coordinates from the 930x614 sprite sheet
  16. // Adjust these based on your actual sprite positions
  17. this.sprites = {
  18. idle: [
  19. { x: 370, y: 15, w: 24, h: 34 },
  20. { x: 341, y: 15, w: 24, h: 34 },
  21. { x: 404, y: 15, w: 24, h: 34 },
  22. { x: 341, y: 15, w: 24, h: 34 }
  23. ],
  24. walk: [
  25. { x: 2, y: 16, w: 24, h: 34 },
  26. { x: 28, y: 16, w: 24, h: 34 },
  27. { x: 54, y: 16, w: 24, h: 34 },
  28. { x: 80, y: 16, w: 24, h: 34 },
  29. { x: 106, y: 16, w: 24, h: 34 },
  30. { x: 132, y: 16, w: 24, h: 34 }
  31. ]
  32. };
  33. this.currentAnimation = 'idle';
  34. this.spriteSheet = spriteSheet;
  35. }
  36. update() {
  37. this.isMoving = false;
  38. // Horizontal movement
  39. if (input.isMovingLeft()) {
  40. this.x -= this.speed;
  41. this.direction = 'left';
  42. this.isMoving = true;
  43. }
  44. if (input.isMovingRight()) {
  45. this.x += this.speed;
  46. this.direction = 'right';
  47. this.isMoving = true;
  48. }
  49. // Keep player within horizontal bounds
  50. this.x = Math.max(0, Math.min(320 - this.width, this.x));
  51. // Update animation
  52. this.updateAnimation();
  53. }
  54. updateAnimation() {
  55. const newAnimation = this.isMoving ? 'walk' : 'idle';
  56. if (this.currentAnimation !== newAnimation) {
  57. this.currentAnimation = newAnimation;
  58. this.frameIndex = 0;
  59. this.frameTimer = 0;
  60. }
  61. this.frameTimer++;
  62. const frames = this.sprites[this.currentAnimation];
  63. // Usar diferentes velocidades según la animación
  64. const frameInterval = this.currentAnimation === 'walk' ? this.walkFrameInterval : this.idleFrameInterval;
  65. if (this.frameTimer >= frameInterval) {
  66. this.frameTimer = 0;
  67. this.frameIndex = (this.frameIndex + 1) % frames.length;
  68. }
  69. }
  70. draw(ctx) {
  71. const frame = this.sprites[this.currentAnimation][this.frameIndex];
  72. ctx.save();
  73. if (this.direction === 'left') {
  74. ctx.scale(-1, 1);
  75. ctx.drawImage(
  76. this.spriteSheet,
  77. frame.x, frame.y, frame.w, frame.h,
  78. -this.x - this.width, this.y,
  79. this.width, this.height
  80. );
  81. } else {
  82. ctx.drawImage(
  83. this.spriteSheet,
  84. frame.x, frame.y, frame.w, frame.h,
  85. this.x, this.y,
  86. this.width, this.height
  87. );
  88. }
  89. ctx.restore();
  90. }
  91. }