{"size": 8551, "ext": "js", "max_stars_repo_path": "game/class/Player.js", "max_stars_repo_name": "gavinhsmith/Un-Named-Platformer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "game/class/Player.js", "max_issues_repo_name": "gavinhsmith/Un-Named-Platformer", "max_issues_repo_issues_event_min_datetime": "2020-10-06T14:51:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-28T20:38:34.000Z", "max_forks_repo_path": "game/class/Player.js", "max_forks_repo_name": "gavinhsmith/Un-Named-Platformer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 28.4086378738, "max_line_length": 125, "alphanum_fraction": 0.549760262}
const Player = class {
  constructor(x,y,width,height,spd,jumph,controler) {
    this.x =              x;
    this.y =              y;
    this.sw =             width;
    this.sh =             0.5*height;
    this.fw =             width;
    this.fh =             height;
    this.width =          width;
    this.height =         height;
    this.color =          '#44f';
    this.speed =          spd;
    this.dx =             0;
    this.dy =             0;
    this.spritestilll =   ASSETS.SPRITES.PLAYER.STILL_LEFT;
    this.spritestillr =   ASSETS.SPRITES.PLAYER.STILL_RIGHT;
    this.spritewalkl =    ASSETS.SPRITES.PLAYER.WALK_LEFT;
    this.spritewalkr =    ASSETS.SPRITES.PLAYER.WALK_RIGHT;
    this.spritehearts =   ASSETS.SPRITES.PLAYER.HEARTS;
    this.jmph =           jumph;
    this.isJump =         true;
    this.forceJump =      false;
    this.isRun =          false;
    this.isSmall =        false;
    this.wasSmall =       false;
    this.lives =          5;
    this.level =          0;
    this.djc =            0;
    this.facing =         1;
    this.isMoving =       false;
    this.controler =      controler;
    this.debugOLC =       '#00f';
    this.pause =          false;
    this.hearthurtdtime = 0;
  }
  draw(ctx) {
    ctx.save();
    //ctx.fillStyle = this.color;
    //ctx.fillRect(Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height)
    if (this.facing == 0 && !this.isMoving) {
      this.spritestilll.update(ctx,Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height,undefined);
    } else if (this.facing == 1 && !this.isMoving) {
      this.spritestillr.update(ctx,Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height,undefined);
    } else if (this.facing == 1 && this.isMoving) {
      this.spritewalkr.update(ctx,Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height,undefined);
    } else if (this.facing == 0 && this.isMoving) {
      this.spritewalkl.update(ctx,Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height,undefined);
    };

    if (debug) {
      ctx.strokeStyle = this.debugOLC;
      ctx.strokeRect(Math.floor(this.x+camera.x),Math.floor(this.y+camera.y),this.width,this.height);
    }

    ctx.restore();
  }
  resetmove() {
    this.dx = 0;
    this.dy = 0;
    this.isJump = true;
  }
  die(lvl) {
    this.lives--;
    this.spritehearts.updatestate();
    this.resetmove();
    this.x = lvl.sx-(this.width/2);
    this.y = lvl.sy-(this.height/2);
    this.hearthurtdtime = 2;
    Entity.resetall();
  }
  nextlvl() {
    if (!LEVELS[this.level+1]) return;
    if (LEVELS[this.level].theme != LEVELS[this.level+1].theme) {
      LEVELS[this.level].audio.main.stop();
    }
    this.level++;
    if (LEVELS[this.level].theme != LEVELS[this.level-1].theme) {
      LEVELS[this.level].audio.main.play();
    }
    this.resetmove();
    this.x = LEVELS[this.level].sx-(this.width/2);
    this.y = LEVELS[this.level].sy-(this.height/2);
    Entity.resetall();
  };
  setlvl(lvl) {
    if (!LEVELS[lvl]) return;
    var oldlvl = this.level;
    if (LEVELS[this.level].theme != LEVELS[lvl].theme) {
      LEVELS[this.level].audio.main.stop();
    }
    this.level = lvl;
    if (LEVELS[this.level].theme != LEVELS[oldlvl].theme) {
      LEVELS[this.level].audio.main.play();
    }
    this.resetmove();
    this.x = LEVELS[this.level].sx-(this.width/2);
    this.y = LEVELS[this.level].sy-(this.height/2);
    Entity.resetall();
  };
  prevlvl() {
    if (!LEVELS[this.level-1]) return;
    if (LEVELS[this.level].theme != LEVELS[this.level-1].theme) {
      LEVELS[this.level].audio.main.stop();
    }
    this.level--;
    if (LEVELS[this.level].theme != LEVELS[this.level+1].theme) {
      LEVELS[this.level].audio.main.play();
    }
    this.resetmove();
    this.x = LEVELS[this.level].sx-(this.width/2);
    this.y = LEVELS[this.level].sy-(this.height/2);
    Entity.resetall();
  };
  reset() {
    this.setlvl(0);
    this.lives = 5;
    this.spritehearts.setstate(0,0);
    this.facing = 1;
    this.resetmove();
    Entity.resetall();
  }
  collideObj(objlist) {
    if (this.dx >= 0.01) {
      for (var i = 0.1; i < this.dx; i++) {
        this.x += 1;
        this.facing = 1;
        this.isMoving = true;

        for (var b in objlist) {
          if (checkIfRectOverlap(this,objlist[b])) {
            this.x -= 1;
            this.dx = 0;
            this.isMoving = false;
          };
        };

      };
    } else {
      for (var i = this.dx; i < -0.1; i++) {
        this.x -= 1;
        this.facing = 0;
        this.isMoving = true;

        for (var b in objlist) {
          if (checkIfRectOverlap(this,objlist[b])) {
            this.x += 1;
            this.dx = 0;
            this.isMoving = false;
          };
        };

      };
    };

    if (this.dy >= 0) {
      for (var i = 0.1; i < this.dy; i++) {
        this.y += 1;

        for (var b in objlist) {
          if (checkIfRectOverlap(this,objlist[b])) {
            this.y -= 1;
            this.dy = 0;
            this.forceJump = true;
          };
        };

      };
    } else {
      for (var i = this.dy; i < -0.1; i++) {
        this.y -= 1;

        for (var b in objlist) {
          if (checkIfRectOverlap(this,objlist[b])) {
            this.y += 1;
            this.dy = 0;
          };
        };

      };
    };
  }
  move(fri,grav,objlist) {
    this.isRun = false;
    this.isMoving = false;
    var joystickdeadzone = 0.1;
    /*
    this.isSmall = false;
    if (inputcheck(BINDINGS.CROUCH)) {
      this.isSmall = true;
      this.wasSmall = true;
    };
    */
    GameEvent.turnoff('player.move.jump');
    GameEvent.turnoff('player.move.left');
    GameEvent.turnoff('player.move.right');
    if (inputcheck(BINDINGS.LEFT,this.controler).state) {
      this.dx -= inputcheck(BINDINGS.LEFT,this.controler).val*this.speed;
      GameEvent.turnon('player.move.left');
    };
    if (inputcheck(BINDINGS.RIGHT,this.controler).state) {
      this.dx += inputcheck(BINDINGS.RIGHT,this.controler).val*this.speed;
      GameEvent.turnon('player.move.right');
    };
    if (inputcheck(BINDINGS.JUMP,this.controler).state && !this.isJump) {
      this.dy -= /*(this.isSmall) ? 0.5*this.jmph : */this.jmph;
      this.isJump = true;
      GameEvent.turnon('player.move.jump');
    };
    if (inputcheck(BINDINGS.RUN,this.controler).state) {
      this.isRun = true;
    };
    if (inputcheck(BINDINGS.PAUSE,this.controler).state) {
      this.pause = true;
    };

    if (inputcheck(BINDINGS.D_NEXTLEVEL,this.controler).state && this.djc <= 0) {
      this.djc = 10;
      this.nextlvl();
    };
    if (inputcheck(BINDINGS.D_PREVLEVEL,this.controler).state && this.djc <= 0) {
      this.djc = 10;
      this.prevlvl();
    };
    /*
    if (inputcheck([NS_CONTROLER.HOME])) {
      navigator.getGamepads()[0].vibrate(1000,1);
    };
    */

    this.isJump = true;
    this.dy += grav;

    this.dx *= /*(this.isSmall) ? 0.4*fri : */((this.isRun) ? 1.4*fri : fri);

    /* (DECAPRIATED) Crouching
    if (this.isSmall) {
      this.width = this.sw;
      this.height = this.sh;
    } else {
      //this.y -= this.sh;
      this.width = this.fw;
      this.height = this.fh;
    }
    */

    this.wasSmall = false;

    this.collideObj(objlist);
  }
  collideWall(lv,ctx) {
    if (this.x < 0) {
      this.dx = 0;
      this.x = 0;
      this.isMoving = false;
    };
    if (this.x+this.width > lv.mw) {
      this.dx = 0;
      this.x = lv.mw-this.width;
      this.isMoving = false;
    };
    if (this.y < 0) {
      this.dy = 0;
      this.y = 0;
    };
    if (this.y-this.height > lv.mh) {
      this.die(lv);
    };
    if (this.forceJump) {
      this.isJump = false;
      this.forceJump = false;
    }
  }
  checkFlagColi(lvl) {
    return checkIfRectOverlap(this,lvl.getFlagObjs());
  }
  handleFlagEnd(lvl) {
    if (this.checkFlagColi(lvl)) {
      this.nextlvl();
    };
  }
  handleEntityColi(lvl) {
    for (var na in Entity.list) {
      if (checkIfRectOverlap(Entity.list[na],this)) {
        if (Entity.list[na].type == 'bad' && Entity.list[na].pl == this.level) {
          this.die(lvl);
        };
      };
    };
  }
  update(ctx,fri,grav,lvl) {
    this.move(fri,grav,lvl.getCollideObjs());
    this.djc--;
    this.hearthurtdtime -= 0.01;
    if (this.hearthurtdtime < 0) this.hearthurtdtime = 0;
    this.collideWall(lvl,ctx);
    this.draw(ctx);
    this.handleFlagEnd(lvl);
    this.handleEntityColi(lvl);
  }
};
