<commit_before>
var Character = function(game, x, y, key){
    Phaser.Sprite.call(this, game, x, y, key);
};
Character.prototype = Object.create(Phaser.Sprite.prototype);
Character.prototype.constructor = Character;
Character.prototype.update = function(){
    // automatically called by World.update
    this.x += this.data.velocity.x * this.game.time.physicsElapsed;
};

var Player = function(game, x, y, key){
    Character.call(this, game, x, y, key);
    this.health = this.maxHealth;
    this.data.velocity = {x: 300, y: 0};
    this._anim = this.animations.add('walk');
    this._anim.play(10, true);
    this.anchor.setTo(0.5, 1);
    this.flipped = false;
    //the camera will follow the player in the world
    this.game.camera.follow(this);
};
Player.prototype = Object.create(Character.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function(){
    Character.prototype.update.call(this);
};

var Monster = function(game, x, y, key){
    Character.call(this, game, x, y, key);
};
Monster.prototype = Object.create(Character.prototype);
Monster.prototype.constructor = Monster;
Monster.prototype.update = function(){
    Character.prototype.update.call(this);
};


var SideScroller = SideScroller || {};
SideScroller.Game = function(){};
SideScroller.Game.prototype = {
  preload: function() {
      this.game.time.advancedTiming = true;
    },
  create: function() {
    this.map = this.game.add.tilemap('level1');
    //the first parameter is the tileset name as specified in Tiled, the second is the key to the asset
    this.map.addTilesetImage('dungeon', 'gameTiles');
    //create layers
    this.backgroundLayer1 = this.map.createLayer('Background 1');
    this.backgroundLayer2 = this.map.createLayer('Background 2');
    this.blockedLayer = this.map.createLayer('Blocking');
    //collision on blockedLayer
    // this.map.setCollisionBetween(1, 100000, true, 'Blocking');
    //resizes the game world to match the layer dimensions
    this.backgroundLayer1.resizeWorld();
    //create player
    this.player = this.game.add.existing(new Player(this.game, 35, 350, 'player'));
    //move player with cursor keys
    this.cursors = this.game.input.keyboard.createCursorKeys();
    // set up torch sprite and animation
    // load torches
    this.createTorches();
    this.initWanderingMonsters();
    this.monster = null;
    // setup health and text
    this.initText();
  },
  update: function() {
    // collision
    // this.game.physics.arcade.collide(this.player, this.blockedLayer);
    if (this.player.alive){
        if (this.cursors.right.isDown){
            if (this.player.flipped){
                this.player.flipped = false;
                this.player.scale.x = 1;
            }
            this.player.data.velocity.x = 300;
        }else if (this.cursors.left.isDown){
            if (!this.player.flipped){
                this.player.flipped = true;
                this.player.scale.x = -1;
            }
            this.player.data.velocity.x = -300;
        }else{
            this.player.data.velocity.x = 0;
            if(this.cursors.up.isDown) {
                this.playerLook();
            }
        }
        if (!this.monster && this.underTorch()){
            this.addWanderingMonster();
        }
        if (this.monster){
            this.monsterMove();
            this.monsterAttack();
        }
    }
    //restart the game if reaching the edge
    if(this.player.x >= this.game.world.width || this.player.x < 0) {
        this.game.state.start('Game');
    }
    // show health bars
    this.showHealth();
  },
  monsterMove: function(){
    //   this.game.physics.arcade.collide(this.monster, this.blockedLayer);
      var fuzz = 20; // we don't need this to be too specific
      var distance = Math.abs(this.player.x - this.monster.x) - (this.player.width + this.monster.width) / 2;
      if (distance < this.monster.data.range - fuzz){
          this._monsterMoveAway();
      }else if(distance > this.monster.data.range + fuzz){
          this._monsterMoveCloser();
      }else{
          this.monster.data.velocity.x = 0;
      }
  },
  _monsterMoveAway: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.data.velocity.x = this.monster.speed || 350;
      }else{
          this.monster.data.velocity.x = -(this.monster.speed || 350);
      }
  },
  _monsterMoveCloser: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.data.velocity.x = -(this.monster.speed || 350);
      }else{
          this.monster.data.velocity.x = this.monster.speed || 350;
      }
  },
  monsterAttack: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.scale.x = 1;
      }else{
          this.monster.scale.x = -1;
      }
      if (this.monster.data.isCoolingDown){
          this.monster.data.isCoolingDown -= 1;
          return;
      }
      var fuzz = 20; // we don't need this to be too specific
      var distance = Math.abs(this.player.x - this.monster.x) - (this.player.width + this.monster.width) / 2;
      if ((distance + fuzz) > this.monster.data.range && (distance - fuzz) < this.monster.data.range){
          var attack_roll = this.rnd.between(0, 99);
          if (attack_roll < this.monster.data.hitson){
              this.player.damage(this.monster.data.doesDamage);
              this.monster.data.isCoolingDown = this.monster.data.cooldown;
              this.damageText.text = 'The ' + this.monster.name +
                ' ' + this.monster.data.attackText + '.';
          }else{
              this.damageText.text = 'The ' + this.monster.name +
                ' swung at you, but missed.'
          }
      }
  },
  render: function(){
        this.game.debug.text(this.game.time.fps || '--', 10, 20, "#00ff00", "14px Courier");
    },
  playerHit: function(player, blockedLayer) {

  },
  playerLook: function() {
      // Open doors, examine wall
  },
  playerSweepBounds: function(){
      var b = this.player.getBounds();
      return new Phaser.Rectangle(b.x, 0, b.width, this.game.world.height);
  },
  underTorch: function(){
      var bounds = this.playerSweepBounds();
      for (var i = 0; i < this.torches.length; i++){<commit_msg>refactored keyboard handling to allow for more actions<commit_after>
var Character = function(game, x, y, key){
    Phaser.Sprite.call(this, game, x, y, key);
};
Character.prototype = Object.create(Phaser.Sprite.prototype);
Character.prototype.constructor = Character;
Character.prototype.update = function(){
    // automatically called by World.update
    this.x += this.data.velocity.x * this.game.time.physicsElapsed;
};

var Player = function(game, x, y, key){
    Character.call(this, game, x, y, key);
    this.health = this.maxHealth;
    this.data.velocity = {x: 300, y: 0};
    this._anim = this.animations.add('walk');
    this._anim.play(10, true);
    this.anchor.setTo(0.5, 1);
    //the camera will follow the player in the world
    this.game.camera.follow(this);
};
Player.prototype = Object.create(Character.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function(){
    Character.prototype.update.call(this);
};
Player.prototype.look = function(){
    // Open doors, examine wall
};

var Monster = function(game, x, y, key){
    Character.call(this, game, x, y, key);
};
Monster.prototype = Object.create(Character.prototype);
Monster.prototype.constructor = Monster;
Monster.prototype.update = function(){
    Character.prototype.update.call(this);
};


var SideScroller = SideScroller || {};
SideScroller.Game = function(){};
SideScroller.Game.prototype = {
  preload: function() {
      this.game.time.advancedTiming = true;
    },
  create: function() {
    this.map = this.game.add.tilemap('level1');
    //the first parameter is the tileset name as specified in Tiled, the second is the key to the asset
    this.map.addTilesetImage('dungeon', 'gameTiles');
    //create layers
    this.backgroundLayer1 = this.map.createLayer('Background 1');
    this.backgroundLayer2 = this.map.createLayer('Background 2');
    this.blockedLayer = this.map.createLayer('Blocking');
    //collision on blockedLayer
    // this.map.setCollisionBetween(1, 100000, true, 'Blocking');
    //resizes the game world to match the layer dimensions
    this.backgroundLayer1.resizeWorld();
    //create player
    this.player = this.game.add.existing(new Player(this.game, 35, 350, 'player'));
    //move player with cursor keys
    this.setupKeys();
    // set up torch sprite and animation
    // load torches
    this.createTorches();
    this.initWanderingMonsters();
    this.monster = null;
    // setup health and text
    this.initText();
  },
  setupKeys: function(){
      var player = this.player;
      this.game.input.keyboard.enabled = true;
      this.keys = this.game.input.keyboard.addKeys({
          a: Phaser.KeyCode.A,
          b: Phaser.KeyCode.B, // insta-bbq
          c: Phaser.KeyCode.C, // curse
          k: Phaser.KeyCode.K, // kick
          n: Phaser.KeyCode.N, // nap
          s: Phaser.KeyCode.S, // sweep
          t: Phaser.KeyCode.T, // ticket (for bus)
          up: Phaser.KeyCode.UP, // up arrow
          down: Phaser.KeyCode.DOWN, // down arrow
          right: Phaser.KeyCode.RIGHT, // right arrow
          left: Phaser.KeyCode.LEFT, // left arrow
          space: Phaser.KeyCode.SPACEBAR, // repeat last action, or kick
      });
      this.keys.right.action = function(){
          player.scale.x = 1;
          player.data.velocity.x = 300;
      };
      this.keys.left.action = function(){
          player.scale.x = -1;
          player.data.velocity.x = -300;
      };
      this.keys.up.action = function(){
          player.look();
      };
  },
  handleKeys: function(){
      var lastKey = this.game.input.keyboard.lastKey;
      if (lastKey && lastKey.isDown && lastKey.action){
          lastKey.action();
      }
  },
  update: function() {
    // collision
    // this.game.physics.arcade.collide(this.player, this.blockedLayer);
    if (this.player.alive){
        this.player.data.velocity.x = 0;
        this.handleKeys();
        if (!this.monster && this.underTorch()){
            this.addWanderingMonster();
        }
        if (this.monster){
            this.monsterMove();
            this.monsterAttack();
        }
    }
    //restart the game if reaching the edge
    if(this.player.x >= this.game.world.width || this.player.x < 0) {
        this.game.state.start('Game');
    }
    // show health bars
    this.showHealth();
  },
  monsterMove: function(){
    //   this.game.physics.arcade.collide(this.monster, this.blockedLayer);
      var fuzz = 20; // we don't need this to be too specific
      var distance = Math.abs(this.player.x - this.monster.x) - (this.player.width + this.monster.width) / 2;
      if (distance < this.monster.data.range - fuzz){
          this._monsterMoveAway();
      }else if(distance > this.monster.data.range + fuzz){
          this._monsterMoveCloser();
      }else{
          this.monster.data.velocity.x = 0;
      }
  },
  _monsterMoveAway: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.data.velocity.x = this.monster.speed || 350;
      }else{
          this.monster.data.velocity.x = -(this.monster.speed || 350);
      }
  },
  _monsterMoveCloser: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.data.velocity.x = -(this.monster.speed || 350);
      }else{
          this.monster.data.velocity.x = this.monster.speed || 350;
      }
  },
  monsterAttack: function(){
      var playerToTheLeft = this.player.x < this.monster.x;
      if (playerToTheLeft){
          this.monster.scale.x = 1;
      }else{
          this.monster.scale.x = -1;
      }
      if (this.monster.data.isCoolingDown){
          this.monster.data.isCoolingDown -= 1;
          return;
      }
      var fuzz = 20; // we don't need this to be too specific
      var distance = Math.abs(this.player.x - this.monster.x) - (this.player.width + this.monster.width) / 2;
      if ((distance + fuzz) > this.monster.data.range && (distance - fuzz) < this.monster.data.range){
          var attack_roll = this.rnd.between(0, 99);
          if (attack_roll < this.monster.data.hitson){
              this.player.damage(this.monster.data.doesDamage);
              this.monster.data.isCoolingDown = this.monster.data.cooldown;
              this.damageText.text = 'The ' + this.monster.name +
                ' ' + this.monster.data.attackText + '.';
          }else{
              this.damageText.text = 'The ' + this.monster.name +
                ' swung at you, but missed.'
          }
      }
  },
  render: function(){
        this.game.debug.text(this.game.time.fps || '--', 10, 20, "#00ff00", "14px Courier");
    },
  playerHit: function(player, blockedLayer) {

  },
  playerSweepBounds: function(){
      var b = this.player.getBounds();
      return new Phaser.Rectangle(b.x, 0, b.width, this.game.world.height);
  },
  underTorch: function(){
      var bounds = this.playerSweepBounds();
      for (var i = 0; i < this.torches.length; i++){
{'fraction_non_alphanumeric': 0.1437219730941704, 'fraction_numerical': 0.011061285500747383, 'mean_word_length': 2.398780797561595, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3649267', 'n_tokens_mistral': 4426, 'n_tokens_neox': 4253, 'n_words': 875}