Browse Source

Remove spanish class names

Implement restart
Make sure all objectives are reachable
Optimize code

Signed-off-by: Chris Cromer <chris@cromer.cl>
master
Chris Cromer 2 years ago
parent
commit
f7adc8551e
  1. 4
      build.gradle
  2. 44
      src/main/java/cl/cromer/azaraka/Azaraka.java
  3. 64
      src/main/java/cl/cromer/azaraka/Canvas.java
  4. 4
      src/main/java/cl/cromer/azaraka/Cell.java
  5. 8
      src/main/java/cl/cromer/azaraka/Constants.java
  6. 8
      src/main/java/cl/cromer/azaraka/MainWindow.java
  7. 234
      src/main/java/cl/cromer/azaraka/Scene.java
  8. 14
      src/main/java/cl/cromer/azaraka/ai/AI.java
  9. 22
      src/main/java/cl/cromer/azaraka/ai/BreadthFirstSearch.java
  10. 22
      src/main/java/cl/cromer/azaraka/ai/PlayerAI.java
  11. 2
      src/main/java/cl/cromer/azaraka/ai/State.java
  12. 14
      src/main/java/cl/cromer/azaraka/json/Json.java
  13. 28
      src/main/java/cl/cromer/azaraka/object/Chest.java
  14. 68
      src/main/java/cl/cromer/azaraka/object/Enemy.java
  15. 16
      src/main/java/cl/cromer/azaraka/object/Gem.java
  16. 22
      src/main/java/cl/cromer/azaraka/object/Key.java
  17. 40
      src/main/java/cl/cromer/azaraka/object/Object.java
  18. 12
      src/main/java/cl/cromer/azaraka/object/Obstacle.java
  19. 92
      src/main/java/cl/cromer/azaraka/object/Player.java
  20. 26
      src/main/java/cl/cromer/azaraka/object/Portal.java
  21. 4
      src/main/java/cl/cromer/azaraka/sound/Sound.java
  22. 4
      src/main/java/cl/cromer/azaraka/sprite/Animation.java
  23. 4
      src/main/java/cl/cromer/azaraka/sprite/Sheet.java

4
build.gradle

@ -76,9 +76,13 @@ task createDocs {
}
distributions {
//noinspection GroovyAssignabilityCheck
main {
//noinspection GrUnresolvedAccess
contents {
//noinspection GrUnresolvedAccess
from(createDocs) {
//noinspection GrUnresolvedAccess
into 'docs'
}
}

44
src/main/java/cl/cromer/azaraka/Azaraka.java

@ -21,18 +21,41 @@ import java.util.logging.Logger;
/**
* The main class of the game
*/
public class Azaraka implements Constantes {
public class Azaraka implements Constants {
/**
* The main window
*/
private MainWindow mainWindow;
/**
* The logger
*/
private final Logger logger;
/**
* Initialize the main class
* The main game class
*/
private Azaraka() {
Logger logger = getLogger(this.getClass(), LogLevel.MAIN);
logger = getLogger(this.getClass(), LogLevel.MAIN);
start();
}
/**
* Restart the game
*/
public void restart() {
mainWindow.removeAll();
mainWindow.dispose();
start();
}
/**
* Load the main game window to start
*/
private void start() {
logger.info("Load main window");
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();
ventanaPrincipal.setVisible(true);
ventanaPrincipal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow = new MainWindow(this);
mainWindow.setVisible(true);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
@ -41,6 +64,15 @@ public class Azaraka implements Constantes {
* @param args The arguments passed to the application
*/
public static void main(String[] args) {
int validCells = (HORIZONTAL_CELLS - 2) * (VERTICAL_CELLS - 2);
validCells = validCells - ENEMIES;
validCells = validCells - (CHESTS * 2);
validCells = validCells - OBSTACLES;
if (validCells < 10) {
// This is to prevent a possible infinite loop
System.out.println("Not enough valid cells: " + validCells + "!");
System.exit(0);
}
new Azaraka();
}

64
src/main/java/cl/cromer/azaraka/Lienzo.java → src/main/java/cl/cromer/azaraka/Canvas.java

@ -34,7 +34,11 @@ import java.util.logging.Logger;
/**
* This class extends the canvas to make drawing and listening easier
*/
public class Lienzo extends Canvas implements Constantes {
public class Canvas extends java.awt.Canvas implements Constants {
/**
* The main window
*/
private final Azaraka azaraka;
/**
* The current volume
*/
@ -94,7 +98,7 @@ public class Lienzo extends Canvas implements Constantes {
/**
* The game scene
*/
private Escenario escenario;
private Scene scene;
/**
* The sound played when a key is picked up
*/
@ -155,11 +159,13 @@ public class Lienzo extends Canvas implements Constantes {
/**
* Initialize the canvas
*
* @param azaraka The main window
* @param width The width to set the canvas
* @param height The width to set the canvas
*/
public Lienzo(int width, int height) {
public Canvas(Azaraka azaraka, int width, int height) {
logger = getLogger(this.getClass(), LogLevel.LIENZO);
this.azaraka = azaraka;
setSize(width, height);
leftMargin = (width - CELL_PIXELS * HORIZONTAL_CELLS) / 2;
@ -185,25 +191,25 @@ public class Lienzo extends Canvas implements Constantes {
gameOverAnimation = new Animation();
gameOverAnimation.addImage(Animation.Direction.NONE, "/img/gameover/gameover.png");
escenario = new Escenario(this);
scene = new Scene(this);
ArrayList<Object> objectList = escenario.generateRandomObjects();
ArrayList<Object> objectList = scene.generateRandomObjects();
while (objectList == null) {
escenario = new Escenario(this);
objectList = escenario.generateRandomObjects();
scene = new Scene(this);
objectList = scene.generateRandomObjects();
}
escenario.setDoorSound(doorSound);
scene.setDoorSound(doorSound);
setBackground(Color.black);
Enemy.Direction enemyDirection = Enemy.Direction.DOWN;
// Create the gems and later place them in 2 of the chests
ArrayList<Gem> gems = new ArrayList<>();
Gem lifeGem = new Gem(escenario, new Celda(0, 0, 0, 0));
Gem lifeGem = new Gem(scene, new Cell(0, 0, 0, 0));
lifeGem.setSound(getGemSound);
lifeGem.setType(Gem.Type.LIFE);
Gem deathGem = new Gem(escenario, new Celda(0, 0, 0, 0));
Gem deathGem = new Gem(scene, new Cell(0, 0, 0, 0));
deathGem.setSound(getGemSound);
deathGem.setType(Gem.Type.DEATH);
gems.add(lifeGem);
@ -211,12 +217,12 @@ public class Lienzo extends Canvas implements Constantes {
for (Object object : objectList) {
if (object instanceof Player) {
object.getCelda().setObject(object);
object.getCell().setObject(object);
player = (Player) object;
threads.put(object, new Thread(object));
}
else if (object instanceof Enemy) {
object.getCelda().setObject(object);
object.getCell().setObject(object);
if (enemyDirection == Enemy.Direction.UP) {
enemyDirection = Enemy.Direction.DOWN;
}
@ -235,12 +241,12 @@ public class Lienzo extends Canvas implements Constantes {
threads.put(object, new Thread(object));
}
else if (object instanceof Chest) {
object.getCelda().setObject(object);
object.getCell().setObject(object);
((Chest) object).setSound(openChestSound);
if (gems.size() > 0) {
Gem gem = gems.get(0);
// Place the gem in the cell above the chest, but don't add it to object2 until we are ready to draw it
gem.setCelda(escenario.getCeldas()[object.getCelda().getX()][object.getCelda().getY() - 1]);
gem.setCell(scene.getCells()[object.getCell().getX()][object.getCell().getY() - 1]);
threads.put(gem, new Thread(gem));
((Chest) object).setGem(gem);
gems.remove(gem);
@ -249,13 +255,13 @@ public class Lienzo extends Canvas implements Constantes {
threads.put(object, new Thread(object));
}
else if (object instanceof Key) {
object.getCelda().setObjectOnBottom(object);
object.getCell().setObjectOnBottom(object);
((Key) object).setSound(getKeySound);
keys.add((Key) object);
threads.put(object, new Thread(object));
}
else if (object instanceof Portal) {
object.getCelda().setObjectOnBottom(object);
object.getCell().setObjectOnBottom(object);
portal = (Portal) object;
portal.setSound(portalSound);
threads.put(object, new Thread(object));
@ -293,11 +299,11 @@ public class Lienzo extends Canvas implements Constantes {
// Shuffle the chests so that the AI doesn't open the correct chests on the first go
Collections.shuffle(chests, new Random(23));
for (Chest chest : chests) {
player.getAi().addDestination(new State(chest.getCelda().getX(), chest.getCelda().getY() + 1, State.Type.CHEST, null, 1));
player.getAi().addDestination(new State(chest.getCell().getX(), chest.getCell().getY() + 1, State.Type.CHEST, null, 1));
}
for (Key key : keys) {
player.getAi().addDestination(new State(key.getCelda().getX(), key.getCelda().getY(), State.Type.KEY, null, 0));
player.getAi().addDestination(new State(key.getCell().getX(), key.getCell().getY(), State.Type.KEY, null, 0));
}
Thread thread = new Thread(player.getAi());
@ -376,6 +382,16 @@ public class Lienzo extends Canvas implements Constantes {
if (gameOver) {
if (!gameOverRan) {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
super.keyPressed(event);
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
azaraka.restart();
}
}
});
stopBackgroundMusic();
try {
@ -410,7 +426,7 @@ public class Lienzo extends Canvas implements Constantes {
}
}
else {
escenario.paintComponent(graphicBuffer);
scene.paintComponent(graphicBuffer);
if (won) {
int alpha = (255 * 75) / 100; // 75% transparent
@ -427,6 +443,16 @@ public class Lienzo extends Canvas implements Constantes {
int x = rectangle.x + (rectangle.width - metrics.stringWidth(message)) / 2;
int y = rectangle.y + ((rectangle.height - metrics.getHeight()) / 2) + metrics.getAscent();
graphicBuffer.drawString(message, x, y);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
super.keyPressed(event);
if (event.getKeyCode() == KeyEvent.VK_ENTER) {
System.exit(0);
}
}
});
}
}

4
src/main/java/cl/cromer/azaraka/Celda.java → src/main/java/cl/cromer/azaraka/Cell.java

@ -27,7 +27,7 @@ import java.util.Map;
/**
* This class is a cell that will contain a game element such as a player, enemy, prize, etc
*/
public class Celda extends JComponent implements Constantes {
public class Cell extends JComponent implements Constants {
/**
* The x graphical coordinate of the cell
*/
@ -69,7 +69,7 @@ public class Celda extends JComponent implements Constantes {
* @param x The x coordinate of the cell
* @param y The y coordinate of the cell
*/
public Celda(int xPixels, int yPixels, int x, int y) {
public Cell(int xPixels, int yPixels, int x, int y) {
this.xPixels = xPixels;
this.yPixels = yPixels;
this.x = x;

8
src/main/java/cl/cromer/azaraka/Constantes.java → src/main/java/cl/cromer/azaraka/Constants.java

@ -29,7 +29,7 @@ import java.util.logging.Logger;
/**
* Constants used in the game
*/
public interface Constantes {
public interface Constants {
/**
* The name of the game
*/
@ -37,7 +37,7 @@ public interface Constantes {
/**
* Whether or not the player should be controlled by AI
*/
boolean PLAYER_AI = false;
boolean PLAYER_AI = true;
/**
* Make logs
*/
@ -55,11 +55,11 @@ public interface Constantes {
*/
int CELL_PIXELS = 64;
/**
* The number of cells to draw horizontally, minimum 8
* The number of cells to draw horizontally
*/
int HORIZONTAL_CELLS = 16;
/**
* The number of cells to draw vertically, minimum 8
* The number of cells to draw vertically
*/
int VERTICAL_CELLS = 9;
/**

8
src/main/java/cl/cromer/azaraka/VentanaPrincipal.java → src/main/java/cl/cromer/azaraka/MainWindow.java

@ -25,12 +25,14 @@ import java.util.logging.Logger;
/**
* The main window of the game
*/
public class VentanaPrincipal extends JFrame implements Constantes {
public class MainWindow extends JFrame implements Constants {
/**
* Initialize the main window
*
* @param azaraka The main game class
*/
public VentanaPrincipal() {
public MainWindow(Azaraka azaraka) {
Logger logger = getLogger(this.getClass(), LogLevel.VENTANA_PRINCIPAL);
logger.info("Create panels");
@ -52,7 +54,7 @@ public class VentanaPrincipal extends JFrame implements Constantes {
logger.warning(e.getMessage());
}
Lienzo canvas = new Lienzo(screenSize.width, screenSize.height - 50);
Canvas canvas = new Canvas(azaraka, screenSize.width, screenSize.height - 50);
canvas.setFocusable(true);
canvas.requestFocus();
add(canvas);

234
src/main/java/cl/cromer/azaraka/Escenario.java → src/main/java/cl/cromer/azaraka/Scene.java

@ -17,7 +17,6 @@ package cl.cromer.azaraka;
import cl.cromer.azaraka.ai.BreadthFirstSearch;
import cl.cromer.azaraka.ai.State;
import cl.cromer.azaraka.json.Cell;
import cl.cromer.azaraka.json.Json;
import cl.cromer.azaraka.object.Object;
import cl.cromer.azaraka.object.*;
@ -42,15 +41,15 @@ import java.util.logging.Logger;
/**
* The scene used for the game
*/
public class Escenario extends JComponent implements Constantes {
public class Scene extends JComponent implements Constants {
/**
* The canvas
*/
private final Lienzo canvas;
private final Canvas canvas;
/**
* The cells of the game
*/
private final Celda[][] celdas;
private final Cell[][] cells;
/**
* The logger
*/
@ -67,22 +66,18 @@ public class Escenario extends JComponent implements Constantes {
* The sound the door makes
*/
private Sound doorSound;
/**
* The amount of tries before giving up
*/
private int tries = 0;
/**
* Initialize the scene
*
* @param canvas The canvas that this scene is in
*/
public Escenario(Lienzo canvas) {
public Scene(Canvas canvas) {
logger = getLogger(this.getClass(), LogLevel.ESCENARIO);
this.canvas = canvas;
loadTextures();
celdas = new Celda[HORIZONTAL_CELLS][VERTICAL_CELLS];
cells = new Cell[HORIZONTAL_CELLS][VERTICAL_CELLS];
if (GENERATE_SCENE) {
generateScene();
@ -106,7 +101,7 @@ public class Escenario extends JComponent implements Constantes {
if (EXPORT_SCENE) {
Json json = new Json();
json.exportScene(celdas);
json.exportScene(cells);
}
}
@ -118,37 +113,37 @@ public class Escenario extends JComponent implements Constantes {
private void loadScene(String json) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Cell[][] cells = gson.fromJson(json, Cell[][].class);
cl.cromer.azaraka.json.Cell[][] cells = gson.fromJson(json, cl.cromer.azaraka.json.Cell[][].class);
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) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
this.cells[x][y] = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
if (cells[x][y].type.equals(Player.class.getName())) {
celdas[x][y].setObject(new Player(null, celdas[x][y]));
this.cells[x][y].setObject(new Player(null, this.cells[x][y]));
}
else if (cells[x][y].type.equals(Enemy.class.getName())) {
celdas[x][y].setObject(new Enemy(null, celdas[x][y], null));
this.cells[x][y].setObject(new Enemy(null, this.cells[x][y], null));
}
else if (cells[x][y].type.equals(Chest.class.getName())) {
celdas[x][y].setObject(new Chest(null, celdas[x][y]));
this.cells[x][y].setObject(new Chest(null, this.cells[x][y]));
}
else if (cells[x][y].type.equals(Gem.class.getName())) {
celdas[x][y].setObject(new Gem(null, celdas[x][y]));
this.cells[x][y].setObject(new Gem(null, this.cells[x][y]));
}
else if (cells[x][y].type.equals(Key.class.getName())) {
celdas[x][y].setObject(new Key(null, celdas[x][y]));
this.cells[x][y].setObject(new Key(null, this.cells[x][y]));
}
else if (cells[x][y].type.equals(Obstacle.class.getName())) {
celdas[x][y].setObject(new Obstacle(null, celdas[x][y]));
this.cells[x][y].setObject(new Obstacle(null, this.cells[x][y]));
}
else if (cells[x][y].type.equals(Portal.class.getName())) {
celdas[x][y].setObject(new Portal(null, celdas[x][y]));
this.cells[x][y].setObject(new Portal(null, this.cells[x][y]));
}
for (int k = 0; k < cells[x][y].textures.size(); k++) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(cells[x][y].textures.get(k)), cells[x][y].textures.get(k));
this.cells[x][y].addTexture(textureSheet.getTexture(cells[x][y].textures.get(k)), cells[x][y].textures.get(k));
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -168,17 +163,15 @@ public class Escenario extends JComponent implements Constantes {
ArrayList<Object> 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());
cells[2][1].setObject(new Player(this, cells[2][1]));
objectArrayList.add(cells[2][1].getObject());
for (int i = 0; i < OBSTACLES; i++) {
random = randomCoordinates(false);
if (random[0] == -1 || random[1] == -1) {
return null;
}
celdas[random[0]][random[1]].setObject(new Obstacle(this, celdas[random[0]][random[1]]));
random = randomCoordinates();
cells[random[0]][random[1]].setObject(new Obstacle(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObject());
try {
celdas[random[0]][random[1]].addTexture(textureSheet.getTexture(30), 30);
cells[random[0]][random[1]].addTexture(textureSheet.getTexture(30), 30);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -187,105 +180,90 @@ public class Escenario extends JComponent implements Constantes {
final Lock lock = new ReentrantLock(true);
for (int i = 0; i < ENEMIES; i++) {
random = randomCoordinates(true);
if (random[0] == -1 || random[1] == -1) {
return null;
}
celdas[random[0]][random[1]].setObject(new Enemy(this, celdas[random[0]][random[1]], lock));
objectArrayList.add(celdas[random[0]][random[1]].getObject());
random = randomCoordinates();
cells[random[0]][random[1]].setObject(new Enemy(this, cells[random[0]][random[1]], lock));
objectArrayList.add(cells[random[0]][random[1]].getObject());
}
random = randomCoordinates(true);
if (random[0] == -1 || random[1] == -1) {
return null;
}
celdas[random[0]][random[1]].setObjectOnBottom(new Portal(this, celdas[random[0]][random[1]]));
objectArrayList.add(celdas[random[0]][random[1]].getObjectOnBottom());
random = randomCoordinates();
cells[random[0]][random[1]].setObjectOnBottom(new Portal(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObjectOnBottom());
// Generate enough keys for the chests that will exist
for (int i = 0; i < CHESTS; i++) {
random = randomCoordinates(true);
if (random[0] == -1 || random[1] == -1) {
return null;
}
celdas[random[0]][random[1]].setObjectOnBottom(new Key(this, celdas[random[0]][random[1]]));
objectArrayList.add(celdas[random[0]][random[1]].getObjectOnBottom());
random = randomCoordinates();
cells[random[0]][random[1]].setObjectOnBottom(new Key(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObjectOnBottom());
}
// Chests need to be last to make sure they are openable
for (int i = 0; i < CHESTS; i++) {
tries = 0;
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 (random_y + 1 == VERTICAL_CELLS ||
celdas[random_x][random_y].containsObject() ||
celdas[random_x][random_y + 1].containsObject() ||
celdas[random_x][random_y - 1].containsObject() ||
checkBreadthFirst(random_x, random_y + 1)) {
cells[random_x][random_y].containsObject() ||
cells[random_x][random_y + 1].containsObject()) {
random_x = random(0, HORIZONTAL_CELLS - 1);
random_y = random(0, VERTICAL_CELLS - 1);
tries++;
if (tries == HORIZONTAL_CELLS) {
random[0] = -1;
random[1] = -1;
break;
}
cells[random_x][random_y].setObject(new Chest(this, cells[random_x][random_y]));
objectArrayList.add(cells[random_x][random_y].getObject());
}
for (Object object : objectArrayList) {
if (object instanceof Chest) {
int x = object.getCell().getX();
int y = object.getCell().getY();
if (pathInvalid(x, y + 1)) {
return null;
}
}
if (random[0] == -1 || random[1] == -1) {
return null;
else if (object instanceof Portal || object instanceof Key) {
int x = object.getCell().getX();
int y = object.getCell().getY();
if (pathInvalid(x, y)) {
return null;
}
}
celdas[random_x][random_y].setObjectOnBottom(new Chest(this, celdas[random_x][random_y]));
objectArrayList.add(celdas[random_x][random_y].getObjectOnBottom());
}
return objectArrayList;
}
/**
* Check if the path to the objective is valid
*
* @param x The x coordinate of the objective
* @param y The y coordinate of the objective
* @return Returns true if valid or false otherwise
*/
private boolean pathInvalid(int x, int y) {
BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch(this);
State playerState = new State(2, 1, State.Type.START, null, 0);
State objectiveState = new State(x, y, State.Type.EXIT, null, 0);
return !breadthFirstSearch.search(playerState, objectiveState);
}
/**
* Get random x and y coordinates
*
* @param checkPath Check if the path can be reached using AI
* @return Returns an array with the coordinates
*/
private int[] randomCoordinates(boolean checkPath) {
tries = 0;
private int[] randomCoordinates() {
int[] random = new int[2];
random[0] = random(0, HORIZONTAL_CELLS - 1);
random[1] = random(0, VERTICAL_CELLS - 1);
// If the cell is not empty look for another
// If the cell is not reachable by the player look for another
// If the player can't reach the bottom right corner look for another
while (celdas[random[0]][random[1]].containsObject() || (checkPath && checkBreadthFirst(random[0], random[1]))) {
while (cells[random[0]][random[1]].containsObject()) {
random[0] = random(0, HORIZONTAL_CELLS - 1);
random[1] = random(0, VERTICAL_CELLS - 1);
tries++;
if (tries == VERTICAL_CELLS) {
random[0] = -1;
random[1] = -1;
break;
}
}
return random;
}
/**
* Check the path using BreadFirst-Search
*
* @param x The x position to check
* @param y The y position to check
* @return Returns true if the object is reachable or false otherwise
*/
private boolean checkBreadthFirst(int x, int y) {
BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch(this);
return (!breadthFirstSearch.search(
new State(2, 1, State.Type.START, null, 0),
new State(x, y, State.Type.EXIT, null, 0)) ||
!breadthFirstSearch.search(
new State(2, 1, State.Type.START, null, 0),
new State(HORIZONTAL_CELLS - 2, VERTICAL_CELLS - 2, State.Type.EXIT, null, 0)));
}
/**
* Generate the scene manually without the JSON file
*/
@ -293,9 +271,9 @@ public class Escenario extends JComponent implements Constantes {
for (int x = 0; x < HORIZONTAL_CELLS; x++) {
for (int y = 0; y < VERTICAL_CELLS; y++) {
logger.info("Generate cell x: " + x + " y: " + y + " manually");
celdas[x][y] = new Celda((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
cells[x][y] = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
try {
celdas[x][y].addTexture(textureSheet.getTexture(0), 0);
cells[x][y].addTexture(textureSheet.getTexture(0), 0);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -303,9 +281,9 @@ public class Escenario extends JComponent implements Constantes {
if (x == 0 && y == 0) {
// Top left corner
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(33), 33);
cells[x][y].addTexture(textureSheet.getTexture(33), 33);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -313,9 +291,9 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == HORIZONTAL_CELLS - 1 && y == 0) {
// Top right corner
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(37), 37);
cells[x][y].addTexture(textureSheet.getTexture(37), 37);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -323,9 +301,9 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == 0 && y == VERTICAL_CELLS - 1) {
// Bottom left corner
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(97), 97);
cells[x][y].addTexture(textureSheet.getTexture(97), 97);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -333,9 +311,9 @@ 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(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
try {
celdas[x][y].addTexture(textureSheet.getTexture(101), 101);
cells[x][y].addTexture(textureSheet.getTexture(101), 101);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -343,12 +321,12 @@ public class Escenario extends JComponent implements Constantes {
}
else if (y == 0) {
// Top wall
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
if (x == 1) {
// Left door frame
try {
celdas[x][y].addTexture(textureSheet.getTexture(144), 144);
celdas[x][y].addTexture(textureSheet.getTexture(192), 192);
cells[x][y].addTexture(textureSheet.getTexture(144), 144);
cells[x][y].addTexture(textureSheet.getTexture(192), 192);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -357,7 +335,7 @@ public class Escenario extends JComponent implements Constantes {
else if (x == 2) {
// Door
try {
celdas[x][y].addTexture(textureSheet.getTexture(145), 145);
cells[x][y].addTexture(textureSheet.getTexture(145), 145);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -366,8 +344,8 @@ public class Escenario extends JComponent implements Constantes {
else if (x == 3) {
// Right door frame
try {
celdas[x][y].addTexture(textureSheet.getTexture(146), 146);
celdas[x][y].addTexture(textureSheet.getTexture(194), 194);
cells[x][y].addTexture(textureSheet.getTexture(146), 146);
cells[x][y].addTexture(textureSheet.getTexture(194), 194);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -376,7 +354,7 @@ public class Escenario extends JComponent implements Constantes {
else if (x == 8) {
// Broken wall piece
try {
celdas[x][y].addTexture(textureSheet.getTexture(105), 105);
cells[x][y].addTexture(textureSheet.getTexture(105), 105);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -384,8 +362,8 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(34), 34);
celdas[x][y].addTexture(textureSheet.getTexture(222), 222);
cells[x][y].addTexture(textureSheet.getTexture(34), 34);
cells[x][y].addTexture(textureSheet.getTexture(222), 222);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -393,7 +371,7 @@ public class Escenario extends JComponent implements Constantes {
}
else {
try {
celdas[x][y].addTexture(textureSheet.getTexture(35), 35);
cells[x][y].addTexture(textureSheet.getTexture(35), 35);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -402,11 +380,11 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == 0) {
// Left wall
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(49), 49);
celdas[x][y].addTexture(textureSheet.getTexture(255), 255);
cells[x][y].addTexture(textureSheet.getTexture(49), 49);
cells[x][y].addTexture(textureSheet.getTexture(255), 255);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -414,7 +392,7 @@ public class Escenario extends JComponent implements Constantes {
}
else {
try {
celdas[x][y].addTexture(textureSheet.getTexture(65), 65);
cells[x][y].addTexture(textureSheet.getTexture(65), 65);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -423,11 +401,11 @@ public class Escenario extends JComponent implements Constantes {
}
else if (x == HORIZONTAL_CELLS - 1) {
// Right wall
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(53), 53);
celdas[x][y].addTexture(textureSheet.getTexture(238), 238);
cells[x][y].addTexture(textureSheet.getTexture(53), 53);
cells[x][y].addTexture(textureSheet.getTexture(238), 238);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -435,7 +413,7 @@ public class Escenario extends JComponent implements Constantes {
}
else {
try {
celdas[x][y].addTexture(textureSheet.getTexture(69), 69);
cells[x][y].addTexture(textureSheet.getTexture(69), 69);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -444,11 +422,11 @@ public class Escenario extends JComponent implements Constantes {
}
else if (y == VERTICAL_CELLS - 1) {
// Bottom wall
celdas[x][y].setObject(new Obstacle(this, celdas[x][y]));
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
if (x % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(98), 98);
celdas[x][y].addTexture(textureSheet.getTexture(207), 207);
cells[x][y].addTexture(textureSheet.getTexture(98), 98);
cells[x][y].addTexture(textureSheet.getTexture(207), 207);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -456,7 +434,7 @@ public class Escenario extends JComponent implements Constantes {
}
else {
try {
celdas[x][y].addTexture(textureSheet.getTexture(99), 99);
cells[x][y].addTexture(textureSheet.getTexture(99), 99);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -484,8 +462,8 @@ public class Escenario extends JComponent implements Constantes {
*
* @return Returns a matrix of the cells of the game
*/
public Celda[][] getCeldas() {
return celdas;
public Cell[][] getCells() {
return cells;
}
/**
@ -507,7 +485,7 @@ public class Escenario extends JComponent implements Constantes {
public void update(Graphics g) {
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
celdas[i][j].paintComponent(g);
cells[i][j].paintComponent(g);
}
}
}
@ -517,7 +495,7 @@ public class Escenario extends JComponent implements Constantes {
*
* @return Returns the parent canvas
*/
public Lienzo getCanvas() {
public Canvas getCanvas() {
return canvas;
}
@ -559,9 +537,9 @@ public class Escenario extends JComponent implements Constantes {
*/
public void openDoor(boolean doorOpen) {
if (!doorOpen && isDoorOpen()) {
celdas[2][0].setObject(new Obstacle(this, celdas[2][0]));
cells[2][0].setObject(new Obstacle(this, cells[2][0]));
try {
celdas[2][0].addTexture(textureSheet.getTexture(193), 193);
cells[2][0].addTexture(textureSheet.getTexture(193), 193);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -570,8 +548,8 @@ public class Escenario extends JComponent implements Constantes {
playDoorSound();
}
else if (doorOpen && !isDoorOpen()) {
celdas[2][0].removeTexture(193);
celdas[2][0].setObject(null);
cells[2][0].removeTexture(193);
cells[2][0].setObject(null);
this.doorOpen = true;
playDoorSound();
}

14
src/main/java/cl/cromer/azaraka/ai/AI.java

@ -15,7 +15,7 @@
package cl.cromer.azaraka.ai;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Scene;
import java.util.logging.Logger;
@ -26,7 +26,7 @@ public class AI implements Runnable {
/**
* The scene of the game
*/
private final Escenario escenario;
private final Scene scene;
/**
* The logger
*/
@ -39,10 +39,10 @@ public class AI implements Runnable {
/**
* Initialize the AI
*
* @param escenario The scene of the game
* @param scene The scene of the game
*/
protected AI(Escenario escenario) {
this.escenario = escenario;
protected AI(Scene scene) {
this.scene = scene;
}
/**
@ -50,8 +50,8 @@ public class AI implements Runnable {
*
* @return Returns the scene
*/
protected Escenario getEscenario() {
return escenario;
protected Scene getScene() {
return scene;
}
/**

22
src/main/java/cl/cromer/azaraka/ai/BreadthFirstSearch.java

@ -15,8 +15,8 @@
package cl.cromer.azaraka.ai;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Constants;
import cl.cromer.azaraka.Scene;
import java.awt.geom.Point2D;
import java.util.ArrayList;
@ -24,7 +24,7 @@ import java.util.ArrayList;
/**
* This is an implementation of the Breadth-First search algorithm with multiple objectives
*/
public class BreadthFirstSearch extends AI implements Constantes {
public class BreadthFirstSearch extends AI implements Constants {
/**
* The queued states to check
*/
@ -57,10 +57,10 @@ public class BreadthFirstSearch extends AI implements Constantes {
/**
* Initialize the algorithm
*
* @param escenario The scene the AI is in
* @param scene The scene the AI is in
*/
public BreadthFirstSearch(Escenario escenario) {
super(escenario);
public BreadthFirstSearch(Scene scene) {
super(scene);
setLogger(getLogger(this.getClass(), LogLevel.AI));
}
@ -99,6 +99,8 @@ public class BreadthFirstSearch extends AI implements Constantes {
}
}
// TODO: remove escenario from the algorithm
/**
* Move up if possible
*
@ -106,7 +108,7 @@ public class BreadthFirstSearch extends AI implements Constantes {
*/
private void moveUp(State state) {
if (state.getY() > 0) {
if (getEscenario().getCeldas()[state.getX()][state.getY() - 1].getObject() == null) {
if (getScene().getCells()[state.getX()][state.getY() - 1].getObject() == null) {
State up = new State(state.getX(), state.getY() - 1, State.Type.UP, state, state.getImportance());
if (!history.contains(up)) {
queuedStates.add(up);
@ -128,7 +130,7 @@ public class BreadthFirstSearch extends AI implements Constantes {
*/
private void moveDown(State state) {
if (state.getY() < VERTICAL_CELLS - 1) {
if (getEscenario().getCeldas()[state.getX()][state.getY() + 1].getObject() == null) {
if (getScene().getCells()[state.getX()][state.getY() + 1].getObject() == null) {
State down = new State(state.getX(), state.getY() + 1, State.Type.DOWN, state, state.getImportance());
if (!history.contains(down)) {
queuedStates.add(down);
@ -150,7 +152,7 @@ public class BreadthFirstSearch extends AI implements Constantes {
*/
private void moveLeft(State state) {
if (state.getX() > 0) {
if (getEscenario().getCeldas()[state.getX() - 1][state.getY()].getObject() == null) {
if (getScene().getCells()[state.getX() - 1][state.getY()].getObject() == null) {
State left = new State(state.getX() - 1, state.getY(), State.Type.LEFT, state, state.getImportance());
if (!history.contains(left)) {
queuedStates.add(left);
@ -172,7 +174,7 @@ public class BreadthFirstSearch extends AI implements Constantes {
*/
private void moveRight(State state) {
if (state.getX() < HORIZONTAL_CELLS - 1) {
if (getEscenario().getCeldas()[state.getX() + 1][state.getY()].getObject() == null) {
if (getScene().getCells()[state.getX() + 1][state.getY()].getObject() == null) {
State right = new State(state.getX() + 1, state.getY(), State.Type.RIGHT, state, state.getImportance());
if (!history.contains(right)) {
queuedStates.add(right);

22
src/main/java/cl/cromer/azaraka/ai/PlayerAI.java

@ -15,7 +15,7 @@
package cl.cromer.azaraka.ai;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Scene;
import cl.cromer.azaraka.object.Player;
import cl.cromer.azaraka.object.Portal;
import cl.cromer.azaraka.sprite.Animation;
@ -34,11 +34,11 @@ public class PlayerAI extends BreadthFirstSearch {
/**
* Initialize the algorithm
*
* @param escenario The scene the AI is in
* @param scene The scene the AI is in
* @param player The player controlled by the AI
*/
public PlayerAI(Escenario escenario, Player player) {
super(escenario);
public PlayerAI(Scene scene, Player player) {
super(scene);
this.player = player;
}
@ -57,9 +57,9 @@ public class PlayerAI extends BreadthFirstSearch {
player.keyPressed(KeyEvent.VK_UP);
}
player.interact();
Portal portal = getEscenario().getCanvas().getPortal();
Portal portal = getScene().getCanvas().getPortal();
if (portal.getState() == Portal.State.ACTIVE) {
addDestination(new State(portal.getCelda().getX(), portal.getCelda().getY(), State.Type.PORTAL, null, 2));
addDestination(new State(portal.getCell().getX(), portal.getCell().getY(), State.Type.PORTAL, null, 2));
}
return true;
}
@ -70,7 +70,7 @@ public class PlayerAI extends BreadthFirstSearch {
case KEY:
return true;
case PORTAL:
if (player.hasTaintedGem() && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
if (player.hasTaintedGem() && getScene().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
return true;
}
break;
@ -101,13 +101,13 @@ public class PlayerAI extends BreadthFirstSearch {
break;
case PORTAL:
// If the portal is active head towards it
if (player.hasTaintedGem() && getEscenario().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
if (player.hasTaintedGem() && getScene().getCanvas().getPortal().getState() == Portal.State.ACTIVE) {
return true;
}
break;
case EXIT:
// If the door is open head to it
if (getEscenario().isDoorOpen()) {
if (getScene().isDoorOpen()) {
return true;
}
break;
@ -137,7 +137,7 @@ public class PlayerAI extends BreadthFirstSearch {
}
}
getEscenario().getCanvas().repaint();
getScene().getCanvas().repaint();
}
/**
@ -145,6 +145,6 @@ public class PlayerAI extends BreadthFirstSearch {
*/
@Override
public void getNewInitial() {
setInitial(new State(player.getCelda().getX(), player.getCelda().getY(), State.Type.START, null, 0));
setInitial(new State(player.getCell().getX(), player.getCell().getY(), State.Type.START, null, 0));
}
}

2
src/main/java/cl/cromer/azaraka/ai/State.java

@ -38,7 +38,7 @@ public class State {
/**
* The importance of the objective, higher is more important
*/
private int importance;
private final int importance;
/**
* Initialize the state

14
src/main/java/cl/cromer/azaraka/json/Json.java

@ -15,8 +15,8 @@
package cl.cromer.azaraka.json;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Cell;
import cl.cromer.azaraka.Constants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -28,7 +28,7 @@ import java.util.logging.Logger;
/**
* This class handles reading and writing of JSON objects
*/
public class Json implements Constantes {
public class Json implements Constants {
/**
* The logger
*/
@ -46,11 +46,11 @@ public class Json implements Constantes {
*
* @param celdas The cells of the scene to export
*/
public void exportScene(Celda[][] celdas) {
Cell[][] cells = new Cell[celdas.length][celdas[0].length];
public void exportScene(Cell[][] celdas) {
cl.cromer.azaraka.json.Cell[][] cells = new cl.cromer.azaraka.json.Cell[celdas.length][celdas[0].length];
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] = new cl.cromer.azaraka.json.Cell();
if (celdas[x][y].getObject() != null) {
cells[x][y].type = celdas[x][y].getObject().getClass().getName();
}
@ -68,7 +68,7 @@ public class Json implements Constantes {
*
* @param cells The JSON cells object
*/
private void writeScene(Cell[][] cells) {
private void writeScene(cl.cromer.azaraka.json.Cell[][] cells) {
GsonBuilder gsonBuilder;
if (PRETTY_JSON) {
gsonBuilder = new GsonBuilder().setPrettyPrinting();

28
src/main/java/cl/cromer/azaraka/object/Chest.java

@ -15,9 +15,9 @@
package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Cell;
import cl.cromer.azaraka.Constants;
import cl.cromer.azaraka.Scene;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
@ -28,7 +28,7 @@ import cl.cromer.azaraka.sprite.SheetException;
/**
* This class handles the chests
*/
public class Chest extends Object implements Constantes {
public class Chest extends Object implements Constants {
/**
* The current state of the chest
*/
@ -49,11 +49,11 @@ public class Chest extends Object implements Constantes {
/**
* Initialize the chest
*
* @param escenario The scene the chest is in
* @param celda The cell that contains the chest
* @param scene The scene the chest is in
* @param cell The cell that contains the chest
*/
public Chest(Escenario escenario, Celda celda) {
super(escenario, celda);
public Chest(Scene scene, Cell cell) {
super(scene, cell);
setLogger(getLogger(this.getClass(), LogLevel.CHEST));
loadChestAnimation();
@ -109,7 +109,7 @@ public class Chest extends Object implements Constantes {
catch (AnimationException e) {
getLogger().warning(e.getMessage());
}
getEscenario().getCanvas().repaint();
getScene().getCanvas().repaint();
}
}
@ -136,7 +136,7 @@ public class Chest extends Object implements Constantes {
*/
private void playChestOpenSound() {
try {
sound.setVolume(getEscenario().getCanvas().getVolume());
sound.setVolume(getScene().getCanvas().getVolume());
sound.play();
}
catch (SoundException e) {
@ -188,19 +188,19 @@ public class Chest extends Object implements Constantes {
gemLoops--;
}
else if (gemLoops == 0) {
gem.getCelda().setObjectOnTop(null);
gem.getCell().setObjectOnTop(null);
gem.setYScale(24);
gem.setXScale(24);
gem.setUseOffset(false);
getEscenario().getCanvas().getPlayer().addInventory(gem);
getEscenario().getCanvas().getPortal().setState(Portal.State.ACTIVE);
getScene().getCanvas().getPlayer().addInventory(gem);
getScene().getCanvas().getPortal().setState(Portal.State.ACTIVE);
gemLoops--;
}
}
}
else if (state == State.OPENING) {
animate();
getEscenario().getCanvas().repaint();
getScene().getCanvas().repaint();
}
}
}

68
src/main/java/cl/cromer/azaraka/object/Enemy.java

@ -15,9 +15,9 @@
package cl.cromer.azaraka.object;
import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.Cell;
import cl.cromer.azaraka.Constants;
import cl.cromer.azaraka.Scene;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
@ -29,7 +29,7 @@ import java.util.concurrent.locks.Lock;
/**
* This class handles the enemy object
*/
public class Enemy extends Object implements Constantes {
public class Enemy extends Object implements Constants {
/**
* The lock helps prevent race conditions when checking positioning
*/
@ -46,12 +46,12 @@ public class Enemy extends Object implements Constantes {
/**
* Initialize the enemy
*
* @param escenario The scene the enemy is in
* @param celda The cell this enemy is in