/** spawns a pickup for every enemy marked as "should spawn a pickup" */
 private void spawnPickups() {
   for (IEnemy enemy : enemyManager.getEnemies()) {
     if (enemy.shouldSpawnPickup()) {
       pickupManager.addPickup(
           new WeaponPickup(enemy.getShip().getPositionClone(), enemy.getWeapon().getName()));
     }
   }
 }
 /**
  * Resets the game
  *
  * @param difficulty Which difficulty it should be reset to.
  */
 public void reset(Difficulty difficulty) {
   bulletManager.clearAllBullets();
   enemyManager.clearAllEnemies();
   pickupManager.clearAllPickups();
   enemySpawner = new EnemySpawner(new RandomWaveList(difficulty));
   enemySpawner.addPropertyChangeListener(enemyManager);
   player.reset();
   player.giveWeapon(new BasicWeapon(bulletManager));
 }
  /**
   * Run an update on the model.
   *
   * @param input Input variables to the model.
   * @param timeElapsed Time in seconds since last update.
   */
  public void update(final ModelInput input, final float timeElapsed) {
    player.update(input, timeElapsed);
    for (AbstractBullet bullet : bulletManager.getBullets()) {
      bullet.doMovement(timeElapsed);
    }
    for (AbstractEnemy enemy : enemyManager.getEnemies()) {
      enemy.update(timeElapsed);
    }
    for (WeaponPickup pickup : pickupManager.getPickups()) {
      pickup.doMovement(timeElapsed);
    }

    enemySpawner.update(timeElapsed);

    checkCollisions();

    spawnPickups();
    enemyManager.clearDeadEnemies();
    bulletManager.clearBulletsOffScreen();
    pickupManager.clearPickupsOffScreen();
  }
  /** @param difficulty Which difficulty level the game model should be on. */
  public GameModel(Difficulty difficulty) {
    bulletManager = new BulletManager();
    enemyManager = new EnemyManager();
    pickupManager = new PickupManager();
    WeaponFactory.initialize(bulletManager);
    enemySpawner = new EnemySpawner(new RandomWaveList(difficulty));
    enemySpawner.addPropertyChangeListener(enemyManager);
    player = new Player(PlayerID.PLAYER1, bulletManager);
    pcs = new PropertyChangeSupport(this);

    // Player listens when enemies are killed
    enemyManager.addPropertyChangeListener(player);
  }
  private void checkCollisions() {
    final List<IBullet> playerBullets = bulletManager.getBulletsFrom(PlayerID.PLAYER1);
    final List<IBullet> enemyBullets = bulletManager.getBulletsFrom(PlayerID.ENEMY);

    // Check player bullets against enemies
    for (IBullet b : playerBullets) {
      for (IEnemy e : enemyManager.getEnemies()) {
        if (b.collidesWith(e.getShip())) {
          e.getShip().receiveDamage(b.getDamage());
          pcs.firePropertyChange(Constants.EVENT_ENEMY_DAMAGED, null, e.getShip());
          b.markForRemoval();
        }
      }
    }

    // Check enemy bullets against player
    if (!player.isInvincible()) {
      for (IBullet b : enemyBullets) {
        if (b.collidesWith(player.getShip())) {
          player.getShip().receiveDamage(b.getDamage());
          pcs.firePropertyChange(Constants.EVENT_ENTITY_INVINCIBLE, null, player);
          b.markForRemoval();
          break;
        }
      }
    }

    // Check Items against player
    for (int i = 0; i < pickupManager.getPickups().size(); i++) {
      WeaponPickup wp = pickupManager.getPickups().get(i);
      if (wp.collidesWith(player.getShip())) {
        player.giveWeapon(WeaponFactory.getNewWeapon(wp.getObjectName()));
        pickupManager.removePickup(i);
        i--;
      }
    }
  }