Assign objects to cells

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-30 22:30:27 -03:00
parent db72bcf5dc
commit 4f846a45fc
21 changed files with 942 additions and 932 deletions

File diff suppressed because it is too large Load Diff

View File

@ -15,14 +15,12 @@
package cl.cromer.azaraka;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import cl.cromer.azaraka.object.Object;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Logger;
/**
* This class is a cell that will contain a game element such as a player, enemy, prize, etc
@ -44,14 +42,6 @@ public class Celda extends JComponent implements Constantes {
* The y coordinate of the cell
*/
private int y;
/**
* The type of cell
*/
private Type type = Type.SPACE;
/**
* The sprites that can be used in the cell
*/
private Animation animation = null;
/**
* The textures to show in this cell
*/
@ -61,9 +51,9 @@ public class Celda extends JComponent implements Constantes {
*/
private ArrayList<Integer> textureNumbers = new ArrayList<>();
/**
* The logger
* The object in the cell
*/
private Logger logger;
private Object object = null;
/**
* Initialize the cell with its coordinates
@ -77,7 +67,24 @@ public class Celda extends JComponent implements Constantes {
this.yPixels = yPixels;
this.x = x;
this.y = y;
logger = getLogger(this.getClass(), LogLevel.CELDA);
}
/**
* Get the object that is in the cell
*
* @return Returns the object
*/
public Object getObject() {
return object;
}
/**
* Put an object in the cell
*
* @param object The new object
*/
public void setObject(Object object) {
this.object = object;
}
/**
@ -98,24 +105,6 @@ public class Celda extends JComponent implements Constantes {
return y;
}
/**
* Get the sprite for the cell
*
* @return Return the sprite in use
*/
public Animation getAnimation() {
return animation;
}
/**
* Set which sprite to use for this cell
*
* @param animation The sprite to show
*/
public void setAnimation(Animation animation) {
this.animation = animation;
}
/**
* Add a texture to the texture list
*
@ -144,23 +133,6 @@ public class Celda extends JComponent implements Constantes {
return textureNumbers;
}
/**
* Get the current type of this cell
*
* @return Returns the type of cell
*/
public Type getType() {
return this.type;
}
/**
* Set the type of cell that this will be
* @param type The type
*/
public void setType(Type type) {
this.type = type;
}
/**
* Override the paintComponent method of JComponent to paint the cell based on type
* @param g The graphics object to paint
@ -186,49 +158,8 @@ public class Celda extends JComponent implements Constantes {
}
// Draw a sprite in the cell if needed
if (getType() != Type.SPACE) {
try {
if (animation != null && animation.getFrame() != null) {
g.drawImage(animation.getFrame(), xPixels + animation.getXOffset(), yPixels + animation.getYOffset(), null);
if (object != null) {
object.drawAnimation(g, xPixels, yPixels);
}
}
catch (AnimationException e) {
logger.warning(e.getMessage());
}
}
}
/**
* The possible types of cell that this could be
*/
public enum Type {
/**
* The player
*/
PLAYER,
/**
* An enemy
*/
ENEMY,
/**
* An empty space
*/
SPACE,
/**
* The portal
*/
PORTAL,
/**
* An obstacle
*/
OBSTACLE,
/**
* A chest
*/
CHEST,
/**
* A key
*/
KEY
}
}

View File

@ -90,11 +90,7 @@ public interface Constantes {
/**
* The amount of margin to the left and right of cells
*/
int LEFT_MARGIN = 40;
/**
* The screen size
*/
Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
int LEFT_MARGIN = 50;
/**
* The amount of chests to draw
*/
@ -142,7 +138,7 @@ public interface Constantes {
/**
* Use pretty JSON if true
*/
boolean PRETTY_JSON = false;
boolean PRETTY_JSON = true;
/**
* The normal font to use
*/
@ -217,7 +213,6 @@ public interface Constantes {
SOUND(Level.WARNING),
ANIMATION(Level.WARNING),
SHEET(Level.WARNING),
CELDA(Level.WARNING),
KEY(Level.WARNING),
JSON(Level.WARNING),
PORTAL(Level.WARNING);

View File

@ -17,10 +17,8 @@ package cl.cromer.azaraka;
import cl.cromer.azaraka.json.Cell;
import cl.cromer.azaraka.json.Json;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationMap;
import cl.cromer.azaraka.object.Object;
import cl.cromer.azaraka.object.*;
import cl.cromer.azaraka.sprite.Sheet;
import cl.cromer.azaraka.sprite.SheetException;
import com.google.gson.Gson;
@ -33,8 +31,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
/**
@ -77,14 +73,6 @@ public class Escenario extends JComponent implements Constantes {
* The keys
*/
private ArrayList<Celda> keys = new ArrayList<>();
/**
* A hash map that contains all the sprites for the game
*/
private Map<Animation.SpriteType, Animation> sprites = new AnimationMap();
/**
* A hash map of the sounds
*/
private Map<Sound.SoundType, Sound> sounds = new HashMap<>();
/**
* A collection of tiles that can be used in the scene
*/
@ -106,7 +94,7 @@ public class Escenario extends JComponent implements Constantes {
public Escenario(Lienzo canvas) {
logger = getLogger(this.getClass(), LogLevel.ESCENARIO);
this.canvas = canvas;
loadResources();
loadTextures();
celdas = new Celda[HORIZONTAL_CELLS][VERTICAL_CELLS];
@ -151,20 +139,28 @@ public class Escenario extends JComponent implements Constantes {
for (int x = 0; x < cells.length; x++) {
for (int y = 0; y < cells[x].length; y++) {
celdas[x][y] = new Celda((x * CELL_PIXELS) + LEFT_MARGIN, (y * CELL_PIXELS) + TOP_MARGIN, x, y);
celdas[x][y].setType(cells[x][y].type);
if (cells[x][y].type == Celda.Type.PLAYER) {
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.PLAYER));
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 == Celda.Type.ENEMY) {
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.ENEMY));
else if (cells[x][y].type.equals(Enemy.class.getName())) {
celdas[x][y].setObject(new Enemy(null, celdas[x][y], null));
}
else if (cells[x][y].type == Celda.Type.CHEST) {
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.CHEST));
else if (cells[x][y].type.equals(Chest.class.getName())) {
celdas[x][y].setObject(new Chest(null, celdas[x][y]));
}
else if (cells[x][y].type == Celda.Type.KEY) {
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.KEY));
else if (cells[x][y].type.equals(Gem.class.getName())) {
celdas[x][y].setObject(new Gem(null, celdas[x][y]));
}
else if (cells[x][y].type.equals(Key.class.getName())) {
celdas[x][y].setObject(new Key(null, celdas[x][y]));
}
else if (cells[x][y].type.equals(Obstacle.class.getName())) {
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
}
else if (cells[x][y].type.equals(Portal.class.getName())) {
celdas[x][y].setObject(new Portal(null, celdas[x][y]));
}
for (int k = 0; k < cells[x][y].textures.size(); k++) {
@ -190,45 +186,42 @@ public class Escenario extends JComponent implements Constantes {
int random_y;
ArrayList<RandomPositionList> arrayList = new ArrayList<>();
arrayList.add(new RandomPositionList(5, 5, Celda.Type.CHEST));
arrayList.add(new RandomPositionList(6, 5, Celda.Type.CHEST));
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, Celda.Type.ENEMY)) || celdas[random_x][random_y].getType() != Celda.Type.SPACE) {
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, Celda.Type.ENEMY));
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, Celda.Type.OBSTACLE)) || celdas[random_x][random_y].getType() != Celda.Type.SPACE) {
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, Celda.Type.OBSTACLE));
arrayList.add(new RandomPositionList(random_x, random_y, new Obstacle(null, celdas[random_x][random_y])));
}
random_x = random(0, HORIZONTAL_CELLS - 1);
random_y = random(0, VERTICAL_CELLS - 1);
while (arrayList.contains(new RandomPositionList(random_x, random_y, Celda.Type.PORTAL)) || celdas[random_x][random_y].getType() != Celda.Type.SPACE) {
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, Celda.Type.PORTAL));
arrayList.add(new RandomPositionList(random_x, random_y, new Portal(null, celdas[random_x][random_y])));
// 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, Celda.Type.KEY)) || celdas[random_x][random_y].getType() != Celda.Type.SPACE) {
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, Celda.Type.KEY));
arrayList.add(new RandomPositionList(random_x, random_y, new Key(null, celdas[random_x][random_y])));
}
// Chests need to be last to make sure they are openable
@ -236,42 +229,37 @@ public class Escenario extends JComponent implements Constantes {
random_x = random(0, HORIZONTAL_CELLS - 1);
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, Celda.Type.CHEST)) || arrayList.contains(new RandomPositionList(random_x, random_y + 1, Celda.Type.CHEST)) || celdas[random_x][random_y].getType() != Celda.Type.SPACE || celdas[random_x][random_y + 1].getType() != Celda.Type.SPACE || celdas[random_x][random_y - 1].getType() != Celda.Type.SPACE) {
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) {
random_x = random(0, HORIZONTAL_CELLS - 1);
random_y = random(0, VERTICAL_CELLS - 1);
}
arrayList.add(new RandomPositionList(random_x, random_y, Celda.Type.CHEST));
arrayList.add(new RandomPositionList(random_x, random_y, new Chest(null, celdas[random_x][random_y])));
}
for (RandomPositionList randomList : arrayList) {
int x = randomList.getX();
int y = randomList.getY();
celdas[x][y].setType(randomList.getType());
switch (randomList.getType()) {
case ENEMY:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.ENEMY));
Object object = randomList.getObject();
celdas[x][y].setObject(object);
if (object instanceof Enemy) {
enemies.add(celdas[x][y]);
break;
case CHEST:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.CHEST));
}
else if (object instanceof Chest) {
chests.add(celdas[x][y]);
break;
case KEY:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.KEY));
}
else if (object instanceof Key) {
keys.add(celdas[x][y]);
break;
case PORTAL:
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.INACTIVE_PORTAL));
}
else if (object instanceof Portal) {
portal = celdas[x][y];
break;
case OBSTACLE:
}
else if (object instanceof Obstacle) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(30), 30);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
break;
}
}
}
@ -293,7 +281,7 @@ public class Escenario extends JComponent implements Constantes {
if (x == 0 && y == 0) {
// Top left corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(33), 33);
}
@ -303,7 +291,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == HORIZONTAL_CELLS - 1 && y == 0) {
// Top right corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(37), 37);
}
@ -313,7 +301,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == 0 && y == VERTICAL_CELLS - 1) {
// Bottom left corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(97), 97);
}
@ -323,7 +311,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == HORIZONTAL_CELLS - 1 && y == VERTICAL_CELLS - 1) {
// Bottom right corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(101), 101);
}
@ -333,7 +321,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (y == 0) {
// Top wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
if (x == 1) {
// Left door frame
try {
@ -392,7 +380,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == 0) {
// Left wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(49), 49);
@ -413,7 +401,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == HORIZONTAL_CELLS - 1) {
// Right wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(53), 53);
@ -434,7 +422,7 @@ public class Escenario extends JComponent implements Constantes {
}
else if (y == VERTICAL_CELLS - 1) {
// Bottom wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
if (x % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(98), 98);
@ -456,8 +444,7 @@ public class Escenario extends JComponent implements Constantes {
// The player starts at the door
if (x == 2 && y == 1) {
celdas[x][y].setType(Celda.Type.PLAYER);
celdas[x][y].setAnimation(sprites.get(Animation.SpriteType.PLAYER));
celdas[x][y].setObject(new Player(null, celdas[x][y]));
player = celdas[x][y];
}
}
@ -465,161 +452,10 @@ public class Escenario extends JComponent implements Constantes {
}
/**
* Load all the images that will be shown in the game
* Load all the textures that will be shown in the game
*/
private void loadResources() {
Animation animation;
// Load player animations
Sheet characterSheet = new Sheet("/img/player/chara2.png", 54, 39);
int character = 6;
try {
animation = new Animation();
animation.setCurrentDirection(Animation.Direction.DOWN);
loadCharacter(animation, characterSheet, character);
sprites.put(Animation.SpriteType.PLAYER, animation);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
// Load enemy animations
characterSheet = new Sheet("/img/enemy/chara4.png", 54, 39);
character = 57;
try {
animation = new Animation();
animation.setCurrentDirection(Animation.Direction.LEFT);
loadCharacter(animation, characterSheet, character);
sprites.put(Animation.SpriteType.ENEMY, animation);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
// Load the chest animation
Sheet chestSheet = new Sheet("/img/chest/chests.png", 54, 63);
try {
animation = new Animation();
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(54));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(66));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(78));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(80));
animation.setYOffset(0);
sprites.put(Animation.SpriteType.CHEST, animation);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
// Load the key animation
Sheet keySheet = new Sheet("/img/key/key.png", 24, 24);
try {
animation = new Animation();
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(0));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(1));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(2));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(3));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(4));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(5));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(6));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(7));
sprites.put(Animation.SpriteType.KEY, animation);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
// Load the active portal
animation = new Animation();
for (int i = 0; i < 120; i++) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(i);
while (stringBuilder.length() < 3) {
stringBuilder.insert(0, 0);
}
stringBuilder.append(".png");
animation.addImage(Animation.Direction.NONE, "/img/portal/green/" + stringBuilder.toString());
}
sprites.put(Animation.SpriteType.ACTIVE_PORTAL, animation);
// Load the inactive portal
animation = new Animation();
for (int i = 0; i < 120; i++) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(i);
while (stringBuilder.length() < 3) {
stringBuilder.insert(0, 0);
}
stringBuilder.append(".png");
animation.addImage(Animation.Direction.NONE, "/img/portal/gray/" + stringBuilder.toString());
}
sprites.put(Animation.SpriteType.INACTIVE_PORTAL, animation);
// Load the hearts
animation = new Animation();
for (int i = 0; i < 5; i++) {
animation.addImage(Animation.Direction.NONE, "/img/heart/heart" + i + ".png");
}
sprites.put(Animation.SpriteType.HEART, animation);
// Load the game over
animation = new Animation();
animation.addImage(Animation.Direction.NONE, "/img/gameover/gameover.png");
sprites.put(Animation.SpriteType.GAME_OVER, animation);
// Load the background textures
private void loadTextures() {
textureSheet = new Sheet("/img/textures/dungeon.png", 64, 64);
try {
Sound sound = new Sound("/snd/OpenChest.wav");
sounds.put(Sound.SoundType.OPEN_CHEST, sound);
sound = new Sound("/snd/EnemyAttack.wav");
sounds.put(Sound.SoundType.ENEMY_ATTACK, sound);
sound = new Sound("/snd/GameOver.wav");
sounds.put(Sound.SoundType.GAME_OVER, sound);
sound = new Sound("/snd/GameLoop.wav");
sounds.put(Sound.SoundType.BACKGROUND, sound);
sound = new Sound("/snd/GetKey.wav");
sounds.put(Sound.SoundType.GET_KEY, sound);
sound = new Sound("/snd/Success.wav");
sounds.put(Sound.SoundType.SUCCESS, sound);
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
}
/**
* Load a character animation
*
* @param animation The animation object
* @param characterSheet The sheet to load from
* @param character The position in the character sheet to start from
* @throws SheetException Thrown if there is a problem loading images from the sheet
*/
private void loadCharacter(Animation animation, Sheet characterSheet, int character) throws SheetException {
animation.addImage(Animation.Direction.DOWN, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.DOWN, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.LEFT, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.LEFT, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.RIGHT, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.RIGHT, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character + 2));
animation.setYOffset(0);
}
/**
@ -738,24 +574,6 @@ public class Escenario extends JComponent implements Constantes {
}
}
/**
* Get the sprites available
*
* @return Returns all available sprites
*/
public Map<Animation.SpriteType, Animation> getSprites() {
return sprites;
}
/**
* Get the available sounds
*
* @return Returns all available sounds
*/
public Map<Sound.SoundType, Sound> getSounds() {
return sounds;
}
/**
* Get the texture sheet
*

View File

@ -78,10 +78,30 @@ public class Lienzo extends Canvas implements Constantes {
* The logger
*/
private Logger logger;
/**
* The game over animation
*/
private Animation gameOverAnimation;
/**
* The hearts animation
*/
private Animation heartAnimation;
/**
* The background music of the game
*/
private Sound backgroundMusic;
/**
* The music played when game over shows
*/
private Sound gameOverMusic;
/**
* The sound played when a gem is purified or the player wins
*/
private Sound successSound;
/**
* Has the game started
*/
private boolean gameStarted = false;
/**
* Game over
*/
@ -90,12 +110,37 @@ public class Lienzo extends Canvas implements Constantes {
* If the game over loop has been run at least once
*/
private boolean gameOverRan = false;
/**
* The current volume
*/
private float volume = (float) DEFAULT_VOLUME / 100;
/**
* Initialize the canvas
*/
public Lienzo() {
logger = getLogger(this.getClass(), LogLevel.LIENZO);
// Load the sounds
try {
backgroundMusic = new Sound("/snd/GameLoop.wav");
gameOverMusic = new Sound("/snd/GameOver.wav");
successSound = new Sound("/snd/Success.wav");
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
// Load the hearts
heartAnimation = new Animation();
for (int i = 0; i < 5; i++) {
heartAnimation.addImage(Animation.Direction.NONE, "/img/heart/heart" + i + ".png");
}
// Load the game over
gameOverAnimation = new Animation();
gameOverAnimation.addImage(Animation.Direction.NONE, "/img/gameover/gameover.png");
escenario = new Escenario(this);
setBackground(Color.black);
setSize(escenario.width, escenario.height);
@ -112,6 +157,7 @@ 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);
@ -119,6 +165,7 @@ public class Lienzo extends Canvas implements Constantes {
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;
@ -138,32 +185,26 @@ public class Lienzo extends Canvas implements Constantes {
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<Object, Thread> entry : threads.entrySet()) {
Thread thread = entry.getValue();
thread.start();
}
try {
backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND);
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
backgroundMusic.play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
}
/**
@ -189,32 +230,16 @@ public class Lienzo extends Canvas implements Constantes {
graphicBuffer.setColor(getBackground());
graphicBuffer.fillRect(0, 0, this.getWidth(), this.getHeight());
// This is needed if there is a background image
//graphicBuffer.drawImage();
Animation keyAnimation = null;
switch (player.keyCount()) {
case 2:
try {
keyAnimation = escenario.getSprites().get(Animation.SpriteType.KEY);
keyAnimation.setCurrentFrame(4);
graphicBuffer.drawImage(keyAnimation.getFrame(), 69, 8, null);
int xKey = LEFT_MARGIN;
for (int i = 0; i < keys.size(); i++) {
Key key = keys.get(i);
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));
}
catch (AnimationException e) {
logger.warning(e.getMessage());
}
case 1:
try {
if (keyAnimation == null) {
keyAnimation = escenario.getSprites().get(Animation.SpriteType.KEY);
keyAnimation.setCurrentFrame(4);
}
graphicBuffer.drawImage(keyAnimation.getFrame(), 40, 8, null);
}
catch (AnimationException e) {
logger.warning(e.getMessage());
}
break;
}
int health = player.getHealth();
@ -223,7 +248,6 @@ public class Lienzo extends Canvas implements Constantes {
}
int hearts = Player.MAX_HEALTH / 4;
for (int i = 0; i < hearts; i++) {
Animation heartAnimation = escenario.getSprites().get(Animation.SpriteType.HEART);
if (health >= 4) {
try {
heartAnimation.setCurrentFrame(4);
@ -241,7 +265,7 @@ public class Lienzo extends Canvas implements Constantes {
}
}
try {
int x = ((HORIZONTAL_CELLS) * CELL_PIXELS) + LEFT_MARGIN - heartAnimation.getFrame().getWidth() * hearts + heartAnimation.getFrame().getWidth() * i;
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) {
@ -260,7 +284,8 @@ public class Lienzo extends Canvas implements Constantes {
stopBackgroundMusic();
try {
escenario.getSounds().get(Sound.SoundType.GAME_OVER).play();
gameOverMusic.setVolume(volume);
gameOverMusic.play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
@ -272,13 +297,12 @@ public class Lienzo extends Canvas implements Constantes {
}
// Place the game over image on the screen
Animation gameOver = escenario.getSprites().get(Animation.SpriteType.GAME_OVER);
graphicBuffer.setColor(Color.black);
graphicBuffer.drawRect(0, 0, getWidth(), getHeight());
try {
int x = (getWidth() - gameOver.getFrame().getWidth()) / 2;
int y = (getHeight() - gameOver.getFrame().getHeight()) / 2;
graphicBuffer.drawImage(gameOver.getFrame(), x, y, null);
int x = (getWidth() - gameOverAnimation.getFrame().getWidth()) / 2;
int y = (getHeight() - gameOverAnimation.getFrame().getHeight()) / 2;
graphicBuffer.drawImage(gameOverAnimation.getFrame(), x, y, null);
}
catch (AnimationException e) {
logger.warning(e.getMessage());
@ -289,8 +313,24 @@ public class Lienzo extends Canvas implements Constantes {
}
g.drawImage(imageBuffer, 0, 0, null);
if (!gameStarted) {
gameStarted = true;
try {
if (!backgroundMusic.isPlaying()) {
backgroundMusic.setVolume(volume);
backgroundMusic.play();
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
}
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
}
}
/**
* Stop the background music
*/
private void stopBackgroundMusic() {
try {
if (backgroundMusic.isPlaying()) {
@ -329,7 +369,8 @@ public class Lienzo extends Canvas implements Constantes {
stopBackgroundMusic();
try {
escenario.getSounds().get(Sound.SoundType.SUCCESS).play();
successSound.setVolume(volume);
successSound.play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
@ -355,18 +396,29 @@ public class Lienzo extends Canvas implements Constantes {
requestFocus();
}
/**
* Get the current volume
*
* @return Returns the current volume
*/
public float getVolume() {
return volume;
}
/**
* Change the volume of the game background music
*
* @param volume The new volume
*/
public void changeVolume(float volume) {
this.volume = volume;
try {
backgroundMusic.setVolume(volume);
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
requestFocus();
}
/**

View File

@ -33,7 +33,6 @@ public class Main implements Constantes {
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();
ventanaPrincipal.setVisible(true);
ventanaPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
logger.info("Finished loading main window");
}
/**

View File

@ -15,6 +15,8 @@
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
*/
@ -28,20 +30,20 @@ public class RandomPositionList {
*/
private int y;
/**
* The type
* The object
*/
private Celda.Type type;
private Object object;
/**
* Initialize the position and type of the list
* @param x The x position
* @param y The y position
* @param type The type
* @param object The object
*/
public RandomPositionList(int x, int y, Celda.Type type) {
public RandomPositionList(int x, int y, Object object) {
this.x = x;
this.y = y;
this.type = type;
this.object = object;
}
/**
@ -65,8 +67,8 @@ public class RandomPositionList {
* Get the type of object that will be stored at the cell position
* @return Returns the cell type
*/
public Celda.Type getType() {
return type;
public Object getObject() {
return object;
}
/**
@ -75,7 +77,7 @@ public class RandomPositionList {
* @return Returns true if they are the same
*/
@Override
public boolean equals(Object o) {
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}

View File

@ -38,23 +38,7 @@ public class VentanaPrincipal extends JFrame implements Constantes {
logger.info("Create panels");
JSplitPane panelSeparator = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
panelSeparator.setOneTouchExpandable(true);
Game gamePanel = new Game();
Config configPanel = new Config(gamePanel);
panelSeparator.setLeftComponent(gamePanel);
panelSeparator.setRightComponent(configPanel);
panelSeparator.setDividerLocation(gamePanel.getWidth() + (LEFT_MARGIN * 2));
panelSeparator.setDividerSize(0);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panelSeparator, BorderLayout.CENTER);
setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
setSize(SCREEN_SIZE.width - 50, SCREEN_SIZE.height - 50);
setTitle(TITLE);
String icon = "/img/icon.png";
@ -67,6 +51,20 @@ public class VentanaPrincipal extends JFrame implements Constantes {
logger.warning(e.getMessage());
}
logger.info("Finished creating panels");
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JSplitPane panelSeparator = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
panelSeparator.setOneTouchExpandable(true);
Game gamePanel = new Game();
Config configPanel = new Config(gamePanel);
panelSeparator.setLeftComponent(gamePanel);
panelSeparator.setRightComponent(configPanel);
panelSeparator.setDividerLocation(gamePanel.getWidth() + (LEFT_MARGIN * 2));
panelSeparator.setDividerSize(0);
contentPane.add(panelSeparator, BorderLayout.CENTER);
}
}

View File

@ -15,8 +15,6 @@
package cl.cromer.azaraka.json;
import cl.cromer.azaraka.Celda;
import java.util.ArrayList;
/**
@ -26,7 +24,7 @@ public class Cell {
/**
* The type of cell, e.g. player, chest, enemy, etc
*/
public Celda.Type type;
public String type;
/**
* A list of the textures to apply to the cell
*/

View File

@ -51,7 +51,12 @@ public class Json implements Constantes {
for (int x = 0; x < celdas.length; x++) {
for (int y = 0; y < celdas[x].length; y++) {
cells[x][y] = new Cell();
cells[x][y].type = celdas[x][y].getType();
if (celdas[x][y].getObject() != null) {
cells[x][y].type = celdas[x][y].getObject().getClass().getName();
}
else {
cells[x][y].type = "null";
}
cells[x][y].textures = celdas[x][y].getTextureNumbers();
}
}

View File

@ -18,9 +18,12 @@ package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import java.util.logging.Logger;
import cl.cromer.azaraka.sprite.Sheet;
import cl.cromer.azaraka.sprite.SheetException;
/**
* This class handles the chests
@ -31,9 +34,9 @@ public class Chest extends Object implements Constantes {
*/
private State state = State.CLOSED;
/**
* The logger
* The open chest sound
*/
private Logger logger;
private Sound sound;
/**
* Initialize the chest
@ -42,7 +45,41 @@ public class Chest extends Object implements Constantes {
*/
public Chest(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), LogLevel.CHEST);
setLogger(getLogger(this.getClass(), LogLevel.CHEST));
loadChestAnimation();
loadChestOpenSound();
}
/**
* Load the chest open sound
*/
private void loadChestOpenSound() {
try {
sound = new Sound("/snd/OpenChest.wav");
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Load the chest animation
*/
private void loadChestAnimation() {
Sheet chestSheet = new Sheet("/img/chest/chests.png", 54, 63);
try {
Animation animation = new Animation();
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(54));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(66));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(78));
animation.addImage(Animation.Direction.NONE, chestSheet.getTexture(80));
animation.setYOffset(0);
setAnimation(animation);
}
catch (SheetException e) {
getLogger().warning(e.getMessage());
}
}
/**
@ -54,6 +91,19 @@ public class Chest extends Object implements Constantes {
return state;
}
/**
* Play the chest opening sound
*/
private void playChestOpenSound() {
try {
sound.setVolume(getEscenario().getCanvas().getVolume());
sound.play();
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Sets the state of the chest
*
@ -62,18 +112,19 @@ public class Chest extends Object implements Constantes {
public void setState(State state) {
this.state = state;
if (state == State.OPENING) {
logger.info("Chest is opening");
getLogger().info("Chest is opening");
playChestOpenSound();
}
else if (state == State.OPENED) {
logger.info("Chest is opened");
getLogger().info("Chest is opened");
}
else if (state == State.CLOSED) {
logger.info("Chest is closed");
getLogger().info("Chest is closed");
try {
getCelda().getAnimation().setCurrentFrame(0);
getAnimation().setCurrentFrame(0);
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
getEscenario().getCanvas().repaint();
}
@ -84,13 +135,13 @@ public class Chest extends Object implements Constantes {
*/
private void animate() {
try {
getCelda().getAnimation().getNextFrame();
if (getCelda().getAnimation().getCurrentFrame() == getCelda().getAnimation().getFrameCount() - 1) {
getAnimation().getNextFrame();
if (getAnimation().getCurrentFrame() == getAnimation().getFrameCount() - 1) {
setState(State.OPENED);
}
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
@ -105,7 +156,7 @@ public class Chest extends Object implements Constantes {
Thread.sleep(200);
}
catch (InterruptedException e) {
logger.info(e.getMessage());
getLogger().info(e.getMessage());
}
synchronized (this) {
if (state == State.OPENING) {

View File

@ -25,7 +25,6 @@ import cl.cromer.azaraka.sprite.AnimationException;
import cl.cromer.azaraka.sprite.SheetException;
import java.util.concurrent.locks.Lock;
import java.util.logging.Logger;
/**
* This class handles the enemy object
@ -35,10 +34,6 @@ public class Enemy extends Object implements Constantes {
* The current direction the enemy is facing
*/
private Direction direction = Direction.LEFT;
/**
* The logger for this class
*/
private Logger logger;
/**
* The speed of the enemy
*/
@ -47,6 +42,10 @@ public class Enemy extends Object implements Constantes {
* The lock helps prevent race conditions when checking positioning
*/
private Lock lock;
/**
* The enemy attack sound
*/
private Sound sound;
/**
* Initialize the enemy
@ -57,8 +56,42 @@ public class Enemy extends Object implements Constantes {
*/
public Enemy(Escenario escenario, Celda celda, Lock lock) {
super(escenario, celda);
logger = getLogger(this.getClass(), LogLevel.ENEMY);
setLogger(getLogger(this.getClass(), LogLevel.ENEMY));
this.lock = lock;
loadEnemyAnimation();
loadAttackSound();
}
/**
* Load the enemy animation
*/
private void loadEnemyAnimation() {
loadCharacter("/img/enemy/chara4.png", 57);
}
/**
* Load the attack sound
*/
private void loadAttackSound() {
try {
sound = new Sound("/snd/EnemyAttack.wav");
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Play the attack sound
*/
private void playAttackSound() {
try {
sound.setVolume(getEscenario().getCanvas().getVolume());
sound.play();
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
@ -77,98 +110,98 @@ public class Enemy extends Object implements Constantes {
int x = getX();
int y = getY();
if (direction == Direction.LEFT) {
if (x > 0 && getEscenario().getCeldas()[x - 1][y].getType() == Celda.Type.SPACE) {
getEscenario().getCeldas()[x - 1][y].setType(Celda.Type.ENEMY);
getEscenario().getCeldas()[x - 1][y].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y].setAnimation(null);
if (x > 0 && getEscenario().getCeldas()[x - 1][y].getObject() == null) {
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x - 1][y]);
getCelda().setObject(this);
try {
getEscenario().getCeldas()[x - 1][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
setX(getX() - 1);
logger.info("Move left to x: " + x + " y: " + y);
getLogger().info("Move left to x: " + x + " y: " + y);
}
else if (x > 0 && getEscenario().getCeldas()[x - 1][y].getType() == Celda.Type.PLAYER) {
else if (x > 0 && getEscenario().getCeldas()[x - 1][y].getObject() instanceof Player) {
attackPlayer(x - 1, y);
}
else {
logger.info("Change to right direction");
getEscenario().getCeldas()[x][y].getAnimation().setCurrentDirection(Animation.Direction.RIGHT);
getLogger().info("Change to right direction");
getAnimation().setCurrentDirection(Animation.Direction.RIGHT);
direction = Direction.RIGHT;
}
}
else if (direction == Direction.RIGHT) {
if (x < (HORIZONTAL_CELLS - 1) && getEscenario().getCeldas()[x + 1][y].getType() == Celda.Type.SPACE) {
getEscenario().getCeldas()[x + 1][y].setType(Celda.Type.ENEMY);
getEscenario().getCeldas()[x + 1][y].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y].setAnimation(null);
if (x < (HORIZONTAL_CELLS - 1) && getEscenario().getCeldas()[x + 1][y].getObject() == null) {
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x + 1][y]);
getCelda().setObject(this);
try {
getEscenario().getCeldas()[x + 1][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
setX(getX() + 1);
logger.info("Move right to x: " + x + " y: " + y);
getLogger().info("Move right to x: " + x + " y: " + y);
}
else if (x < (HORIZONTAL_CELLS - 1) && getEscenario().getCeldas()[x + 1][y].getType() == Celda.Type.PLAYER) {
else if (x < (HORIZONTAL_CELLS - 1) && getEscenario().getCeldas()[x + 1][y].getObject() instanceof Player) {
attackPlayer(x + 1, y);
}
else {
logger.info("Change to left direction");
getEscenario().getCeldas()[x][y].getAnimation().setCurrentDirection(Animation.Direction.LEFT);
getLogger().info("Change to left direction");
getAnimation().setCurrentDirection(Animation.Direction.LEFT);
direction = Direction.LEFT;
}
}
else if (direction == Direction.DOWN) {
if (y < (VERTICAL_CELLS) - 1 && getEscenario().getCeldas()[x][y + 1].getType() == Celda.Type.SPACE) {
getEscenario().getCeldas()[x][y + 1].setType(Celda.Type.ENEMY);
getEscenario().getCeldas()[x][y + 1].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y].setAnimation(null);
if (y < (VERTICAL_CELLS) - 1 && getEscenario().getCeldas()[x][y + 1].getObject() == null) {
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x][y + 1]);
getCelda().setObject(this);
try {
getEscenario().getCeldas()[x][y + 1].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
setY(getY() + 1);
logger.info("Move down to x: " + x + " y: " + y);
getLogger().info("Move down to x: " + x + " y: " + y);
}
else if (y < (VERTICAL_CELLS - 1) && getEscenario().getCeldas()[x][y + 1].getType() == Celda.Type.PLAYER) {
else if (y < (VERTICAL_CELLS - 1) && getEscenario().getCeldas()[x][y + 1].getObject() instanceof Player) {
attackPlayer(x, y + 1);
}
else {
logger.info("Change to up direction");
getEscenario().getCeldas()[x][y].getAnimation().setCurrentDirection(Animation.Direction.UP);
getLogger().info("Change to up direction");
getAnimation().setCurrentDirection(Animation.Direction.UP);
direction = Direction.UP;
}
}
else if (direction == Direction.UP) {
if (y > 0 && getEscenario().getCeldas()[x][y - 1].getType() == Celda.Type.SPACE) {
getEscenario().getCeldas()[x][y - 1].setType(Celda.Type.ENEMY);
getEscenario().getCeldas()[x][y - 1].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y].setAnimation(null);
if (y > 0 && getEscenario().getCeldas()[x][y - 1].getObject() == null) {
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x][y - 1]);
getCelda().setObject(this);
try {
getEscenario().getCeldas()[x][y - 1].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
setY(getY() - 1);
logger.info("Move up to x: " + x + " y: " + y);
getLogger().info("Move up to x: " + x + " y: " + y);
}
else if (y > 0 && getEscenario().getCeldas()[x][y - 1].getType() == Celda.Type.PLAYER) {
else if (y > 0 && getEscenario().getCeldas()[x][y - 1].getObject() instanceof Player) {
attackPlayer(x, y - 1);
}
else {
logger.info("Change to down direction");
getEscenario().getCeldas()[x][y].getAnimation().setCurrentDirection(Animation.Direction.DOWN);
getLogger().info("Change to down direction");
getAnimation().setCurrentDirection(Animation.Direction.DOWN);
direction = Direction.DOWN;
}
}
@ -182,21 +215,16 @@ public class Enemy extends Object implements Constantes {
*/
private void attackPlayer(int x, int y) {
if (getEscenario().getCanvas().getPlayer().getHealth() > 0) {
logger.info("Attacked player at x: " + x + " y: " + y);
getLogger().info("Attacked player at x: " + x + " y: " + y);
try {
getEscenario().getSounds().get(Sound.SoundType.ENEMY_ATTACK).play();
}
catch (SoundException e) {
e.printStackTrace();
}
playAttackSound();
getEscenario().getCanvas().getPlayer().loseHealth(2);
try {
getEscenario().getCeldas()[x][y].addTexture(getEscenario().getTextureSheet().getTexture(12), 12);
}
catch (SheetException e) {
e.printStackTrace();
getLogger().warning(e.getMessage());
}
}
}
@ -211,7 +239,7 @@ public class Enemy extends Object implements Constantes {
Thread.sleep(speed);
}
catch (InterruptedException e) {
logger.info(e.getMessage());
getLogger().info(e.getMessage());
}
synchronized (this) {
lock.lock();

View File

@ -18,18 +18,25 @@ package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import java.util.logging.Logger;
import cl.cromer.azaraka.sprite.Sheet;
import cl.cromer.azaraka.sprite.SheetException;
/**
* This class contains the key
*/
public class Key extends Object implements Constantes {
/**
* The logger
* The current state of the key
*/
private Logger logger;
private State state = State.UNUSED;
/**
* The sound when the player gets a key
*/
private Sound sound;
/**
* Initialize the key
@ -39,7 +46,104 @@ public class Key extends Object implements Constantes {
*/
public Key(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), LogLevel.KEY);
setLogger(getLogger(this.getClass(), LogLevel.KEY));
loadKeyAnimation();
loadGetKeySound();
}
/**
* Load the key sound
*/
private void loadGetKeySound() {
try {
sound = new Sound("/snd/GetKey.wav");
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Load the key animation
*/
private void loadKeyAnimation() {
Sheet keySheet = new Sheet("/img/key/key.png", 24, 24);
Animation animation = new Animation();
try {
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(0));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(1));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(2));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(3));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(4));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(5));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(6));
animation.addImage(Animation.Direction.NONE, keySheet.getTexture(7));
setAnimation(animation);
}
catch (SheetException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Get the width of the key animation
*
* @return Returns the key animation width
*/
public int getAnimationWidth() {
try {
return getAnimation().getFrame().getWidth();
}
catch (AnimationException e) {
getLogger().warning(e.getMessage());
}
return 0;
}
/**
* Play the get key sound
*/
public void playGetKeySound() {
try {
sound.setVolume(getEscenario().getCanvas().getVolume());
sound.play();
}
catch (SoundException e) {
getLogger().warning(e.getMessage());
}
}
/**
* Get the key
*/
public void getKey() {
// Remove the key from the cell
getCelda().setObject(null);
setState(State.HELD);
}
/**
* Get the current state of the key
*
* @return Returns the key's state
*/
public State getState() {
return state;
}
/**
* Set the new state of the key
*
* @param state The new state of the key
*/
public void setState(State state) {
if (this.state == State.UNUSED && state == State.HELD) {
setUseOffset(false);
}
else if (this.state == State.HELD && state == State.UNUSED) {
setUseOffset(true);
}
this.state = state;
}
/**
@ -47,10 +151,10 @@ public class Key extends Object implements Constantes {
*/
private void animate() {
try {
getCelda().getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
@ -65,7 +169,7 @@ public class Key extends Object implements Constantes {
Thread.sleep(100);
}
catch (InterruptedException e) {
logger.info(e.getMessage());
getLogger().info(e.getMessage());
}
synchronized (this) {
animate();
@ -84,4 +188,22 @@ public class Key extends Object implements Constantes {
public boolean checkPosition(int x, int y) {
return (getX() == x && getY() == y);
}
/**
* The state of the key
*/
public enum State {
/**
* The key has been used
*/
USED,
/**
* The key has not been used
*/
UNUSED,
/**
* The key is held by the player
*/
HELD
}
}

View File

@ -17,6 +17,13 @@ package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import cl.cromer.azaraka.sprite.Sheet;
import cl.cromer.azaraka.sprite.SheetException;
import java.awt.*;
import java.util.logging.Logger;
/**
* All game objects extend this class
@ -38,6 +45,18 @@ public class Object implements Runnable {
* The cell the object is in
*/
private Celda celda;
/**
* The animation of the object
*/
private Animation animation;
/**
* Use an offset when drawing the animation
*/
private boolean useOffset = true;
/**
* The logger
*/
private Logger logger;
/**
* Whether or not the run loop of the object is active
*/
@ -110,6 +129,89 @@ public class Object implements Runnable {
return celda;
}
/**
* Get the current animation
*
* @return Returns an animation
*/
public Animation getAnimation() {
return animation;
}
/**
* Set a new animation
*
* @param animation The new animation
*/
public void setAnimation(Animation animation) {
this.animation = animation;
}
/**
* Set the use offset for animation
*
* @param useOffset If true the animation will use an offset to help center it
*/
public void setUseOffset(boolean useOffset) {
this.useOffset = useOffset;
}
/**
* Load the character animation
*
* @param path The path to the image
* @param character The character number
*/
public void loadCharacter(String path, int character) {
Sheet characterSheet = new Sheet(path, 54, 39);
try {
Animation animation = new Animation();
animation.setCurrentDirection(Animation.Direction.DOWN);
animation.addImage(Animation.Direction.DOWN, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.DOWN, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.LEFT, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.LEFT, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.RIGHT, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.RIGHT, characterSheet.getTexture(character + 2));
character = character + 12;
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character + 2));
animation.setYOffset(0);
setAnimation(animation);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
/**
* Draw the animation on the canvas
*
* @param graphics The graphics object to draw to
* @param x The x coordinate to draw to
* @param y The y coordinate to draw to
*/
public void drawAnimation(Graphics graphics, int x, int y) {
try {
if (animation != null && animation.getFrame() != null) {
if (useOffset) {
graphics.drawImage(animation.getFrame(), x + animation.getXOffset(), y + animation.getYOffset(), null);
}
else {
graphics.drawImage(animation.getFrame(), x, y, null);
}
}
}
catch (AnimationException e) {
logger.warning(e.getMessage());
}
}
/**
* Get the cell the object is in
*
@ -119,6 +221,24 @@ public class Object implements Runnable {
this.celda = celda;
}
/**
* Get the logger
*
* @return Returns a logger
*/
public Logger getLogger() {
return logger;
}
/**
* Set the logger
*
* @param logger The logger to set
*/
public void setLogger(Logger logger) {
this.logger = logger;
}
/**
* Get the active state of the GameObject
*

View File

@ -13,33 +13,22 @@
*
*/
package cl.cromer.azaraka.sprite;
package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Constantes;
import java.util.HashMap;
import java.util.logging.Logger;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Escenario;
/**
* This class is used to copy the sprite into a new sprite object so that the sprite doesn't get passed by reference
* This is important because 2 cells share the same sprite, but not the same frame of animation
* This class handles the obstacles
*/
public class AnimationMap extends HashMap<Animation.SpriteType, Animation> implements Constantes {
public class Obstacle extends Object {
/**
* Clone the sprite object when returning
* Initialize the obstacle
*
* @param key The key used to get the object
* @return Return the clone of the sprite
* @param escenario The scene the object is in
* @param celda The cell the object is in
*/
@Override
public Animation get(Object key) {
try {
return (Animation) super.get(key).clone();
}
catch (CloneNotSupportedException e) {
Logger logger = getLogger(this.getClass(), LogLevel.ANIMATION);
logger.warning(e.getMessage());
}
return null;
public Obstacle(Escenario escenario, Celda celda) {
super(escenario, celda);
}
}

View File

@ -18,14 +18,11 @@ package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.logging.Logger;
/**
* This class contains the player
@ -35,10 +32,6 @@ public class Player extends Object implements Constantes {
* The maximum health of the player
*/
public final static int MAX_HEALTH = 8;
/**
* The logger
*/
private Logger logger;
/**
* The current health of the player
*/
@ -56,7 +49,15 @@ public class Player extends Object implements Constantes {
*/
public Player(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), LogLevel.PLAYER);
setLogger(getLogger(this.getClass(), LogLevel.PLAYER));
loadPlayerAnimation();
}
/**
* Load the player animation
*/
private void loadPlayerAnimation() {
loadCharacter("/img/player/chara2.png", 6);
}
/**
@ -93,49 +94,45 @@ public class Player extends Object implements Constantes {
private void moveUp() {
int x = getX();
int y = getY();
logger.info("Up key pressed");
getLogger().info("Up key pressed");
if (y > 0) {
Celda.Type type = getEscenario().getCeldas()[x][y - 1].getType();
if (type == Celda.Type.SPACE || type == Celda.Type.KEY) {
if (type == Celda.Type.KEY) {
Object type = getEscenario().getCeldas()[x][y - 1].getObject();
if (type == null || type instanceof Key) {
if (type != null) {
for (Key key : getEscenario().getCanvas().getKeys()) {
if (key.checkPosition(x, y - 1)) {
// Get the key
getKey(key);
// Remove the key from the cell
getEscenario().getCeldas()[x][y - 1].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y - 1].setAnimation(null);
break;
}
}
}
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y - 1].setType(Celda.Type.PLAYER);
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x][y - 1]);
getCelda().setObject(this);
if (changeDirection(Animation.Direction.UP)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
getEscenario().getCeldas()[x][y - 1].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setAnimation(null);
setY(getY() - 1);
}
else if (type == Celda.Type.PORTAL && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
else if (type instanceof Portal && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
getEscenario().getCanvas().win();
}
else {
if (changeDirection(Animation.Direction.UP)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -143,10 +140,10 @@ public class Player extends Object implements Constantes {
else {
if (changeDirection(Animation.Direction.UP)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -158,49 +155,45 @@ public class Player extends Object implements Constantes {
private void moveDown() {
int x = getX();
int y = getY();
logger.info("Down key pressed");
Celda.Type type = getEscenario().getCeldas()[x][y + 1].getType();
getLogger().info("Down key pressed");
Object type = getEscenario().getCeldas()[x][y + 1].getObject();
if (y < (VERTICAL_CELLS - 1)) {
if (type == Celda.Type.SPACE || type == Celda.Type.KEY) {
if (type == Celda.Type.KEY) {
if (type == null || type instanceof Key) {
if (type != null) {
for (Key key : getEscenario().getCanvas().getKeys()) {
if (key.checkPosition(x, y + 1)) {
// Get the key
getKey(key);
// Remove the key from the cell
getEscenario().getCeldas()[x][y + 1].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y + 1].setAnimation(null);
break;
}
}
}
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x][y + 1].setType(Celda.Type.PLAYER);
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x][y + 1]);
getCelda().setObject(this);
if (changeDirection(Animation.Direction.DOWN)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
getEscenario().getCeldas()[x][y + 1].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setAnimation(null);
setY(getY() + 1);
}
else if (type == Celda.Type.PORTAL && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
else if (type instanceof Portal && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
getEscenario().getCanvas().win();
}
else {
if (changeDirection(Animation.Direction.DOWN)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -208,10 +201,10 @@ public class Player extends Object implements Constantes {
else {
if (changeDirection(Animation.Direction.DOWN)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -223,49 +216,45 @@ public class Player extends Object implements Constantes {
private void moveLeft() {
int x = getX();
int y = getY();
logger.info("Left key pressed");
getLogger().info("Left key pressed");
if (x > 0) {
Celda.Type type = getEscenario().getCeldas()[x - 1][y].getType();
if (type == Celda.Type.SPACE || type == Celda.Type.KEY) {
if (type == Celda.Type.KEY) {
Object type = getEscenario().getCeldas()[x - 1][y].getObject();
if (type == null || type instanceof Key) {
if (type != null) {
for (Key key : getEscenario().getCanvas().getKeys()) {
if (key.checkPosition(x - 1, y)) {
// Get the key
getKey(key);
// Remove the key from the cell
getEscenario().getCeldas()[x - 1][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x - 1][y].setAnimation(null);
break;
}
}
}
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x - 1][y].setType(Celda.Type.PLAYER);
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x - 1][y]);
getCelda().setObject(this);
if (changeDirection(Animation.Direction.LEFT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
getEscenario().getCeldas()[x - 1][y].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setAnimation(null);
setX(getX() - 1);
}
else if (type == Celda.Type.PORTAL && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
else if (type instanceof Portal && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
getEscenario().getCanvas().win();
}
else {
if (changeDirection(Animation.Direction.LEFT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -273,10 +262,10 @@ public class Player extends Object implements Constantes {
else {
if (changeDirection(Animation.Direction.LEFT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -288,49 +277,45 @@ public class Player extends Object implements Constantes {
private void moveRight() {
int x = getX();
int y = getY();
logger.info("Right key pressed");
Celda.Type type = getEscenario().getCeldas()[x + 1][y].getType();
getLogger().info("Right key pressed");
Object type = getEscenario().getCeldas()[x + 1][y].getObject();
if (x < (HORIZONTAL_CELLS - 1)) {
if (type == Celda.Type.SPACE || type == Celda.Type.KEY) {
if (type == Celda.Type.KEY) {
if (type == null || type instanceof Key) {
if (type != null) {
for (Key key : getEscenario().getCanvas().getKeys()) {
if (key.checkPosition(x + 1, y)) {
// Get the key
getKey(key);
// Remove the key from the cell
getEscenario().getCeldas()[x + 1][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x + 1][y].setAnimation(null);
break;
}
}
}
getEscenario().getCeldas()[x][y].setType(Celda.Type.SPACE);
getEscenario().getCeldas()[x + 1][y].setType(Celda.Type.PLAYER);
getCelda().setObject(null);
setCelda(getEscenario().getCeldas()[x + 1][y]);
getCelda().setObject(this);
if (changeDirection(Animation.Direction.RIGHT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
getEscenario().getCeldas()[x + 1][y].setAnimation(getEscenario().getCeldas()[x][y].getAnimation());
getEscenario().getCeldas()[x][y].setAnimation(null);
setX(getX() + 1);
}
else if (type == Celda.Type.PORTAL && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
else if (type instanceof Portal && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
getEscenario().getCanvas().win();
}
else {
if (changeDirection(Animation.Direction.RIGHT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -338,10 +323,10 @@ public class Player extends Object implements Constantes {
else {
if (changeDirection(Animation.Direction.RIGHT)) {
try {
getEscenario().getCeldas()[x][y].getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -354,10 +339,8 @@ public class Player extends Object implements Constantes {
* @return Returns true if a direction change is not necessary
*/
private boolean changeDirection(Animation.Direction direction) {
int x = getX();
int y = getY();
if (getEscenario().getCeldas()[x][y].getAnimation().getCurrentDirection() != direction) {
getEscenario().getCeldas()[x][y].getAnimation().setCurrentDirection(direction);
if (getAnimation().getCurrentDirection() != direction) {
getAnimation().setCurrentDirection(direction);
return false;
}
else {
@ -373,27 +356,11 @@ public class Player extends Object implements Constantes {
private void getKey(Key key) {
gainHealth(1);
// Kill the loop in the thread
key.setActive(false);
key.getKey();
//key.setActive(false);
key.playGetKeySound();
// Add key to inventory
carrying.add(key);
// Stop the thread
try {
getEscenario().getCanvas().getThreads().get(key).join();
}
catch (InterruptedException e) {
logger.warning(e.getMessage());
}
// Play sound
Sound keySound = getEscenario().getSounds().get(Sound.SoundType.GET_KEY);
try {
if (keySound.isPlaying()) {
keySound.stop();
}
keySound.play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
}
/**
@ -402,24 +369,13 @@ public class Player extends Object implements Constantes {
private void interact() {
int x = getX();
int y = getY();
logger.info("Space bar pressed");
if (getEscenario().getCeldas()[x][y].getAnimation().getCurrentDirection() == Animation.Direction.UP) {
if (getEscenario().getCeldas()[x][y - 1].getType() == Celda.Type.CHEST) {
getLogger().info("Space bar pressed");
if (getAnimation().getCurrentDirection() == Animation.Direction.UP) {
if (getEscenario().getCeldas()[x][y - 1].getObject() instanceof Chest) {
if (hasKey()) {
logger.info("Player opened chest");
getLogger().info("Player opened chest");
Sound chestSound = getEscenario().getSounds().get(Sound.SoundType.OPEN_CHEST);
try {
if (chestSound.isPlaying()) {
chestSound.stop();
}
chestSound.play();
}
catch (SoundException e) {
logger.warning(e.getMessage());
}
gainHealth(1);
gainHealth(2);
int openedChests = 0;
for (Chest chest : getEscenario().getCanvas().getChests()) {
@ -434,7 +390,7 @@ public class Player extends Object implements Constantes {
}
}
// All chests are opened, active portal
// All chests are opened, activate portal
if (openedChests == getEscenario().getCanvas().getChests().size()) {
getEscenario().getCanvas().getPortal().setState(Portal.State.ACTIVE);
}
@ -457,28 +413,14 @@ public class Player extends Object implements Constantes {
return false;
}
/**
* Check how many keys the player has
*
* @return Returns the number of key in the inventory
*/
public int keyCount() {
int i = 0;
for (Object object : carrying) {
if (object instanceof Key) {
i++;
}
}
return i;
}
/**
* Removes a key from the player inventory
*/
private void useKey() {
for (Object object : carrying) {
if (object instanceof Key) {
logger.info("Used key");
getLogger().info("Used key");
((Key) object).setState(Key.State.USED);
carrying.remove(object);
return;
}
@ -501,10 +443,10 @@ public class Player extends Object implements Constantes {
*/
public void loseHealth(int amount) {
if (health > 0) {
logger.info("Lose " + amount + " health");
getLogger().info("Lose " + amount + " health");
health = health - amount;
if (health < 0) {
logger.info("Player is dead");
getLogger().info("Player is dead");
health = 0;
}
}
@ -517,7 +459,7 @@ public class Player extends Object implements Constantes {
*/
private void gainHealth(int amount) {
if (health < MAX_HEALTH) {
logger.info("Gain " + amount + " health");
getLogger().info("Gain " + amount + " health");
health = health + amount;
if (health > MAX_HEALTH) {
health = MAX_HEALTH;
@ -536,7 +478,7 @@ public class Player extends Object implements Constantes {
Thread.sleep(5000);
}
catch (InterruptedException e) {
logger.info(e.getMessage());
getLogger().info(e.getMessage());
}
synchronized (this) {
loseHealth(1);

View File

@ -21,8 +21,6 @@ import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
import java.util.logging.Logger;
/**
* This class handles the portal functionality
*/
@ -32,9 +30,13 @@ public class Portal extends Object implements Constantes {
*/
private State state = State.INACTIVE;
/**
* The logger
* The active animation
*/
private Logger logger;
private Animation activeAnimation;
/**
* The inactive animation
*/
private Animation inactiveAnimation;
/**
* Initialize the portal
@ -44,7 +46,37 @@ public class Portal extends Object implements Constantes {
*/
public Portal(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), LogLevel.PORTAL);
setLogger(getLogger(this.getClass(), LogLevel.PORTAL));
loadPortalAnimation();
}
/**
* Load the portal animation
*/
private void loadPortalAnimation() {
activeAnimation = new Animation();
for (int i = 0; i < 120; i++) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(i);
while (stringBuilder.length() < 3) {
stringBuilder.insert(0, 0);
}
stringBuilder.append(".png");
activeAnimation.addImage(Animation.Direction.NONE, "/img/portal/green/" + stringBuilder.toString());
}
inactiveAnimation = new Animation();
for (int i = 0; i < 120; i++) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(i);
while (stringBuilder.length() < 3) {
stringBuilder.insert(0, 0);
}
stringBuilder.append(".png");
inactiveAnimation.addImage(Animation.Direction.NONE, "/img/portal/gray/" + stringBuilder.toString());
}
setAnimation(inactiveAnimation);
}
/**
@ -52,10 +84,10 @@ public class Portal extends Object implements Constantes {
*/
private void animate() {
try {
getCelda().getAnimation().getNextFrame();
getAnimation().getNextFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
@ -77,28 +109,28 @@ public class Portal extends Object implements Constantes {
this.state = state;
int frame = 0;
try {
frame = getCelda().getAnimation().getCurrentFrame();
frame = getAnimation().getCurrentFrame();
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
if (state == State.ACTIVE) {
getCelda().setAnimation(getEscenario().getSprites().get(Animation.SpriteType.ACTIVE_PORTAL));
setAnimation(activeAnimation);
try {
getCelda().getAnimation().setCurrentFrame(frame);
getAnimation().setCurrentFrame(frame);
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
else if (state == State.INACTIVE) {
getCelda().setAnimation(getEscenario().getSprites().get(Animation.SpriteType.INACTIVE_PORTAL));
setAnimation(inactiveAnimation);
try {
getCelda().getAnimation().setCurrentFrame(frame);
getAnimation().setCurrentFrame(frame);
}
catch (AnimationException e) {
logger.warning(e.getMessage());
getLogger().warning(e.getMessage());
}
}
}
@ -114,7 +146,7 @@ public class Portal extends Object implements Constantes {
Thread.sleep(35);
}
catch (InterruptedException e) {
logger.info(e.getMessage());
getLogger().info(e.getMessage());
}
synchronized (this) {
animate();

View File

@ -178,34 +178,4 @@ public class Sound implements Constantes {
logger.info("No control to modify volume");
}
}
/**
* The types of sounds
*/
public enum SoundType {
/**
* Background music
*/
BACKGROUND,
/**
* Open chest sound
*/
OPEN_CHEST,
/**
* Get key sound
*/
GET_KEY,
/**
* Game over music
*/
GAME_OVER,
/**
* Enemy attack sound
*/
ENEMY_ATTACK,
/**
* Sounds when receiving gem, purifying gem, or winning
*/
SUCCESS
}
}

View File

@ -208,48 +208,6 @@ public class Animation implements Cloneable, Constantes {
return getImagesFromHash().size();
}
/**
* The sprite type
*/
public enum SpriteType {
/**
* The player animation
*/
PLAYER,
/**
* The enemy animation
*/
ENEMY,
/**
* The chest animation
*/
CHEST,
/**
* The gem animation
*/
GEM,
/**
* The key animation
*/
KEY,
/**
* The heart animation
*/
HEART,
/**
* The game over animation
*/
GAME_OVER,
/**
* The inactive portal animation
*/
INACTIVE_PORTAL,
/**
* The active portal animation
*/
ACTIVE_PORTAL
}
/**
* Get the current frame
*

View File

@ -17,6 +17,7 @@ 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;
@ -34,7 +35,7 @@ class RandomPositionListTest {
*/
@BeforeEach
void setUp() {
randomPositionList = new RandomPositionList(2, 3, Celda.Type.PLAYER);
randomPositionList = new RandomPositionList(2, 3, new Player(null, new Celda(0, 0, 0, 0)));
}
/**
@ -66,6 +67,6 @@ class RandomPositionListTest {
*/
@Test
void getType() {
assertEquals(Celda.Type.PLAYER, randomPositionList.getType(), "The type should be player");
assertEquals(Player.class.getName(), randomPositionList.getObject().getClass().getName(), "The type should be player");
}
}

View File

@ -15,7 +15,6 @@
package cl.cromer.azaraka.test.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Lienzo;
import cl.cromer.azaraka.object.Player;
@ -71,14 +70,14 @@ class PlayerTest {
@Test
void keyPressed() {
int expected = 2;
if (escenario.getCeldas()[player.getX() - 1][player.getY()].getType() == Celda.Type.SPACE) {
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()].getType() == Celda.Type.SPACE) {
if (escenario.getCeldas()[player.getX() + 1][player.getY()].getObject() == null) {
expected++;
}
player.keyPressed(new KeyEvent(new Component() {
@ -86,14 +85,14 @@ class PlayerTest {
assertEquals(expected, player.getX(), "The player should be at x = 2" + expected);
expected = 1;
if (escenario.getCeldas()[player.getX()][player.getY() + 1].getType() == Celda.Type.SPACE) {
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].getType() == Celda.Type.SPACE) {
if (escenario.getCeldas()[player.getX()][player.getY() - 1].getObject() == null) {
expected--;
}
player.keyPressed(new KeyEvent(new Component() {