private void eventFinish() {
   eventTick++;
   if (eventTick == 1) {
     boxes.clear();
     for (int i = 0; i < 9; i++) {
       if (i % 2 == 0) boxes.add(new Rectangle(-128, i * 16, GamePanel.WIDTH, 16));
       else boxes.add(new Rectangle(128, i * 16, GamePanel.WIDTH, 16));
     }
     JukeBox.stop("music1");
     JukeBox.play("finish");
   }
   if (eventTick > 1) {
     for (int i = 0; i < boxes.size(); i++) {
       Rectangle r = boxes.get(i);
       if (i % 2 == 0) {
         if (r.x < 0) r.x += 4;
       } else {
         if (r.x > 0) r.x -= 4;
       }
     }
   }
   if (eventTick > 33) {
     if (!JukeBox.isPlaying("finish")) {
       Data.setTime(player.getTicks());
       gsm.setState(GameStateManager.GAMEOVER);
     }
   }
 }
  public void init() {

    // create lists
    diamonds = new ArrayList<Diamond>();
    sparkles = new ArrayList<Sparkle>();
    items = new ArrayList<Item>();

    // load map
    tileMap = new TileMap(16);
    tileMap.loadTiles("/Tilesets/testtileset.gif");
    tileMap.loadMap("/Maps/testmap.map");

    // create player
    player = new Player(tileMap);

    // fill lists
    populateDiamonds();
    populateItems();

    // initialize player
    player.setTilePosition(17, 17);
    player.setTotalDiamonds(diamonds.size());

    // set up camera position
    sectorSize = GamePanel.WIDTH;
    xsector = player.getx() / sectorSize;
    ysector = player.gety() / sectorSize;
    tileMap.setPositionImmediately(-xsector * sectorSize, -ysector * sectorSize);

    // load hud
    hud = new Hud(player, diamonds);

    // load music
    JukeBox.load("/Music/bgmusic.mp3", "music1");
    JukeBox.setVolume("music1", -10);
    JukeBox.loop("music1", 1000, 1000, JukeBox.getFrames("music1") - 1000);
    JukeBox.load("/Music/finish.mp3", "finish");
    JukeBox.setVolume("finish", -10);

    // load sfx
    JukeBox.load("/SFX/collect.wav", "collect");
    JukeBox.load("/SFX/mapmove.wav", "mapmove");
    JukeBox.load("/SFX/tilechange.wav", "tilechange");
    JukeBox.load("/SFX/splash.wav", "splash");

    // start event
    boxes = new ArrayList<Rectangle>();
    eventStart = true;
    eventStart();
  }
 // Keyboard input.
 // If Player has axe, dead trees in front
 // of the Player will be chopped down.
 public void setAction() {
   if (hasAxe) {
     if (currentAnimation == UP && tileMap.getIndex(rowTile - 1, colTile) == 21) {
       tileMap.setTile(rowTile - 1, colTile, 1);
       JukeBox.play("tilechange");
     }
     if (currentAnimation == DOWN && tileMap.getIndex(rowTile + 1, colTile) == 21) {
       tileMap.setTile(rowTile + 1, colTile, 1);
       JukeBox.play("tilechange");
     }
     if (currentAnimation == LEFT && tileMap.getIndex(rowTile, colTile - 1) == 21) {
       tileMap.setTile(rowTile, colTile - 1, 1);
       JukeBox.play("tilechange");
     }
     if (currentAnimation == RIGHT && tileMap.getIndex(rowTile, colTile + 1) == 21) {
       tileMap.setTile(rowTile, colTile + 1, 1);
       JukeBox.play("tilechange");
     }
   }
 }
 public void handleInput() {
   if (Keys.isPressed(Keys.ESCAPE)) {
     JukeBox.stop("music1");
     gsm.setPaused(true);
   }
   if (blockInput) return;
   if (Keys.isDown(Keys.LEFT)) player.setLeft();
   if (Keys.isDown(Keys.RIGHT)) player.setRight();
   if (Keys.isDown(Keys.UP)) player.setUp();
   if (Keys.isDown(Keys.DOWN)) player.setDown();
   if (Keys.isPressed(Keys.SPACE)) player.setAction();
 }
  public void update() {

    ticks++;

    // check if on water
    boolean current = onWater;
    if (tileMap.getIndex(ydest / tileSize, xdest / tileSize) == 4) {
      onWater = true;
    } else {
      onWater = false;
    }
    // if going from land to water
    if (!current && onWater) {
      JukeBox.play("splash");
    }

    // set animation
    if (down) {
      if (onWater && currentAnimation != DOWNBOAT) {
        setAnimation(DOWNBOAT, downBoatSprites, 10);
      } else if (!onWater && currentAnimation != DOWN) {
        setAnimation(DOWN, downSprites, 10);
      }
    }
    if (left) {
      if (onWater && currentAnimation != LEFTBOAT) {
        setAnimation(LEFTBOAT, leftBoatSprites, 10);
      } else if (!onWater && currentAnimation != LEFT) {
        setAnimation(LEFT, leftSprites, 10);
      }
    }
    if (right) {
      if (onWater && currentAnimation != RIGHTBOAT) {
        setAnimation(RIGHTBOAT, rightBoatSprites, 10);
      } else if (!onWater && currentAnimation != RIGHT) {
        setAnimation(RIGHT, rightSprites, 10);
      }
    }
    if (up) {
      if (onWater && currentAnimation != UPBOAT) {
        setAnimation(UPBOAT, upBoatSprites, 10);
      } else if (!onWater && currentAnimation != UP) {
        setAnimation(UP, upSprites, 10);
      }
    }

    // update position
    super.update();
  }
  public void update() {

    // check keys
    handleInput();

    // check events
    if (eventStart) eventStart();
    if (eventFinish) eventFinish();

    if (player.numDiamonds() == player.getTotalDiamonds()) {
      eventFinish = blockInput = true;
    }

    // update camera
    int oldxs = xsector;
    int oldys = ysector;
    xsector = player.getx() / sectorSize;
    ysector = player.gety() / sectorSize;
    tileMap.setPosition(-xsector * sectorSize, -ysector * sectorSize);
    tileMap.update();

    if (oldxs != xsector || oldys != ysector) {
      JukeBox.play("mapmove");
    }

    if (tileMap.isMoving()) return;

    // update player
    player.update();

    // update diamonds
    for (int i = 0; i < diamonds.size(); i++) {

      Diamond d = diamonds.get(i);
      d.update();

      // player collects diamond
      if (player.intersects(d)) {

        // remove from list
        diamonds.remove(i);
        i--;

        // increment amount of collected diamonds
        player.collectedDiamond();

        // play collect sound
        JukeBox.play("collect");

        // add new sparkle
        Sparkle s = new Sparkle(tileMap);
        s.setPosition(d.getx(), d.gety());
        sparkles.add(s);

        // make any changes to tile map
        ArrayList<int[]> ali = d.getChanges();
        for (int[] j : ali) {
          tileMap.setTile(j[0], j[1], j[2]);
        }
        if (ali.size() != 0) {
          JukeBox.play("tilechange");
        }
      }
    }

    // update sparkles
    for (int i = 0; i < sparkles.size(); i++) {
      Sparkle s = sparkles.get(i);
      s.update();
      if (s.shouldRemove()) {
        sparkles.remove(i);
        i--;
      }
    }

    // update items
    for (int i = 0; i < items.size(); i++) {
      Item item = items.get(i);
      if (player.intersects(item)) {
        items.remove(i);
        i--;
        item.collected(player);
        JukeBox.play("collect");
        Sparkle s = new Sparkle(tileMap);
        s.setPosition(item.getx(), item.gety());
        sparkles.add(s);
      }
    }
  }