Switch to a concurrent matrix for the cells

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-10-26 18:04:08 -03:00
parent 8c627bda8c
commit 4067055af4
10 changed files with 151 additions and 145 deletions

View File

@ -264,7 +264,7 @@ public class Canvas extends java.awt.Canvas implements Constants {
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.setCell(scene.getCells()[object.getCell().getX()][object.getCell().getY() - 1]);
gem.setCell(scene.getCells().get(object.getCell().getX()).get(object.getCell().getY() - 1));
threads.put(gem, new Thread(gem));
((Chest) object).setGem(gem);
gems.remove(gem);

View File

@ -44,6 +44,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
@ -59,8 +60,8 @@ public class Scene extends JComponent implements Constants {
/**
* The cells of the game
*/
private final Cell[][] cells;
//private final CopyOnWriteArrayList<CopyOnWriteArrayList<Cell>> cells;
//private final Cell[][] cells;
private final CopyOnWriteArrayList<CopyOnWriteArrayList<Cell>> cells;
/**
* The logger
*/
@ -88,7 +89,8 @@ public class Scene extends JComponent implements Constants {
this.canvas = canvas;
loadTextures();
cells = new Cell[HORIZONTAL_CELLS][VERTICAL_CELLS];
//cells = new Cell[HORIZONTAL_CELLS][VERTICAL_CELLS];
cells = new CopyOnWriteArrayList<>();
if (GENERATE_SCENE) {
generateScene();
@ -128,33 +130,37 @@ public class Scene extends JComponent implements Constants {
for (int x = 0; x < jsonCells.length; x++) {
for (int y = 0; y < jsonCells[x].length; y++) {
this.cells[x][y] = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
if (cells.size() <= x) {
cells.add(new CopyOnWriteArrayList<>());
}
Cell cell = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
cells.get(x).add(cell);
if (jsonCells[x][y].type.equals(Player.class.getName())) {
this.cells[x][y].setObject(new Player(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Player(null, cells.get(x).get(y)));
}
else if (jsonCells[x][y].type.equals(Enemy.class.getName())) {
this.cells[x][y].setObject(new Enemy(null, this.cells[x][y], null));
cells.get(x).get(y).setObject(new Enemy(null, cells.get(x).get(y), null));
}
else if (jsonCells[x][y].type.equals(Chest.class.getName())) {
this.cells[x][y].setObject(new Chest(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Chest(null, cells.get(x).get(y)));
}
else if (jsonCells[x][y].type.equals(Gem.class.getName())) {
this.cells[x][y].setObject(new Gem(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Gem(null, cells.get(x).get(y)));
}
else if (jsonCells[x][y].type.equals(Key.class.getName())) {
this.cells[x][y].setObject(new Key(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Key(null, cells.get(x).get(y)));
}
else if (jsonCells[x][y].type.equals(Obstacle.class.getName())) {
this.cells[x][y].setObject(new Obstacle(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(null, cells.get(x).get(y)));
}
else if (jsonCells[x][y].type.equals(Portal.class.getName())) {
this.cells[x][y].setObject(new Portal(null, this.cells[x][y]));
cells.get(x).get(y).setObject(new Portal(null, cells.get(x).get(y)));
}
for (int k = 0; k < jsonCells[x][y].textures.size(); k++) {
try {
this.cells[x][y].addTexture(textureSheet.getTexture(jsonCells[x][y].textures.get(k)), jsonCells[x][y].textures.get(k));
cells.get(x).get(y).addTexture(textureSheet.getTexture(jsonCells[x][y].textures.get(k)), jsonCells[x][y].textures.get(k));
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -174,15 +180,15 @@ public class Scene extends JComponent implements Constants {
List<Object> objectArrayList = new ArrayList<>();
// The player has a fixed position
cells[2][1].setObject(new Player(this, cells[2][1]));
objectArrayList.add(cells[2][1].getObject());
cells.get(2).get(1).setObject(new Player(this, cells.get(2).get(1)));
objectArrayList.add(cells.get(2).get(1).getObject());
for (int i = 0; i < OBSTACLES; i++) {
random = randomCoordinates();
cells[random[0]][random[1]].setObject(new Obstacle(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObject());
cells.get(random[0]).get(random[1]).setObject(new Obstacle(this, cells.get(random[0]).get(random[1])));
objectArrayList.add(cells.get(random[0]).get(random[1]).getObject());
try {
cells[random[0]][random[1]].addTexture(textureSheet.getTexture(30), 30);
cells.get(random[0]).get(random[1]).addTexture(textureSheet.getTexture(30), 30);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -192,19 +198,19 @@ public class Scene extends JComponent implements Constants {
final Lock lock = new ReentrantLock(false);
for (int i = 0; i < ENEMIES; i++) {
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());
cells.get(random[0]).get(random[1]).setObject(new Enemy(this, cells.get(random[0]).get(random[1]), lock));
objectArrayList.add(cells.get(random[0]).get(random[1]).getObject());
}
random = randomCoordinates();
cells[random[0]][random[1]].setObjectOnBottom(new Portal(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObjectOnBottom());
cells.get(random[0]).get(random[1]).setObjectOnBottom(new Portal(this, cells.get(random[0]).get(random[1])));
objectArrayList.add(cells.get(random[0]).get(random[1]).getObjectOnBottom());
// Generate enough keys for the chests that will exist
for (int i = 0; i < CHESTS; i++) {
random = randomCoordinates();
cells[random[0]][random[1]].setObjectOnBottom(new Key(this, cells[random[0]][random[1]]));
objectArrayList.add(cells[random[0]][random[1]].getObjectOnBottom());
cells.get(random[0]).get(random[1]).setObjectOnBottom(new Key(this, cells.get(random[0]).get(random[1])));
objectArrayList.add(cells.get(random[0]).get(random[1]).getObjectOnBottom());
}
// Chests need to be last to make sure they are openable
@ -213,13 +219,13 @@ public class Scene extends JComponent implements Constants {
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 ||
cells[random_x][random_y].containsObject() ||
cells[random_x][random_y + 1].containsObject()) {
cells.get(random_x).get(random_y).containsObject() ||
cells.get(random_x).get(random_y + 1).containsObject()) {
random_x = random(0, HORIZONTAL_CELLS - 1);
random_y = random(0, VERTICAL_CELLS - 1);
}
cells[random_x][random_y].setObject(new Chest(this, cells[random_x][random_y]));
objectArrayList.add(cells[random_x][random_y].getObject());
cells.get(random_x).get(random_y).setObject(new Chest(this, cells.get(random_x).get(random_y)));
objectArrayList.add(cells.get(random_x).get(random_y).getObject());
}
for (Object object : objectArrayList) {
@ -288,7 +294,7 @@ public class Scene extends JComponent implements Constants {
// 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 (cells[random[0]][random[1]].containsObject()) {
while (cells.get(random[0]).get(random[1]).containsObject()) {
random[0] = random(0, HORIZONTAL_CELLS - 1);
random[1] = random(0, VERTICAL_CELLS - 1);
}
@ -302,9 +308,13 @@ public class Scene extends JComponent implements Constants {
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");
cells[x][y] = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
if (cells.size() == x) {
cells.add(new CopyOnWriteArrayList<>());
}
Cell cell = new Cell((x * CELL_PIXELS) + canvas.getLeftMargin(), (y * CELL_PIXELS) + canvas.getTopMargin(), x, y);
cells.get(x).add(cell);
try {
cells[x][y].addTexture(textureSheet.getTexture(0), 0);
cells.get(x).get(y).addTexture(textureSheet.getTexture(0), 0);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -312,9 +322,9 @@ public class Scene extends JComponent implements Constants {
if (x == 0 && y == 0) {
// Top left corner
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
try {
cells[x][y].addTexture(textureSheet.getTexture(33), 33);
cells.get(x).get(y).addTexture(textureSheet.getTexture(33), 33);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -322,9 +332,9 @@ public class Scene extends JComponent implements Constants {
}
else if (x == HORIZONTAL_CELLS - 1 && y == 0) {
// Top right corner
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
try {
cells[x][y].addTexture(textureSheet.getTexture(37), 37);
cells.get(x).get(y).addTexture(textureSheet.getTexture(37), 37);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -332,9 +342,9 @@ public class Scene extends JComponent implements Constants {
}
else if (x == 0 && y == VERTICAL_CELLS - 1) {
// Bottom left corner
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
try {
cells[x][y].addTexture(textureSheet.getTexture(97), 97);
cells.get(x).get(y).addTexture(textureSheet.getTexture(97), 97);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -342,9 +352,9 @@ public class Scene extends JComponent implements Constants {
}
else if (x == HORIZONTAL_CELLS - 1 && y == VERTICAL_CELLS - 1) {
// Bottom right corner
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
try {
cells[x][y].addTexture(textureSheet.getTexture(101), 101);
cells.get(x).get(y).addTexture(textureSheet.getTexture(101), 101);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -352,12 +362,12 @@ public class Scene extends JComponent implements Constants {
}
else if (y == 0) {
// Top wall
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
if (x == 1) {
// Left door frame
try {
cells[x][y].addTexture(textureSheet.getTexture(144), 144);
cells[x][y].addTexture(textureSheet.getTexture(192), 192);
cells.get(x).get(y).addTexture(textureSheet.getTexture(144), 144);
cells.get(x).get(y).addTexture(textureSheet.getTexture(192), 192);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -366,7 +376,7 @@ public class Scene extends JComponent implements Constants {
else if (x == 2) {
// Door
try {
cells[x][y].addTexture(textureSheet.getTexture(145), 145);
cells.get(x).get(y).addTexture(textureSheet.getTexture(145), 145);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -375,8 +385,8 @@ public class Scene extends JComponent implements Constants {
else if (x == 3) {
// Right door frame
try {
cells[x][y].addTexture(textureSheet.getTexture(146), 146);
cells[x][y].addTexture(textureSheet.getTexture(194), 194);
cells.get(x).get(y).addTexture(textureSheet.getTexture(146), 146);
cells.get(x).get(y).addTexture(textureSheet.getTexture(194), 194);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -385,7 +395,7 @@ public class Scene extends JComponent implements Constants {
else if (x == 8) {
// Broken wall piece
try {
cells[x][y].addTexture(textureSheet.getTexture(105), 105);
cells.get(x).get(y).addTexture(textureSheet.getTexture(105), 105);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -393,8 +403,8 @@ public class Scene extends JComponent implements Constants {
}
else if (x % 2 == 0) {
try {
cells[x][y].addTexture(textureSheet.getTexture(34), 34);
cells[x][y].addTexture(textureSheet.getTexture(222), 222);
cells.get(x).get(y).addTexture(textureSheet.getTexture(34), 34);
cells.get(x).get(y).addTexture(textureSheet.getTexture(222), 222);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -402,7 +412,7 @@ public class Scene extends JComponent implements Constants {
}
else {
try {
cells[x][y].addTexture(textureSheet.getTexture(35), 35);
cells.get(x).get(y).addTexture(textureSheet.getTexture(35), 35);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -411,11 +421,11 @@ public class Scene extends JComponent implements Constants {
}
else if (x == 0) {
// Left wall
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
if (y % 2 == 0) {
try {
cells[x][y].addTexture(textureSheet.getTexture(49), 49);
cells[x][y].addTexture(textureSheet.getTexture(255), 255);
cells.get(x).get(y).addTexture(textureSheet.getTexture(49), 49);
cells.get(x).get(y).addTexture(textureSheet.getTexture(255), 255);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -423,7 +433,7 @@ public class Scene extends JComponent implements Constants {
}
else {
try {
cells[x][y].addTexture(textureSheet.getTexture(65), 65);
cells.get(x).get(y).addTexture(textureSheet.getTexture(65), 65);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -432,11 +442,11 @@ public class Scene extends JComponent implements Constants {
}
else if (x == HORIZONTAL_CELLS - 1) {
// Right wall
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
if (y % 2 == 0) {
try {
cells[x][y].addTexture(textureSheet.getTexture(53), 53);
cells[x][y].addTexture(textureSheet.getTexture(238), 238);
cells.get(x).get(y).addTexture(textureSheet.getTexture(53), 53);
cells.get(x).get(y).addTexture(textureSheet.getTexture(238), 238);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -444,7 +454,7 @@ public class Scene extends JComponent implements Constants {
}
else {
try {
cells[x][y].addTexture(textureSheet.getTexture(69), 69);
cells.get(x).get(y).addTexture(textureSheet.getTexture(69), 69);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -453,11 +463,11 @@ public class Scene extends JComponent implements Constants {
}
else if (y == VERTICAL_CELLS - 1) {
// Bottom wall
cells[x][y].setObject(new Obstacle(this, cells[x][y]));
cells.get(x).get(y).setObject(new Obstacle(this, cells.get(x).get(y)));
if (x % 2 == 0) {
try {
cells[x][y].addTexture(textureSheet.getTexture(98), 98);
cells[x][y].addTexture(textureSheet.getTexture(207), 207);
cells.get(x).get(y).addTexture(textureSheet.getTexture(98), 98);
cells.get(x).get(y).addTexture(textureSheet.getTexture(207), 207);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -465,18 +475,13 @@ public class Scene extends JComponent implements Constants {
}
else {
try {
cells[x][y].addTexture(textureSheet.getTexture(99), 99);
cells.get(x).get(y).addTexture(textureSheet.getTexture(99), 99);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
// The player starts at the door
/*if (x == 2 && y == 1) {
celdas[x][y].setObject(new Player(this, celdas[x][y]));
}*/
}
}
}
@ -493,7 +498,7 @@ public class Scene extends JComponent implements Constants {
*
* @return Returns a matrix of the cells of the game
*/
public Cell[][] getCells() {
public CopyOnWriteArrayList<CopyOnWriteArrayList<Cell>> getCells() {
return cells;
}
@ -514,9 +519,9 @@ public class Scene extends JComponent implements Constants {
*/
@Override
public void update(Graphics g) {
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
cells[i][j].paintComponent(g);
for (int x = 0; x < HORIZONTAL_CELLS; x++) {
for (int y = 0; y < VERTICAL_CELLS; y++) {
cells.get(x).get(y).paintComponent(g);
}
}
}
@ -568,9 +573,9 @@ public class Scene extends JComponent implements Constants {
*/
public void openDoor(boolean doorOpen) {
if (!doorOpen && isDoorOpen()) {
cells[2][0].setObject(new Obstacle(this, cells[2][0]));
cells.get(2).get(0).setObject(new Obstacle(this, cells.get(2).get(0)));
try {
cells[2][0].addTexture(textureSheet.getTexture(193), 193);
cells.get(2).get(0).addTexture(textureSheet.getTexture(193), 193);
}
catch (SheetException e) {
logger.warning(e.getMessage());
@ -579,8 +584,8 @@ public class Scene extends JComponent implements Constants {
playDoorSound();
}
else if (doorOpen && !isDoorOpen()) {
cells[2][0].removeTexture(193);
cells[2][0].setObject(null);
cells.get(2).get(0).removeTexture(193);
cells.get(2).get(0).setObject(null);
this.doorOpen = true;
playDoorSound();
}

View File

@ -133,7 +133,7 @@ public class EnemyAI extends AI implements Runnable, Constants {
*/
private void moveUp(State current) {
if (current.getY() > 0) {
Object object = scene.getCells()[current.getX()][current.getY() - 1].getObject();
Object object = scene.getCells().get(current.getX()).get(current.getY() - 1).getObject();
if (object == null || object instanceof Player) {
State next = new State(current.getX(), current.getY() - 1, State.Type.UP, current, current.getImportance());
move(next);
@ -148,7 +148,7 @@ public class EnemyAI extends AI implements Runnable, Constants {
*/
private void moveDown(State current) {
if (current.getY() < VERTICAL_CELLS - 1) {
Object object = scene.getCells()[current.getX()][current.getY() + 1].getObject();
Object object = scene.getCells().get(current.getX()).get(current.getY() + 1).getObject();
if (object == null || object instanceof Player) {
State next = new State(current.getX(), current.getY() + 1, State.Type.DOWN, current, current.getImportance());
move(next);
@ -163,7 +163,7 @@ public class EnemyAI extends AI implements Runnable, Constants {
*/
private void moveLeft(State current) {
if (current.getX() > 0) {
Object object = scene.getCells()[current.getX() - 1][current.getY()].getObject();
Object object = scene.getCells().get(current.getX() - 1).get(current.getY()).getObject();
if (object == null || object instanceof Player) {
State next = new State(current.getX() - 1, current.getY(), State.Type.LEFT, current, current.getImportance());
move(next);
@ -178,7 +178,7 @@ public class EnemyAI extends AI implements Runnable, Constants {
*/
private void moveRight(State current) {
if (current.getX() < HORIZONTAL_CELLS - 1) {
Object object = scene.getCells()[current.getX() + 1][current.getY()].getObject();
Object object = scene.getCells().get(current.getX() + 1).get(current.getY()).getObject();
if (object == null || object instanceof Player) {
State next = new State(current.getX() + 1, current.getY(), State.Type.RIGHT, current, current.getImportance());
move(next);

View File

@ -205,16 +205,16 @@ public interface PlayerAI extends Runnable, Constants {
default State.Type getOpenSpaceAroundPlayer(Scene scene) {
Player player = scene.getCanvas().getPlayer();
List<State.Type> openSpaces = new ArrayList<>();
if (player.getCell().getX() > 0 && scene.getCells()[player.getCell().getX() - 1][player.getCell().getY()].getObject() == null) {
if (player.getCell().getX() > 0 && scene.getCells().get(player.getCell().getX() - 1).get(player.getCell().getY()).getObject() == null) {
openSpaces.add(State.Type.LEFT);
}
if (player.getCell().getX() < HORIZONTAL_CELLS - 1 && scene.getCells()[player.getCell().getX() + 1][player.getCell().getY()].getObject() == null) {
if (player.getCell().getX() < HORIZONTAL_CELLS - 1 && scene.getCells().get(player.getCell().getX() + 1).get(player.getCell().getY()).getObject() == null) {
openSpaces.add(State.Type.RIGHT);
}
if (player.getCell().getY() > 0 && scene.getCells()[player.getCell().getX()][player.getCell().getY() - 1].getObject() == null) {
if (player.getCell().getY() > 0 && scene.getCells().get(player.getCell().getX()).get(player.getCell().getY() - 1).getObject() == null) {
openSpaces.add(State.Type.UP);
}
if (player.getCell().getY() < VERTICAL_CELLS - 1 && scene.getCells()[player.getCell().getX()][player.getCell().getY() + 1].getObject() == null) {
if (player.getCell().getY() < VERTICAL_CELLS - 1 && scene.getCells().get(player.getCell().getX()).get(player.getCell().getY() + 1).getObject() == null) {
openSpaces.add(State.Type.DOWN);
}

View File

@ -140,7 +140,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
*/
private void moveUp(State current, State goal) {
if (current.getY() > 0) {
if (scene.getCells()[current.getX()][current.getY() - 1].getObject() == null) {
if (scene.getCells().get(current.getX()).get(current.getY() - 1).getObject() == null) {
State next = new State(current.getX(), current.getY() - 1, State.Type.UP, current, 0);
move(current, next, goal);
}
@ -155,7 +155,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
*/
private void moveDown(State current, State goal) {
if (current.getY() < VERTICAL_CELLS - 1) {
if (scene.getCells()[current.getX()][current.getY() + 1].getObject() == null) {
if (scene.getCells().get(current.getX()).get(current.getY() + 1).getObject() == null) {
State next = new State(current.getX(), current.getY() + 1, State.Type.DOWN, current, 0);
move(current, next, goal);
}
@ -170,7 +170,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
*/
private void moveLeft(State current, State goal) {
if (current.getX() > 0) {
if (scene.getCells()[current.getX() - 1][current.getY()].getObject() == null) {
if (scene.getCells().get(current.getX() - 1).get(current.getY()).getObject() == null) {
State next = new State(current.getX() - 1, current.getY(), State.Type.LEFT, current, 0);
move(current, next, goal);
}
@ -185,7 +185,7 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
*/
private void moveRight(State current, State goal) {
if (current.getX() < HORIZONTAL_CELLS - 1) {
if (scene.getCells()[current.getX() + 1][current.getY()].getObject() == null) {
if (scene.getCells().get(current.getX() + 1).get(current.getY()).getObject() == null) {
State next = new State(current.getX() + 1, current.getY(), State.Type.RIGHT, current, 0);
move(current, next, goal);
}
@ -216,119 +216,119 @@ public class PlayerAStarAI extends AI implements PlayerAI, Constants {
if (enemyCost.getLevel() >= EnemyCost.DIRECT.getLevel()) {
// The enemy
if (scene.getCells()[state.getX()][state.getY()].getObject() instanceof Enemy) {
if (scene.getCells().get(state.getX()).get(state.getY()).getObject() instanceof Enemy) {
return EnemyCost.DIRECT.getCost();
}
}
if (enemyCost.getLevel() >= EnemyCost.DIRECT_SIDES.getLevel()) {
// Left
if (state.getX() > 0 && scene.getCells()[state.getX() - 1][state.getY()].getObject() instanceof Enemy) {
if (state.getX() > 0 && scene.getCells().get(state.getX() - 1).get(state.getY()).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_SIDES.getCost();
}
// Right
else if (state.getX() < HORIZONTAL_CELLS - 1 && scene.getCells()[state.getX() + 1][state.getY()].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 1 && scene.getCells().get(state.getX() + 1).get(state.getY()).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_SIDES.getCost();
}
// Up
else if (state.getY() > 0 && scene.getCells()[state.getX()][state.getY() - 1].getObject() instanceof Enemy) {
else if (state.getY() > 0 && scene.getCells().get(state.getX()).get(state.getY() - 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_SIDES.getCost();
}
// Down
else if (state.getY() < VERTICAL_CELLS - 1 && scene.getCells()[state.getX()][state.getY() + 1].getObject() instanceof Enemy) {
else if (state.getY() < VERTICAL_CELLS - 1 && scene.getCells().get(state.getX()).get(state.getY() + 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_SIDES.getCost();
}
}
if (enemyCost.getLevel() >= EnemyCost.DIRECT_CORNERS.getLevel()) {
// Upper left corner
if (state.getX() > 0 && state.getY() > 0 && scene.getCells()[state.getX() - 1][state.getY() - 1].getObject() instanceof Enemy) {
if (state.getX() > 0 && state.getY() > 0 && scene.getCells().get(state.getX() - 1).get(state.getY() - 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_CORNERS.getCost();
}
// Upper right corner
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() > 0 && scene.getCells()[state.getX() + 1][state.getY() - 1].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() > 0 && scene.getCells().get(state.getX() + 1).get(state.getY() - 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_CORNERS.getCost();
}
// Lower left corner
else if (state.getX() > 0 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells()[state.getX() - 1][state.getY() + 1].getObject() instanceof Enemy) {
else if (state.getX() > 0 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells().get(state.getX() - 1).get(state.getY() + 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_CORNERS.getCost();
}
// Lower right corner
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells()[state.getX() + 1][state.getY() + 1].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells().get(state.getX() + 1).get(state.getY() + 1).getObject() instanceof Enemy) {
return EnemyCost.DIRECT_CORNERS.getCost();
}
}
if (enemyCost.getLevel() >= EnemyCost.FAR_SIDES.getLevel()) {
// Left
if (state.getX() > 1 && scene.getCells()[state.getX() - 2][state.getY()].getObject() instanceof Enemy) {
if (state.getX() > 1 && scene.getCells().get(state.getX() - 2).get(state.getY()).getObject() instanceof Enemy) {
return EnemyCost.FAR_SIDES.getCost();
}
// Right
else if (state.getX() < HORIZONTAL_CELLS - 2 && scene.getCells()[state.getX() + 2][state.getY()].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 2 && scene.getCells().get(state.getX() + 2).get(state.getY()).getObject() instanceof Enemy) {
return EnemyCost.FAR_SIDES.getCost();
}
// Up
else if (state.getY() > 1 && scene.getCells()[state.getX()][state.getY() - 2].getObject() instanceof Enemy) {
else if (state.getY() > 1 && scene.getCells().get(state.getX()).get(state.getY() - 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_SIDES.getCost();
}
// Down
else if (state.getY() < VERTICAL_CELLS - 2 && scene.getCells()[state.getX()][state.getY() + 2].getObject() instanceof Enemy) {
else if (state.getY() < VERTICAL_CELLS - 2 && scene.getCells().get(state.getX()).get(state.getY() + 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_SIDES.getCost();
}
}
if (enemyCost.getLevel() >= EnemyCost.FAR_CORNERS.getLevel()) {
// Upper left corner
if (state.getX() > 1 && state.getY() > 0 && scene.getCells()[state.getX() - 2][state.getY() - 1].getObject() instanceof Enemy) {
if (state.getX() > 1 && state.getY() > 0 && scene.getCells().get(state.getX() - 2).get(state.getY() - 1).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() > 1 && state.getY() > 1 && scene.getCells()[state.getX() - 2][state.getY() - 2].getObject() instanceof Enemy) {
else if (state.getX() > 1 && state.getY() > 1 && scene.getCells().get(state.getX() - 2).get(state.getY() - 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() > 0 && state.getY() > 1 && scene.getCells()[state.getX() - 1][state.getY() - 2].getObject() instanceof Enemy) {
else if (state.getX() > 0 && state.getY() > 1 && scene.getCells().get(state.getX() - 1).get(state.getY() - 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
// Upper right corner
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() > 0 && scene.getCells()[state.getX() + 2][state.getY() - 1].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() > 0 && scene.getCells().get(state.getX() + 2).get(state.getY() - 1).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() > 1 && scene.getCells()[state.getX() + 2][state.getY() - 2].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() > 1 && scene.getCells().get(state.getX() + 2).get(state.getY() - 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() > 1 && scene.getCells()[state.getX() + 1][state.getY() - 2].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() > 1 && scene.getCells().get(state.getX() + 1).get(state.getY() - 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
// Lower left corner
else if (state.getX() > 1 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells()[state.getX() - 2][state.getY() + 1].getObject() instanceof Enemy) {
else if (state.getX() > 1 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells().get(state.getX() - 2).get(state.getY() + 1).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() > 1 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells()[state.getX() - 2][state.getY() + 2].getObject() instanceof Enemy) {
else if (state.getX() > 1 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells().get(state.getX() - 2).get(state.getY() + 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() > 0 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells()[state.getX() - 1][state.getY() + 2].getObject() instanceof Enemy) {
else if (state.getX() > 0 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells().get(state.getX() - 1).get(state.getY() + 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
// Lower right corner
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells()[state.getX() + 2][state.getY() + 1].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() < VERTICAL_CELLS - 1 && scene.getCells().get(state.getX() + 2).get(state.getY() + 1).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells()[state.getX() + 2][state.getY() + 2].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 2 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells().get(state.getX() + 2).get(state.getY() + 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells()[state.getX() + 1][state.getY() + 2].getObject() instanceof Enemy) {
else if (state.getX() < HORIZONTAL_CELLS - 1 && state.getY() < VERTICAL_CELLS - 2 && scene.getCells().get(state.getX() + 1).get(state.getY() + 2).getObject() instanceof Enemy) {
return EnemyCost.FAR_CORNERS.getCost();
}
}

View File

@ -138,7 +138,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
*/
private void moveUp(State current) {
if (current.getY() > 0) {
if (scene.getCells()[current.getX()][current.getY() - 1].getObject() == null) {
if (scene.getCells().get(current.getX()).get(current.getY() - 1).getObject() == null) {
State next = new State(current.getX(), current.getY() - 1, State.Type.UP, current, -1);
move(next);
}
@ -152,7 +152,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
*/
private void moveDown(State current) {
if (current.getY() < VERTICAL_CELLS - 1) {
if (scene.getCells()[current.getX()][current.getY() + 1].getObject() == null) {
if (scene.getCells().get(current.getX()).get(current.getY() + 1).getObject() == null) {
State next = new State(current.getX(), current.getY() + 1, State.Type.DOWN, current, -1);
move(next);
}
@ -166,7 +166,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
*/
private void moveLeft(State current) {
if (current.getX() > 0) {
if (scene.getCells()[current.getX() - 1][current.getY()].getObject() == null) {
if (scene.getCells().get(current.getX() - 1).get(current.getY()).getObject() == null) {
State next = new State(current.getX() - 1, current.getY(), State.Type.LEFT, current, -1);
move(next);
}
@ -180,7 +180,7 @@ public class PlayerBreadthFirstAI extends AI implements PlayerAI, Constants {
*/
private void moveRight(State current) {
if (current.getX() < HORIZONTAL_CELLS - 1) {
if (scene.getCells()[current.getX() + 1][current.getY()].getObject() == null) {
if (scene.getCells().get(current.getX() + 1).get(current.getY()).getObject() == null) {
State next = new State(current.getX() + 1, current.getY(), State.Type.RIGHT, current, -1);
move(next);
}

View File

@ -23,6 +23,7 @@ import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
/**
@ -44,20 +45,20 @@ public class Json implements Constants {
/**
* Export the game cells to a JSON ready object then write it to a file
*
* @param celdas The cells of the scene to export
* @param cells The cells of the scene to export
*/
public void exportScene(Cell[][] celdas) {
JsonCell[][] jsonCells = new JsonCell[celdas.length][celdas[0].length];
for (int x = 0; x < celdas.length; x++) {
for (int y = 0; y < celdas[x].length; y++) {
public void exportScene(CopyOnWriteArrayList<CopyOnWriteArrayList<Cell>> cells) {
JsonCell[][] jsonCells = new JsonCell[cells.size()][cells.get(0).size()];
for (int x = 0; x < cells.size(); x++) {
for (int y = 0; y < cells.get(x).size(); y++) {
jsonCells[x][y] = new JsonCell();
if (celdas[x][y].getObject() != null) {
jsonCells[x][y].type = celdas[x][y].getObject().getClass().getName();
if (cells.get(x).get(y).getObject() != null) {
jsonCells[x][y].type = cells.get(x).get(y).getObject().getClass().getName();
}
else {
jsonCells[x][y].type = "null";
}
jsonCells[x][y].textures = celdas[x][y].getTextureNumbers();
jsonCells[x][y].textures = cells.get(x).get(y).getTextureNumbers();
}
}
writeScene(jsonCells);

View File

@ -152,11 +152,11 @@ public class Enemy extends Object implements Constants {
public boolean moveUp() {
int x = getX();
int y = getY();
if (y > 0 && getScene().getCells()[x][y - 1].getObject() == null) {
if (y > 0 && getScene().getCells().get(x).get(y - 1).getObject() == null) {
super.moveUp();
getLogger().info("Move up to x: " + x + " y: " + y);
}
else if (y > 0 && getScene().getCells()[x][y - 1].getObject() instanceof Player) {
else if (y > 0 && getScene().getCells().get(x).get(y - 1).getObject() instanceof Player) {
if (changeDirection(Animation.Direction.UP)) {
try {
getAnimation().getNextFrame();
@ -190,11 +190,11 @@ public class Enemy extends Object implements Constants {
public boolean moveDown() {
int x = getX();
int y = getY();
if (y < (VERTICAL_CELLS) - 1 && getScene().getCells()[x][y + 1].getObject() == null) {
if (y < (VERTICAL_CELLS) - 1 && getScene().getCells().get(x).get(y + 1).getObject() == null) {
super.moveDown();
getLogger().info("Move down to x: " + x + " y: " + y);
}
else if (y < (VERTICAL_CELLS - 1) && getScene().getCells()[x][y + 1].getObject() instanceof Player) {
else if (y < (VERTICAL_CELLS - 1) && getScene().getCells().get(x).get(y + 1).getObject() instanceof Player) {
if (changeDirection(Animation.Direction.DOWN)) {
try {
getAnimation().getNextFrame();
@ -228,11 +228,11 @@ public class Enemy extends Object implements Constants {
public boolean moveLeft() {
int x = getX();
int y = getY();
if (x > 0 && getScene().getCells()[x - 1][y].getObject() == null) {
if (x > 0 && getScene().getCells().get(x - 1).get(y).getObject() == null) {
super.moveLeft();
getLogger().info("Move left to x: " + x + " y: " + y);
}
else if (x > 0 && getScene().getCells()[x - 1][y].getObject() instanceof Player) {
else if (x > 0 && getScene().getCells().get(x - 1).get(y).getObject() instanceof Player) {
if (changeDirection(Animation.Direction.LEFT)) {
try {
getAnimation().getNextFrame();
@ -266,11 +266,11 @@ public class Enemy extends Object implements Constants {
public boolean moveRight() {
int x = getX();
int y = getY();
if (x < (HORIZONTAL_CELLS - 1) && getScene().getCells()[x + 1][y].getObject() == null) {
if (x < (HORIZONTAL_CELLS - 1) && getScene().getCells().get(x + 1).get(y).getObject() == null) {
super.moveRight();
getLogger().info("Move right to x: " + x + " y: " + y);
}
else if (x < (HORIZONTAL_CELLS - 1) && getScene().getCells()[x + 1][y].getObject() instanceof Player) {
else if (x < (HORIZONTAL_CELLS - 1) && getScene().getCells().get(x + 1).get(y).getObject() instanceof Player) {
if (changeDirection(Animation.Direction.RIGHT)) {
try {
getAnimation().getNextFrame();
@ -310,7 +310,7 @@ public class Enemy extends Object implements Constants {
getScene().getCanvas().getPlayer().loseHealth(2);
try {
getScene().getCells()[x][y].addTexture(getScene().getTextureSheet().getTexture(12), 12);
getScene().getCells().get(x).get(y).addTexture(getScene().getTextureSheet().getTexture(12), 12);
}
catch (SheetException e) {
getLogger().warning(e.getMessage());

View File

@ -290,7 +290,7 @@ public class Object implements Runnable, Constants {
*/
protected boolean moveUp() {
getCell().setObject(null);
setCell(getScene().getCells()[x][y - 1]);
setCell(getScene().getCells().get(x).get(y - 1));
getCell().setObject(this);
if (changeDirection(Animation.Direction.UP)) {
@ -313,7 +313,7 @@ public class Object implements Runnable, Constants {
*/
protected boolean moveDown() {
getCell().setObject(null);
setCell(getScene().getCells()[x][y + 1]);
setCell(getScene().getCells().get(x).get(y + 1));
getCell().setObject(this);
if (changeDirection(Animation.Direction.DOWN)) {
@ -336,7 +336,7 @@ public class Object implements Runnable, Constants {
*/
protected boolean moveLeft() {
getCell().setObject(null);
setCell(getScene().getCells()[x - 1][y]);
setCell(getScene().getCells().get(x - 1).get(y));
getCell().setObject(this);
if (changeDirection(Animation.Direction.LEFT)) {
@ -359,7 +359,7 @@ public class Object implements Runnable, Constants {
*/
protected boolean moveRight() {
getCell().setObject(null);
setCell(getScene().getCells()[x + 1][y]);
setCell(getScene().getCells().get(x + 1).get(y));
getCell().setObject(this);
if (changeDirection(Animation.Direction.RIGHT)) {

View File

@ -146,9 +146,9 @@ public class Player extends Object implements Constants {
}
}
else if (y > 0) {
Object type = getScene().getCells()[x][y - 1].getObject();
Object type = getScene().getCells().get(x).get(y - 1).getObject();
if (type == null) {
Object typeBottom = getScene().getCells()[x][y - 1].getObjectOnBottom();
Object typeBottom = getScene().getCells().get(x).get(y - 1).getObjectOnBottom();
if (typeBottom instanceof Key) {
for (Key key : getScene().getCanvas().getKeys()) {
if (key.checkPosition(x, y - 1)) {
@ -201,9 +201,9 @@ public class Player extends Object implements Constants {
int y = getY();
getLogger().info("Down key pressed");
if (y < (VERTICAL_CELLS - 1)) {
Object type = getScene().getCells()[x][y + 1].getObject();
Object type = getScene().getCells().get(x).get(y + 1).getObject();
if (type == null) {
Object typeBottom = getScene().getCells()[x][y + 1].getObjectOnBottom();
Object typeBottom = getScene().getCells().get(x).get(y + 1).getObjectOnBottom();
if (typeBottom instanceof Key) {
for (Key key : getScene().getCanvas().getKeys()) {
if (key.checkPosition(x, y + 1)) {
@ -256,9 +256,9 @@ public class Player extends Object implements Constants {
int y = getY();
getLogger().info("Left key pressed");
if (x > 0) {
Object type = getScene().getCells()[x - 1][y].getObject();
Object type = getScene().getCells().get(x - 1).get(y).getObject();
if (type == null) {
Object typeBottom = getScene().getCells()[x - 1][y].getObjectOnBottom();
Object typeBottom = getScene().getCells().get(x - 1).get(y).getObjectOnBottom();
if (typeBottom instanceof Key) {
for (Key key : getScene().getCanvas().getKeys()) {
if (key.checkPosition(x - 1, y)) {
@ -311,9 +311,9 @@ public class Player extends Object implements Constants {
int y = getY();
getLogger().info("Right key pressed");
if (x < (HORIZONTAL_CELLS - 1)) {
Object type = getScene().getCells()[x + 1][y].getObject();
Object type = getScene().getCells().get(x + 1).get(y).getObject();
if (type == null) {
Object typeBottom = getScene().getCells()[x + 1][y].getObjectOnBottom();
Object typeBottom = getScene().getCells().get(x + 1).get(y).getObjectOnBottom();
if (typeBottom instanceof Key) {
for (Key key : getScene().getCanvas().getKeys()) {
if (key.checkPosition(x + 1, y)) {
@ -379,7 +379,7 @@ public class Player extends Object implements Constants {
getLogger().info("Space bar pressed");
if (y > 0) {
if (getAnimation().getCurrentDirection() == Animation.Direction.UP) {
if (getScene().getCells()[x][y - 1].getObject() instanceof Chest) {
if (getScene().getCells().get(x).get(y - 1).getObject() instanceof Chest) {
if (hasKey()) {
getLogger().info("Player opened chest");