Skip to content
Snippets Groups Projects
Select Git revision
  • 5db9daed93aa67b432d697d776864b59499c3f80
  • main default protected
  • TypeScript
3 results

Player.js

Blame
  • Player.js 2.48 KiB
    class Player {
      image = new Image();
      constructor(x, y, radius, imageFolder, id) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.speed = 4;
        this.direction = {
          x: 0,
          y: 0,
        };
        this.imageId = 1;
        this.imageFolder = imageFolder;
        this.image.src = `/images/character/${this.imageFolder}/${this.imageFolder}${this.imageId}.png`;
      }
      draw(ctx) {
        // Dessiner le cercle
        ctx.beginPath();
        ctx.drawImage(
          this.image,
          this.x - this.radius,
          this.y - this.radius,
          this.radius * 2,
          this.radius * 2
        );
        ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
        ctx.strokeStyle = "black";
        ctx.stroke();
      }
    
      grow() {
        this.radius += 0.5;
        if (this.radius % 80 === 0 && this.imageId <= 5) {
          this.imageId += 1;
          this.image.src = `/images/character/${this.imageFolder}/${this.imageFolder}${this.imageId}.png`;
        }
      }
    
      eatSushi(sushis) {
        sushis.forEach((sushi, idx) => {
          const dx = sushi.x - this.x;
          const dy = sushi.y - this.y;
          const distance = Math.sqrt(dx * dx + dy * dy);
          if (distance < this.radius) {
            // Supprime la nourriture de la liste
            sushis.splice(idx, 1);
            // Augmente la taille du joueur
            this.grow();
          }
        });
      }
    
      move(ctx, mouseX, mouseY) {
        // Déplacer le cercle en fonction de la direction
        if (
          mouseX > ctx.canvas.width / 2 - this.radius &&
          mouseX < ctx.canvas.width / 2 + this.radius &&
          mouseY > ctx.canvas.height / 2 - this.radius &&
          mouseY < ctx.canvas.height / 2 + this.radius
        ) {
          return;
        }
        this.x += this.direction.x * this.speed;
        this.y += this.direction.y * this.speed;
    
        if (this.x < this.radius + ctx.canvas.width / 2)
          this.x = this.radius + ctx.canvas.width / 2;
        if (this.y < this.radius + ctx.canvas.height / 2)
          this.y = this.radius + ctx.canvas.height / 2;
        if (this.x > ctx.width - this.radius - ctx.canvas.width / 2) {
          this.x = ctx.width - this.radius - ctx.canvas.width / 2;
        }
        if (this.y > ctx.height - this.radius - ctx.canvas.height / 2) {
          this.y = ctx.height - this.radius - ctx.canvas.height / 2;
        }
      }
      moveTo(x, y, canvas) {
        const directionX = x - canvas.width / 2;
        const directionY = y - canvas.height / 2;
    
        const distance = Math.sqrt(directionX ** 2 + directionY ** 2);
        this.direction.x = directionX / distance;
        this.direction.y = directionY / distance;
      }
    }
    
    export default Player;