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(); }
public void draw(Graphics2D g) { // draw tilemap tileMap.draw(g); // draw player player.draw(g); // draw diamonds for (Diamond d : diamonds) { d.draw(g); } // draw sparkles for (Sparkle s : sparkles) { s.draw(g); } // draw items for (Item i : items) { i.draw(g); } // draw hud hud.draw(g); // draw transition boxes g.setColor(java.awt.Color.BLACK); for (int i = 0; i < boxes.size(); i++) { g.fill(boxes.get(i)); } }
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); } } }