// So this is for moving your dude around and making him blast suckas.
function get_controls() {
  if (is_key_left) vectorx -= 3;
  else if (is_key_right) vectorx += 3;
  if (is_key_up) vectory -= 4;
  else if (is_key_down) vectory += 4;

  if (vectorx > 11) vectorx = 11;
  else if (vectorx < -11) vectorx = -11;
  if (vectory > 13) vectory = 13;
  else if (vectory < -13) vectory = -13;
  
  // Friction. Yeah, I know there's no friction in a vacuum. Fuck you, smartass.
  if (vectorx > 0) vectorx -= 1.5;
  else if (vectorx < 0) vectorx += 1.5;
  if (vectory > 0) vectory -= 1.5;
  else if (vectory < 0) vectory += 1.5;
  
  if (vectorx > -1 && vectorx < 1) vectorx = 0;
  if (vectory > -1 && vectory < 1) vectory = 0;

  if (is_firing == 1) {
    if (cycle_step % 8 == 0) {
      Bullets.push (new Bullet (shipx - 11, shipy - 16, 1));
      Bullets.push (new Bullet (shipx + 9, shipy - 16, 1));
      if (power1 > 0) {
        Bullets.push (new Bullet (shipx - 20, shipy, 3));
        Bullets.push (new Bullet (shipx + 18, shipy, 3));
      }
    }
    else if (cycle_step % 8 - 4 == 0) {
      Bullets.push (new Bullet (shipx + 9, shipy - 16, 1));
      Bullets.push (new Bullet (shipx - 11, shipy - 16, 1));
    }
    if (power1 == 2 && cycle_step % 16 == 0) {
      Bullets.push (new Bullet (shipx - 1, shipy - 26, 2, vectorx / 3));
      Bullets.push (new Bullet (shipx - 20, shipy - 5, 2, -2 + vectorx / 3));
      Bullets.push (new Bullet (shipx + 18, shipy - 5, 2, 2 + vectorx / 3));
    }
  }
  else if (is_firing == 2) {
    if (cycle_step % 8 == 0) {
      Bullets.push (new Bullet (shipx - 1, shipy - 26, 2, vectorx / 3));
      if (cycle_step % 16 == 0) {
        Bullets.push (new Bullet (shipx - 20, shipy - 5 , 2, -2 + vectorx / 3));
        Bullets.push (new Bullet (shipx + 18, shipy - 5, 2, 2 + vectorx / 3));
      }
      else {
        Bullets.push (new Bullet (shipx - 20, shipy - 5, 2, -1 + vectorx / 3));
        Bullets.push (new Bullet (shipx + 18, shipy - 5, 2, 1 + vectorx / 3));
      }
    }
    if (power2 > 0) {
      if (cycle_step % 16 == 4) Bullets.push (new Bullet (shipx - 1, shipy - 26, 2, -0.75 + vectorx / 3));
      else if (cycle_step % 16 == 12) Bullets.push (new Bullet (shipx - 1, shipy - 26, 2, 0.75 + vectorx / 3));
    }
    if (power2 == 2) {
      if (cycle_step % 16 == 0) Bullets.push (new Bullet (shipx - 11, shipy - 16, 1));
      else if (cycle_step % 16 == 8) Bullets.push (new Bullet (shipx + 9, shipy - 16, 1));
    }
  }
}

// See, ya press a key and JavaScript calls this event. Magic!
function get_key_down (evt) {
  if (life_state != "Dead") {
    if (evt.keyCode == 13 && current_wave == 5) {
      Enemies.splice (0, Enemies.length);
    }
    if (evt.keyCode == 37 || evt.keyCode == 65) is_key_left = 1;
    if (evt.keyCode == 39 || evt.keyCode == 68) is_key_right = 1;
    if (evt.keyCode == 38 || evt.keyCode == 87) is_key_up = 1;
    if (evt.keyCode == 40 || evt.keyCode == 83) is_key_down = 1;
    if (evt.keyCode == 17 || evt.keyCode == 90 || evt.keyCode == 96) is_firing = 1;
    if (evt.keyCode == 16 || evt.keyCode == 88 || evt.keyCode == 110) is_firing = 2;
  }
  else if (challenge_mode != 1 && lives < 1 && evt.keyCode == 13) restart_game (current_wave);
  if (evt.keyCode == 32) {
    if (is_paused == 1) is_paused = 0;
    else is_paused = 1;
  }
  if (current_wave == 5) {
    if (evt.keyCode == 67 && code_progress % 2 == 0) code_progress++;
    else code_progress = 0;
    if (code_progress == 5) {
      if (challenge_mode == 1) challenge_mode = 0;
      else {
        challenge_mode = 1;
        challenge_mode_unlocked = 1;
      }
      code_progress = 0;
    }
  }
}

// Holy shit, it even knows when you stop pressing a key. Spooky.
function get_key_up (evt) {
  if (evt.keyCode == 67 && code_progress > 0) code_progress++;
  else code_progress = 0;
  if (evt.keyCode == 37 || evt.keyCode == 65) is_key_left = 0;
  if (evt.keyCode == 39 || evt.keyCode == 68) is_key_right = 0;
  if (evt.keyCode == 38 || evt.keyCode == 87) is_key_up = 0;
  if (evt.keyCode == 40 || evt.keyCode == 83) is_key_down = 0;
  if ((evt.keyCode == 17 || evt.keyCode == 90 || evt.keyCode == 96) && is_firing != 2) is_firing = 0;
  if ((evt.keyCode == 16 || evt.keyCode == 88 || evt.keyCode == 110) && is_firing != 1) is_firing = 0;
}

