<commit_before>var GameState = function(game){};

GameState.prototype.preload = function() {
};

GameState.prototype.create = function() {
  this.game.stage.backgroundColor = 0xBF844F;

  this.game.physics.startSystem(Phaser.Physics.ARCADE);

  this.sounds = {
    //tada: this.game.add.audio("tada")
  };

  this.groups = {
    bg: this.game.add.group(),
    enemies: this.game.add.group(),
    players: this.game.add.group(),
    enemyBullets: this.game.add.group(),
    playerBullets: this.game.add.group(),
    enemyHits: this.game.add.group(),
    playerHits: this.game.add.group(),
    lasso: this.game.add.group(),
    dialogs: this.game.add.group(),
    spawnerIcons: this.game.add.group(),
    lifeCounters: this.game.add.group()
  };

  // Life counters
  for (var i = 2; i >= 0; i--) {
    this.groups.lifeCounters.add(
      this.game.make.sprite(
        10 + 16*i, SCREEN_HEIGHT - 10 - 16, 'health'
      )
    );
  }

  // Spawn counters
  this.spawner = new Spawner(
    this.game, this.groups.spawnerIcons
  );

  this.spawnPlayer();

  // Add some enemies
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    80, 60, EnemyTypes.outlaw, true);
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    90, 50, EnemyTypes.outlaw, true);
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    200, 90, EnemyTypes.outlaw, true);

  this.dialog = new Dialog(this.game, this.groups.dialogs,0, 0);
  // Hide dialog initially
  this.groups.dialogs.alpha = 0;

  this.keys = this.game.input.keyboard.addKeys({
    up: Phaser.Keyboard.UP,
    down: Phaser.Keyboard.DOWN,
    left: Phaser.Keyboard.LEFT,
    right: Phaser.Keyboard.RIGHT,
    fire: Phaser.Keyboard.Z,
    lasso: Phaser.Keyboard.X,
    spawn1: Phaser.Keyboard.ONE,
    spawn2: Phaser.Keyboard.TWO,
    spawn3: Phaser.Keyboard.THREE
  });
  this.keys.spawn1.onDown.add(function() {
    if (this.spawner.trySpawn('outlaw')) {
      this.spawnAlly('outlaw');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('cowboy')) {
      this.spawnAlly('cowboy');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('bandito')) {
      this.spawnAlly('bandito');
    }
  }, this);
};

GameState.prototype.spawnPlayer = function() {
  if (this.groups.lifeCounters.total === 0) {
    console.log('GAME OVER!');
    // TODO: game over screen, reset to title
  } else {
    this.player = new Player(
      this.game,
      this.groups.players, this.groups.playerBullets,
      this.groups.playerHits, this.groups.lasso,
      SCREEN_WIDTH / 2, SCREEN_HEIGHT - 32, []);
    this.playerRespawnCounter = 2000;
    this.groups.lifeCounters.getFirstExists().destroy();
  }
};

GameState.prototype.spawnAlly = function(key) {
  // Spawn ally near where player is
  var x = Phaser.Math.clamp(
    (Math.random() - 0.5) * 100 + this.player.x,
    0, SCREEN_WIDTH);
  new Enemy(
    this.game,
    this.groups.players, this.groups.playerBullets,
    this.groups.playerHits,
    this.groups.enemies,
    x, SCREEN_HEIGHT, EnemyTypes[key], false);
}

GameState.prototype.update = function() {
  // Move using arrow keys
  var dx = 0;
  var dy = 0;
  if (this.keys.left.isDown) {
    dx = -1;
  } else if (this.keys.right.isDown) {
    dx = 1;
  }
  if (this.keys.up.isDown) {
    dy = -1;
  } else if (this.keys.down.isDown) {
    dy = 1;<commit_msg>Refactor spawn enemy<commit_after>var GameState = function(game){};

GameState.prototype.preload = function() {
};

GameState.prototype.create = function() {
  this.game.stage.backgroundColor = 0xBF844F;

  this.game.physics.startSystem(Phaser.Physics.ARCADE);

  this.sounds = {
    //tada: this.game.add.audio("tada")
  };

  this.groups = {
    bg: this.game.add.group(),
    enemies: this.game.add.group(),
    players: this.game.add.group(),
    enemyBullets: this.game.add.group(),
    playerBullets: this.game.add.group(),
    enemyHits: this.game.add.group(),
    playerHits: this.game.add.group(),
    lasso: this.game.add.group(),
    dialogs: this.game.add.group(),
    spawnerIcons: this.game.add.group(),
    lifeCounters: this.game.add.group()
  };

  // Life counters
  for (var i = 2; i >= 0; i--) {
    this.groups.lifeCounters.add(
      this.game.make.sprite(
        10 + 16*i, SCREEN_HEIGHT - 10 - 16, 'health'
      )
    );
  }

  // Spawn counters
  this.spawner = new Spawner(
    this.game, this.groups.spawnerIcons
  );

  this.spawnPlayer();

  // Add some enemies
  this.spawnEnemy('outlaw');
  this.spawnEnemy('outlaw');
  this.spawnEnemy('outlaw');
  this.spawnEnemy('outlaw');
  this.spawnEnemy('outlaw');

  this.dialog = new Dialog(this.game, this.groups.dialogs,0, 0);
  // Hide dialog initially
  this.groups.dialogs.alpha = 0;

  this.keys = this.game.input.keyboard.addKeys({
    up: Phaser.Keyboard.UP,
    down: Phaser.Keyboard.DOWN,
    left: Phaser.Keyboard.LEFT,
    right: Phaser.Keyboard.RIGHT,
    fire: Phaser.Keyboard.Z,
    lasso: Phaser.Keyboard.X,
    spawn1: Phaser.Keyboard.ONE,
    spawn2: Phaser.Keyboard.TWO,
    spawn3: Phaser.Keyboard.THREE
  });
  this.keys.spawn1.onDown.add(function() {
    if (this.spawner.trySpawn('outlaw')) {
      this.spawnAlly('outlaw');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('cowboy')) {
      this.spawnAlly('cowboy');
    }
  }, this);
  this.keys.spawn2.onDown.add(function() {
    if (this.spawner.trySpawn('bandito')) {
      this.spawnAlly('bandito');
    }
  }, this);
};

GameState.prototype.spawnPlayer = function() {
  if (this.groups.lifeCounters.total === 0) {
    console.log('GAME OVER!');
    // TODO: game over screen, reset to title
  } else {
    this.player = new Player(
      this.game,
      this.groups.players, this.groups.playerBullets,
      this.groups.playerHits, this.groups.lasso,
      SCREEN_WIDTH / 2, SCREEN_HEIGHT - 32, []);
    this.playerRespawnCounter = 2000;
    this.groups.lifeCounters.getFirstExists().destroy();
  }
};

GameState.prototype.spawnAlly = function(key) {
  // Spawn ally near where player is
  var x = Phaser.Math.clamp(
    (Math.random() - 0.5) * 100 + this.player.x,
    0, SCREEN_WIDTH);
  new Enemy(
    this.game,
    this.groups.players, this.groups.playerBullets,
    this.groups.playerHits,
    this.groups.enemies,
    x, SCREEN_HEIGHT, EnemyTypes[key], false);
};

GameState.prototype.spawnEnemy = function(key) {
  // Spawn enemy along top
  new Enemy(
    this.game,
    this.groups.enemies, this.groups.enemyBullets,
    this.groups.enemyHits,
    this.groups.players,
    Math.random() * SCREEN_WIDTH, 0,
    EnemyTypes[key], true);
};

GameState.prototype.update = function() {
  // Move using arrow keys
  var dx = 0;
  var dy = 0;
  if (this.keys.left.isDown) {
    dx = -1;
  } else if (this.keys.right.isDown) {
    dx = 1;
  }
  if (this.keys.up.isDown) {
    dy = -1;
  } else if (this.keys.down.isDown) {
    dy = 1;
{'fraction_non_alphanumeric': 0.1575486217993564, 'fraction_numerical': 0.013991884706870015, 'mean_word_length': 2.9060109289617486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4463037', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2717, 'n_words': 451}