diff --git a/.idea/libraries/org_junit_jupiter_junit_jupiter_5_5_2.xml b/.idea/libraries/org_junit_jupiter_junit_jupiter_5_5_2.xml deleted file mode 100644 index 7204ced..0000000 --- a/.idea/libraries/org_junit_jupiter_junit_jupiter_5_5_2.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Azaraka.iml b/Azaraka.iml index 4d0925a..e8c39a6 100644 --- a/Azaraka.iml +++ b/Azaraka.iml @@ -17,6 +17,5 @@ - \ No newline at end of file diff --git a/res/scene.json b/res/scene.json index 365cc48..549435b 100644 --- a/res/scene.json +++ b/res/scene.json @@ -149,7 +149,7 @@ ] }, { - "type": "cl.cromer.azaraka.object.Player", + "type": "null", "textures": [ 0 ] diff --git a/src/cl/cromer/azaraka/Constantes.java b/src/cl/cromer/azaraka/Constantes.java index 6a169dd..f3aa785 100644 --- a/src/cl/cromer/azaraka/Constantes.java +++ b/src/cl/cromer/azaraka/Constantes.java @@ -126,7 +126,7 @@ public interface Constantes { /** * The default volume */ - int DEFAULT_VOLUME = 0; + int DEFAULT_VOLUME = 100; /** * Generates the scene manually instead of from the JSON file if true */ diff --git a/src/cl/cromer/azaraka/Escenario.java b/src/cl/cromer/azaraka/Escenario.java index 1996373..91d7989 100644 --- a/src/cl/cromer/azaraka/Escenario.java +++ b/src/cl/cromer/azaraka/Escenario.java @@ -31,6 +31,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; /** @@ -53,26 +55,6 @@ public class Escenario extends JComponent implements Constantes { * The cells of the game */ private Celda[][] celdas; - /** - * The cell that contains the player - */ - private Celda player; - /** - * The magic portal - */ - private Celda portal; - /** - * The enemies - */ - private ArrayList enemies = new ArrayList<>(); - /** - * The chests - */ - private ArrayList chests = new ArrayList<>(); - /** - * The keys - */ - private ArrayList keys = new ArrayList<>(); /** * A collection of tiles that can be used in the scene */ @@ -122,8 +104,6 @@ public class Escenario extends JComponent implements Constantes { Json json = new Json(); json.exportScene(celdas); } - - generateRandomObjects(); } /** @@ -142,7 +122,6 @@ public class Escenario extends JComponent implements Constantes { if (cells[x][y].type.equals(Player.class.getName())) { celdas[x][y].setObject(new Player(null, celdas[x][y])); - player = celdas[x][y]; } else if (cells[x][y].type.equals(Enemy.class.getName())) { celdas[x][y].setObject(new Enemy(null, celdas[x][y], null)); @@ -177,91 +156,83 @@ public class Escenario extends JComponent implements Constantes { /** * Generate random objects in the scene + * + * @return Returns a list of objects that where generated */ - private void generateRandomObjects() { + public ArrayList generateRandomObjects() { final int cells = (HORIZONTAL_CELLS * VERTICAL_CELLS); final int obstacles = (int) Math.floor((double) cells * 0.05); - int random_x; - int random_y; - ArrayList arrayList = new ArrayList<>(); + int[] random; + ArrayList objectArrayList = new ArrayList<>(); + + // The player has a fixed position + celdas[2][1].setObject(new Player(this, celdas[2][1])); + objectArrayList.add(celdas[2][1].getObject()); + + final Lock lock = new ReentrantLock(true); + + for (int i = 0; i < obstacles; i++) { + random = randomCoordinates(); + celdas[random[0]][random[1]].setObject(new Obstacle(this, celdas[random[0]][random[1]])); + try { + celdas[random[0]][random[1]].addTexture(textureSheet.getTexture(30), 30); + } + catch (SheetException e) { + logger.warning(e.getMessage()); + } + } for (int i = 0; i < ENEMIES; i++) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - while (arrayList.contains(new RandomPositionList(random_x, random_y, new Enemy(null, celdas[random_x][random_y], null))) || celdas[random_x][random_y].getObject() != null) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - } - arrayList.add(new RandomPositionList(random_x, random_y, new Enemy(null, celdas[random_x][random_y], null))); - } - for (int i = 0; i < obstacles; i++) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - while (arrayList.contains(new RandomPositionList(random_x, random_y, new Obstacle(null, celdas[random_x][random_y]))) || celdas[random_x][random_y].getObject() != null) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - } - arrayList.add(new RandomPositionList(random_x, random_y, new Obstacle(null, celdas[random_x][random_y]))); + random = randomCoordinates(); + celdas[random[0]][random[1]].setObject(new Enemy(this, celdas[random[0]][random[1]], lock)); + objectArrayList.add(celdas[random[0]][random[1]].getObject()); } - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - while (arrayList.contains(new RandomPositionList(random_x, random_y, new Portal(null, celdas[random_x][random_y]))) || celdas[random_x][random_y].getObject() != null) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - } - arrayList.add(new RandomPositionList(random_x, random_y, new Portal(null, celdas[random_x][random_y]))); + random = randomCoordinates(); + celdas[random[0]][random[1]].setObject(new Portal(this, celdas[random[0]][random[1]])); + objectArrayList.add(celdas[random[0]][random[1]].getObject()); // Generate enough keys for the chests that will exist for (int i = 0; i < CHESTS; i++) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - while (arrayList.contains(new RandomPositionList(random_x, random_y, new Key(null, celdas[random_x][random_y]))) || celdas[random_x][random_y].getObject() != null) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); - } - arrayList.add(new RandomPositionList(random_x, random_y, new Key(null, celdas[random_x][random_y]))); + random = randomCoordinates(); + celdas[random[0]][random[1]].setObject(new Key(this, celdas[random[0]][random[1]])); + objectArrayList.add(celdas[random[0]][random[1]].getObject()); } // Chests need to be last to make sure they are openable for (int i = 0; i < CHESTS; i++) { - random_x = random(0, HORIZONTAL_CELLS - 1); - random_y = random(0, VERTICAL_CELLS - 1); + int random_x = random(0, HORIZONTAL_CELLS - 1); + int random_y = random(0, VERTICAL_CELLS - 1); // Don't put a chest if it can't be opened - while (arrayList.contains(new RandomPositionList(random_x, random_y, new Chest(null, celdas[random_x][random_y]))) || arrayList.contains(new RandomPositionList(random_x, random_y + 1, new Chest(null, celdas[random_x][random_y]))) || celdas[random_x][random_y].getObject() != null || celdas[random_x][random_y + 1].getObject() != null || celdas[random_x][random_y - 1].getObject() != null) { + while (random_y + 1 == VERTICAL_CELLS || + celdas[random_x][random_y].getObject() != null || + celdas[random_x][random_y + 1].getObject() != null || + celdas[random_x][random_y - 1].getObject() != null) { random_x = random(0, HORIZONTAL_CELLS - 1); random_y = random(0, VERTICAL_CELLS - 1); } - arrayList.add(new RandomPositionList(random_x, random_y, new Chest(null, celdas[random_x][random_y]))); + celdas[random_x][random_y].setObject(new Chest(this, celdas[random_x][random_y])); + objectArrayList.add(celdas[random_x][random_y].getObject()); } - for (RandomPositionList randomList : arrayList) { - int x = randomList.getX(); - int y = randomList.getY(); - Object object = randomList.getObject(); - celdas[x][y].setObject(object); - if (object instanceof Enemy) { - enemies.add(celdas[x][y]); - } - else if (object instanceof Chest) { - chests.add(celdas[x][y]); - } - else if (object instanceof Key) { - keys.add(celdas[x][y]); - } - else if (object instanceof Portal) { - portal = celdas[x][y]; - } - else if (object instanceof Obstacle) { - try { - celdas[x][y].addTexture(textureSheet.getTexture(30), 30); - } - catch (SheetException e) { - logger.warning(e.getMessage()); - } - } + return objectArrayList; + } + + /** + * Get random x and y coordinates + * + * @return Returns an array with the coordinates + */ + private int[] randomCoordinates() { + int[] random = new int[2]; + random[0] = random(0, HORIZONTAL_CELLS - 1); + random[1] = random(0, VERTICAL_CELLS - 1); + while (celdas[random[0]][random[1]].getObject() != null) { + random[0] = random(0, HORIZONTAL_CELLS - 1); + random[1] = random(0, VERTICAL_CELLS - 1); } + return random; } /** @@ -281,7 +252,7 @@ public class Escenario extends JComponent implements Constantes { if (x == 0 && y == 0) { // Top left corner - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); try { celdas[x][y].addTexture(textureSheet.getTexture(33), 33); } @@ -291,7 +262,7 @@ public class Escenario extends JComponent implements Constantes { } else if (x == HORIZONTAL_CELLS - 1 && y == 0) { // Top right corner - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); try { celdas[x][y].addTexture(textureSheet.getTexture(37), 37); } @@ -301,7 +272,7 @@ public class Escenario extends JComponent implements Constantes { } else if (x == 0 && y == VERTICAL_CELLS - 1) { // Bottom left corner - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); try { celdas[x][y].addTexture(textureSheet.getTexture(97), 97); } @@ -311,7 +282,7 @@ public class Escenario extends JComponent implements Constantes { } else if (x == HORIZONTAL_CELLS - 1 && y == VERTICAL_CELLS - 1) { // Bottom right corner - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); try { celdas[x][y].addTexture(textureSheet.getTexture(101), 101); } @@ -321,7 +292,7 @@ public class Escenario extends JComponent implements Constantes { } else if (y == 0) { // Top wall - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); if (x == 1) { // Left door frame try { @@ -380,7 +351,7 @@ public class Escenario extends JComponent implements Constantes { } else if (x == 0) { // Left wall - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); if (y % 2 == 0) { try { celdas[x][y].addTexture(textureSheet.getTexture(49), 49); @@ -401,7 +372,7 @@ public class Escenario extends JComponent implements Constantes { } else if (x == HORIZONTAL_CELLS - 1) { // Right wall - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); if (y % 2 == 0) { try { celdas[x][y].addTexture(textureSheet.getTexture(53), 53); @@ -422,7 +393,7 @@ public class Escenario extends JComponent implements Constantes { } else if (y == VERTICAL_CELLS - 1) { // Bottom wall - celdas[x][y].setObject(new Obstacle(null, celdas[x][y])); + celdas[x][y].setObject(new Obstacle(this, celdas[x][y])); if (x % 2 == 0) { try { celdas[x][y].addTexture(textureSheet.getTexture(98), 98); @@ -443,10 +414,9 @@ public class Escenario extends JComponent implements Constantes { } // The player starts at the door - if (x == 2 && y == 1) { - celdas[x][y].setObject(new Player(null, celdas[x][y])); - player = celdas[x][y]; - } + /*if (x == 2 && y == 1) { + celdas[x][y].setObject(new Player(this, celdas[x][y])); + }*/ } } } @@ -491,51 +461,6 @@ public class Escenario extends JComponent implements Constantes { } } - /** - * Get the player cell - * - * @return Returns a cell that contains the player - */ - public Celda getPlayer() { - return player; - } - - /** - * Get the portal - * - * @return Returns the cell contain the portal - */ - public Celda getPortal() { - return portal; - } - - /** - * Get the enemies - * - * @return Returns an array list containing the enemies - */ - public ArrayList getEnemies() { - return enemies; - } - - /** - * Get the chests - * - * @return Returns an array list containing the chests - */ - public ArrayList getChests() { - return chests; - } - - /** - * Get the keys - * - * @return Returns an array list containing the keys - */ - public ArrayList getKeys() { - return keys; - } - /** * Get the parent canvas of this scene * @return Returns the parent canvas diff --git a/src/cl/cromer/azaraka/Lienzo.java b/src/cl/cromer/azaraka/Lienzo.java index 9dc21b4..a631829 100644 --- a/src/cl/cromer/azaraka/Lienzo.java +++ b/src/cl/cromer/azaraka/Lienzo.java @@ -30,8 +30,6 @@ import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; /** @@ -139,6 +137,51 @@ public class Lienzo extends Canvas implements Constantes { setBackground(Color.black); setSize(escenario.width, escenario.height); + Enemy.Direction enemyDirection = Enemy.Direction.DOWN; + + ArrayList objectList = escenario.generateRandomObjects(); + for (Object object : objectList) { + object.getCelda().setObject(object); + if (object instanceof Player) { + player = (Player) object; + threads.put(object, new Thread(object)); + } + else if (object instanceof Enemy) { + ((Enemy) object).setDirection(enemyDirection); + if (enemyDirection == Enemy.Direction.UP) { + enemyDirection = Enemy.Direction.DOWN; + } + else if (enemyDirection == Enemy.Direction.DOWN) { + enemyDirection = Enemy.Direction.LEFT; + } + else if (enemyDirection == Enemy.Direction.LEFT) { + enemyDirection = Enemy.Direction.RIGHT; + } + else { + enemyDirection = Enemy.Direction.UP; + } + enemies.add((Enemy) object); + threads.put(object, new Thread(object)); + } + else if (object instanceof Chest) { + chests.add((Chest) object); + threads.put(object, new Thread(object)); + } + else if (object instanceof Key) { + keys.add((Key) object); + threads.put(object, new Thread(object)); + } + else if (object instanceof Portal) { + portal = (Portal) object; + threads.put(object, new Thread(object)); + } + } + + for (Map.Entry entry : threads.entrySet()) { + Thread thread = entry.getValue(); + thread.start(); + } + addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { @@ -149,56 +192,6 @@ public class Lienzo extends Canvas implements Constantes { } } }); - - player = new Player(escenario, escenario.getPlayer()); - escenario.getPlayer().setObject(player); - threads.put(player, new Thread(player)); - - final Lock lock = new ReentrantLock(true); - - Enemy.Direction enemyDirection = Enemy.Direction.DOWN; - for (Celda celda : escenario.getEnemies()) { - Enemy enemy = new Enemy(escenario, celda, lock); - celda.setObject(enemy); - enemy.setDirection(enemyDirection); - if (enemyDirection == Enemy.Direction.UP) { - enemyDirection = Enemy.Direction.DOWN; - } - else if (enemyDirection == Enemy.Direction.DOWN) { - enemyDirection = Enemy.Direction.LEFT; - } - else if (enemyDirection == Enemy.Direction.LEFT) { - enemyDirection = Enemy.Direction.RIGHT; - } - else { - enemyDirection = Enemy.Direction.UP; - } - enemies.add(enemy); - threads.put(enemy, new Thread(enemy)); - } - - for (Celda celda : escenario.getKeys()) { - Key key = new Key(escenario, celda); - celda.setObject(key); - keys.add(key); - threads.put(key, new Thread(key)); - } - - for (Celda celda : escenario.getChests()) { - Chest chest = new Chest(escenario, celda); - celda.setObject(chest); - chests.add(chest); - threads.put(chest, new Thread(chest)); - } - - portal = new Portal(escenario, escenario.getPortal()); - escenario.getPortal().setObject(portal); - threads.put(portal, new Thread(portal)); - - for (Map.Entry entry : threads.entrySet()) { - Thread thread = entry.getValue(); - thread.start(); - } } /** @@ -226,40 +219,39 @@ public class Lienzo extends Canvas implements Constantes { graphicBuffer.fillRect(0, 0, this.getWidth(), this.getHeight()); int xKey = LEFT_MARGIN; - for (int i = 0; i < keys.size(); i++) { - Key key = keys.get(i); + for (Key key : keys) { if (key.getState() == Key.State.HELD) { - // Set a still frame of the key - //key.setAnimationFrame(4); key.drawAnimation(graphicBuffer, xKey, 8); - xKey = xKey + ((key.getAnimationWidth() + 5) * (i + 1)); + xKey = xKey + 3 + (key.getAnimationWidth()); } } - int health = player.getHealth(); - if (health == 0) { - gameOver = true; - } - int hearts = Player.MAX_HEALTH / 4; - if (heartAnimation == null) { - heartAnimation = new Animation(); - for (int i = 0; i < 5; i++) { - heartAnimation.addImage(Animation.Direction.NONE, "/img/heart/heart" + i + ".png"); + if (player != null) { + int health = player.getHealth(); + if (health == 0) { + gameOver = true; } - } - for (int i = 0; i < hearts; i++) { - try { - heartAnimation.setCurrentFrame(Math.min(health, 4)); - int x = (HORIZONTAL_CELLS * CELL_PIXELS) + LEFT_MARGIN - (heartAnimation.getFrame().getWidth() * hearts) + (heartAnimation.getFrame().getWidth() * i); - graphicBuffer.drawImage(heartAnimation.getFrame(), x, 8, null); + int hearts = Player.MAX_HEALTH / 4; + if (heartAnimation == null) { + heartAnimation = new Animation(); + for (int i = 0; i < 5; i++) { + heartAnimation.addImage(Animation.Direction.NONE, "/img/heart/heart" + i + ".png"); + } } - catch (AnimationException e) { - logger.warning(e.getMessage()); - } - if (health > 0) { - health = health - 4; - if (health < 0) { - health = 0; + for (int i = 0; i < hearts; i++) { + try { + heartAnimation.setCurrentFrame(Math.min(health, 4)); + int x = (HORIZONTAL_CELLS * CELL_PIXELS) + LEFT_MARGIN - (heartAnimation.getFrame().getWidth() * hearts) + (heartAnimation.getFrame().getWidth() * i); + graphicBuffer.drawImage(heartAnimation.getFrame(), x, 8, null); + } + catch (AnimationException e) { + logger.warning(e.getMessage()); + } + if (health > 0) { + health = health - 4; + if (health < 0) { + health = 0; + } } } } @@ -442,13 +434,4 @@ public class Lienzo extends Canvas implements Constantes { public ArrayList getChests() { return chests; } - - /** - * Get the threads that have been created - * - * @return Returns the threads that run in the background - */ - public HashMap getThreads() { - return threads; - } } \ No newline at end of file diff --git a/src/cl/cromer/azaraka/RandomPositionList.java b/src/cl/cromer/azaraka/RandomPositionList.java deleted file mode 100644 index 7705e22..0000000 --- a/src/cl/cromer/azaraka/RandomPositionList.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2019 Chris Cromer - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -package cl.cromer.azaraka; - -import cl.cromer.azaraka.object.Object; - -/** - * This class is used to save locations of random cells for enemies, obstacles, chests, etc - */ -public class RandomPositionList { - /** - * The x position - */ - private int x; - /** - * The y position - */ - private int y; - /** - * The object - */ - private Object object; - - /** - * Initialize the position and type of the list - * @param x The x position - * @param y The y position - * @param object The object - */ - public RandomPositionList(int x, int y, Object object) { - this.x = x; - this.y = y; - this.object = object; - } - - /** - * Get the x position - * @return Returns the x position - */ - public int getX() { - return x; - } - - /** - * Get the y position - * - * @return Returns the y position - */ - public int getY() { - return y; - } - - /** - * Get the type of object that will be stored at the cell position - * @return Returns the cell type - */ - public Object getObject() { - return object; - } - - /** - * Override the equals method so that we only compare the position and not the type - * @param o The object to compare - * @return Returns true if they are the same - */ - @Override - public boolean equals(java.lang.Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - RandomPositionList that = (RandomPositionList) o; - return (x == that.x && y == that.y); - } -} diff --git a/src/cl/cromer/azaraka/object/Key.java b/src/cl/cromer/azaraka/object/Key.java index 960aad7..475600d 100644 --- a/src/cl/cromer/azaraka/object/Key.java +++ b/src/cl/cromer/azaraka/object/Key.java @@ -176,6 +176,15 @@ public class Key extends Object implements Constantes { getEscenario().getCanvas().repaint(); } } + // The thread was killed, set the animation to frame 4 + try { + if (getAnimation().getCurrentFrame() != 4) { + getAnimation().setCurrentFrame(4); + } + } + catch (AnimationException e) { + getLogger().warning(e.getMessage()); + } } /** diff --git a/src/cl/cromer/azaraka/sound/Sound.java b/src/cl/cromer/azaraka/sound/Sound.java index 0d9b0ea..d9b4e20 100644 --- a/src/cl/cromer/azaraka/sound/Sound.java +++ b/src/cl/cromer/azaraka/sound/Sound.java @@ -53,33 +53,19 @@ public class Sound implements Constantes { if (inputStream == null) { throw new SoundException("Could not load sound: " + path); } - AudioInputStream audioInputStream = null; try { - audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream)); - } - catch (UnsupportedAudioFileException | IOException e) { - logger.warning(e.getMessage()); - } - try { - DataLine.Info info = null; - if (audioInputStream != null) { - info = new DataLine.Info(Clip.class, audioInputStream.getFormat()); - } + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream)); + DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat()); sound = (Clip) AudioSystem.getLine(info); + sound.open(audioInputStream); + audioInputStream.close(); } - catch (LineUnavailableException e) { + catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { logger.warning(e.getMessage()); } - try { - if (audioInputStream != null) { - sound.open(audioInputStream); - sound.stop(); - } + finally { + sound.stop(); } - catch (LineUnavailableException | IOException e) { - logger.warning(e.getMessage()); - } - logger.info("Opened sound: " + path); } diff --git a/test/cl/cromer/azaraka/test/RandomPositionListTest.java b/test/cl/cromer/azaraka/test/RandomPositionListTest.java deleted file mode 100644 index 623e118..0000000 --- a/test/cl/cromer/azaraka/test/RandomPositionListTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2019 Chris Cromer - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -package cl.cromer.azaraka.test; - -import cl.cromer.azaraka.Celda; -import cl.cromer.azaraka.RandomPositionList; -import cl.cromer.azaraka.object.Player; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Test the random position position list to make sure it has expected values - */ -class RandomPositionListTest { - private RandomPositionList randomPositionList; - - /** - * Create a random position list - */ - @BeforeEach - void setUp() { - randomPositionList = new RandomPositionList(2, 3, new Player(null, new Celda(0, 0, 0, 0))); - } - - /** - * Destroy the random position list - */ - @AfterEach - void tearDown() { - randomPositionList = null; - } - - /** - * Check if the x position is correct - */ - @Test - void getX() { - assertEquals(2, randomPositionList.getX(), "The position should be 2"); - } - - /** - * Check if the y position is correct - */ - @Test - void getY() { - assertEquals(3, randomPositionList.getY(), "The position should be 3"); - } - - /** - * Check if the type is correct - */ - @Test - void getType() { - assertEquals(Player.class.getName(), randomPositionList.getObject().getClass().getName(), "The type should be player"); - } -} \ No newline at end of file diff --git a/test/cl/cromer/azaraka/test/object/PlayerTest.java b/test/cl/cromer/azaraka/test/object/PlayerTest.java deleted file mode 100644 index 48a2793..0000000 --- a/test/cl/cromer/azaraka/test/object/PlayerTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2019 Chris Cromer - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -package cl.cromer.azaraka.test.object; - -import cl.cromer.azaraka.Escenario; -import cl.cromer.azaraka.Lienzo; -import cl.cromer.azaraka.object.Player; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.awt.*; -import java.awt.event.KeyEvent; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Test the playser object - */ -class PlayerTest { - /** - * A player object to test - */ - private Player player; - /** - * The canvas the scene is in - */ - private Lienzo lienzo; - /** - * The scene the player is in - */ - private Escenario escenario; - - /** - * Create the canvas, scene, and then the player - */ - @BeforeEach - void setUp() { - lienzo = new Lienzo(); - lienzo.changeVolume(0); - escenario = new Escenario(lienzo); - player = new Player(escenario, escenario.getPlayer()); - } - - /** - * Destroy it all - */ - @AfterEach - void tearDown() { - player = null; - escenario = null; - lienzo = null; - } - - /** - * Test key press events and see if the player is where he should be - */ - @Test - void keyPressed() { - int expected = 2; - if (escenario.getCeldas()[player.getX() - 1][player.getY()].getObject() == null) { - expected--; - } - player.keyPressed(new KeyEvent(new Component() { - }, 0, 0, 0, KeyEvent.VK_LEFT, KeyEvent.getKeyText(KeyEvent.VK_LEFT).charAt(0))); - assertEquals(expected, player.getX(), "The player should be at x = 1" + expected); - - if (escenario.getCeldas()[player.getX() + 1][player.getY()].getObject() == null) { - expected++; - } - player.keyPressed(new KeyEvent(new Component() { - }, 0, 0, 0, KeyEvent.VK_RIGHT, KeyEvent.getKeyText(KeyEvent.VK_RIGHT).charAt(0))); - assertEquals(expected, player.getX(), "The player should be at x = 2" + expected); - - expected = 1; - if (escenario.getCeldas()[player.getX()][player.getY() + 1].getObject() == null) { - expected++; - } - player.keyPressed(new KeyEvent(new Component() { - }, 0, 0, 0, KeyEvent.VK_DOWN, KeyEvent.getKeyText(KeyEvent.VK_DOWN).charAt(0))); - assertEquals(expected, player.getY(), "The player should be at y = " + expected); - - if (escenario.getCeldas()[player.getX()][player.getY() - 1].getObject() == null) { - expected--; - } - player.keyPressed(new KeyEvent(new Component() { - }, 0, 0, 0, KeyEvent.VK_UP, KeyEvent.getKeyText(KeyEvent.VK_UP).charAt(0))); - assertEquals(expected, player.getY(), "The player should be at y = " + expected); - } -} \ No newline at end of file