Change from simple-json library to gson library

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-22 14:23:42 -03:00
parent 5f3a2df8cf
commit c1fde9eded
10 changed files with 1585 additions and 1599 deletions

View File

@ -3,10 +3,10 @@
<output-path>$PROJECT_DIR$/out/artifacts/Game_jar</output-path>
<root id="archive" name="Game.jar">
<element id="module-output" name="Game" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/json-simple-3.1.0.jar" path-in-jar="/" />
<element id="directory" name="javadoc">
<element id="dir-copy" path="$PROJECT_DIR$/javadoc" />
</element>
<element id="extracted-dir" path="$PROJECT_DIR$/lib/gson-2.8.5.jar" path-in-jar="/" />
</root>
</artifact>
</component>

BIN
lib/gson-2.8.5.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@ -49,9 +49,13 @@ public class Celda extends JComponent implements Constantes {
*/
private Animation animation = null;
/**
* The tiles to show in this cell
* The textures to show in this cell
*/
private ArrayList<BufferedImage> tiles = new ArrayList<>();
private ArrayList<BufferedImage> textures = new ArrayList<>();
/**
* The texture numbers
*/
private ArrayList<Integer> textureNumbers = new ArrayList<>();
/**
* The logger
*/
@ -133,8 +137,19 @@ public class Celda extends JComponent implements Constantes {
this.animation = animation;
}
public void addTile(BufferedImage tile) {
tiles.add(tile);
/**
* Add a texture to the texture list
*
* @param texture The new texture
* @param textureNumber The texture's number
*/
public void addTexture(BufferedImage texture, int textureNumber) {
textures.add(texture);
textureNumbers.add(textureNumber);
}
public ArrayList<Integer> getTextureNumbers() {
return textureNumbers;
}
/**
@ -172,7 +187,7 @@ public class Celda extends JComponent implements Constantes {
// Set the text font
g.setFont(new Font("monospaced", Font.BOLD, 10));
for (BufferedImage tile : tiles) {
for (BufferedImage tile : textures) {
if (tile != null) {
g.drawImage(tile, x, y, null);
}

View File

@ -34,7 +34,6 @@ public interface Constantes {
* The name of the game
*/
String TITLE = "La Aventura de Azaraka";
/**
* The level of logs to record
*/
@ -48,6 +47,7 @@ public interface Constantes {
Level SOUND_LOG_LEVEL = Level.WARNING;
Level IMAGE_LOG_LEVEL = Level.WARNING;
Level CELDA_LOG_LEVEL = Level.WARNING;
Level JSON_LOG_LEVEL = Level.WARNING;
/**
* Use a global log if true or individual logs if false
*/
@ -56,7 +56,6 @@ public interface Constantes {
* Append to the logs if true or make a new log if false
*/
boolean APPEND_LOGS = false;
/**
* The size in pixels of the cells
*/
@ -69,12 +68,9 @@ public interface Constantes {
* The number of cells to draw vertically
*/
int VERTICAL_CELLS = 10;
int TOP_MARGIN = 40;
int LEFT_MARGIN = 40;
Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
/**
* The letter that represents the end
*/
@ -90,8 +86,6 @@ public interface Constantes {
int PLAYER_START_X = 2;
int PLAYER_START_Y = 1;
int FONT_SIZE = 12;
Font BOLD_FONT = new Font("monospaced", Font.BOLD, FONT_SIZE);
Font FONT = new Font("monospaced", Font.PLAIN, FONT_SIZE);
int MINIMUM_SPEED = 100;
int MAXIMUM_SPEED = 500;
int DEFAULT_SPEED = 100;
@ -99,7 +93,11 @@ public interface Constantes {
int MAXIMUM_VOLUME = 100;
int DEFAULT_VOLUME = 100;
boolean GENERATE_SCENE = false;
boolean EXPORT_SCENE = false;
boolean EXPORT_SCENE = true;
Font BOLD_FONT = new Font("monospaced", Font.BOLD, FONT_SIZE);
Font FONT = new Font("monospaced", Font.PLAIN, FONT_SIZE);
boolean PRETTY_JSON = false;
/**
* Generate a random number between given min and max

View File

@ -15,18 +15,21 @@
package cl.cromer.game;
import cl.cromer.game.json.Cell;
import cl.cromer.game.json.Json;
import cl.cromer.game.sound.Sound;
import cl.cromer.game.sound.SoundException;
import cl.cromer.game.sprite.*;
import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.*;
import java.math.BigDecimal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Logger;
@ -83,9 +86,11 @@ public class Escenario extends JComponent implements Constantes {
celdas = new Celda[HORIZONTAL_CELLS][VERTICAL_CELLS];
StringBuilder stringBuilder;
if (!GENERATE_SCENE) {
stringBuilder = new StringBuilder();
if (GENERATE_SCENE) {
generateScene();
}
else {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = getClass().getResourceAsStream("/res/scene.json");
try {
@ -98,30 +103,57 @@ public class Escenario extends JComponent implements Constantes {
catch (IOException e) {
logger.warning(e.getMessage());
}
}
int cellCount = 0;
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
celdas[i][j] = new Celda((i * CELL_PIXELS) + LEFT_MARGIN, (j * CELL_PIXELS) + TOP_MARGIN);
if (GENERATE_SCENE) {
generateScene(i, j);
}
else {
loadScene(i, j, stringBuilder, cellCount);
cellCount++;
}
}
loadScene(stringBuilder.toString());
}
if (EXPORT_SCENE) {
exportScene();
Json json = new Json();
json.exportScene(celdas);
}
generateRandomObjects();
}
/**
* Load the scene from a JSON file
*
* @param json The JSON string to load
*/
private void loadScene(String json) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Cell[][] cells = gson.fromJson(json, Cell[][].class);
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
celdas[i][j] = new Celda((i * CELL_PIXELS) + LEFT_MARGIN, (j * CELL_PIXELS) + TOP_MARGIN);
celdas[i][j].setType(cells[i][j].type);
if (cells[i][j].type == Celda.Type.PLAYER) {
celdas[i][j].setAnimation(sprites.get(SpriteType.PLAYER));
}
else if (cells[i][j].type == Celda.Type.ENEMY) {
celdas[i][j].setAnimation(sprites.get(SpriteType.ENEMY));
}
else if (cells[i][j].type == Celda.Type.CHEST) {
celdas[i][j].setAnimation(sprites.get(SpriteType.CHEST));
}
for (int k = 0; k < cells[i][j].textures.size(); k++) {
try {
celdas[i][j].addTexture(textureSheet.getTexture(cells[i][j].textures.get(k)), cells[i][j].textures.get(k));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
}
}
/**
* Generate random objects in the scene
*/
private void generateRandomObjects() {
final int cells = (HORIZONTAL_CELLS * VERTICAL_CELLS);
final int obstacles = (int) Math.floor((double) cells * 0.05);
@ -176,7 +208,7 @@ public class Escenario extends JComponent implements Constantes {
break;
case OBSTACLE:
try {
celdas[x][y].addTile(textureSheet.getTile(30));
celdas[x][y].addTexture(textureSheet.getTexture(30), 30);
}
catch (SheetException e) {
e.printStackTrace();
@ -188,430 +220,223 @@ public class Escenario extends JComponent implements Constantes {
/**
* Generate the scene manually without the JSON file
*
* @param x The cell x position
* @param y The cell y position
*/
private void generateScene(int x, int y) {
logger.info("Generate cell x: " + x + " y: " + y + " manually");
try {
celdas[x][y].addTile(textureSheet.getTile(0));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
private void generateScene() {
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) + LEFT_MARGIN, (y * CELL_PIXELS) + TOP_MARGIN);
try {
celdas[x][y].addTexture(textureSheet.getTexture(0), 0);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
if (x == 0 && y == 0) {
// Top left corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTile(textureSheet.getTile(33));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 4 && y == 3) {
// Obstacle on floor
try {
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].addTile(textureSheet.getTile(30));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 6 && y == 6) {
// Blood on floor
try {
celdas[x][y].addTile(textureSheet.getTile(12));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == HORIZONTAL_CELLS - 1 && y == 0) {
// Top right corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTile(textureSheet.getTile(37));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 0 && y == VERTICAL_CELLS - 1) {
// Bottom left corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTile(textureSheet.getTile(97));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == HORIZONTAL_CELLS - 1 && y == VERTICAL_CELLS - 1) {
// Bottom right corner
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTile(textureSheet.getTile(101));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (y == 0) {
// Top wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (x == 1) {
// Left door frame
try {
celdas[x][y].addTile(textureSheet.getTile(144));
celdas[x][y].addTile(textureSheet.getTile(192));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 2) {
// Door
try {
celdas[x][y].addTile(textureSheet.getTile(145));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 3) {
// Right door frame
try {
celdas[x][y].addTile(textureSheet.getTile(146));
celdas[x][y].addTile(textureSheet.getTile(194));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x == 8) {
// Broken wall piece
try {
celdas[x][y].addTile(textureSheet.getTile(105));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (x % 2 == 0) {
try {
celdas[x][y].addTile(textureSheet.getTile(34));
celdas[x][y].addTile(textureSheet.getTile(222));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
try {
celdas[x][y].addTile(textureSheet.getTile(35));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
else if (x == 0) {
// Left wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (y % 2 == 0) {
try {
celdas[x][y].addTile(textureSheet.getTile(49));
celdas[x][y].addTile(textureSheet.getTile(255));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
try {
celdas[x][y].addTile(textureSheet.getTile(65));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
else if (x == HORIZONTAL_CELLS - 1) {
// Right wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (y % 2 == 0) {
try {
celdas[x][y].addTile(textureSheet.getTile(53));
celdas[x][y].addTile(textureSheet.getTile(238));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
try {
celdas[x][y].addTile(textureSheet.getTile(69));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
else if (y == VERTICAL_CELLS - 1) {
// Bottom wall
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (x % 2 == 0) {
try {
celdas[x][y].addTile(textureSheet.getTile(98));
celdas[x][y].addTile(textureSheet.getTile(207));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
try {
celdas[x][y].addTile(textureSheet.getTile(99));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
if (x == PLAYER_START_X && y == PLAYER_START_Y) {
celdas[x][y].setType(Celda.Type.PLAYER);
celdas[x][y].setAnimation(sprites.get(SpriteType.PLAYER));
}
else if (x == 10 && y == 3) {
celdas[x][y].setType(Celda.Type.ENEMY);
celdas[x][y].setAnimation(sprites.get(SpriteType.ENEMY));
}
else if (x == 10 && y == 7) {
celdas[x][y].setType(Celda.Type.ENEMY);
celdas[x][y].setAnimation(sprites.get(SpriteType.ENEMY));
}
/*else if (x == 16 && y == 1) {
celdas[x][y].setType(Celda.Type.CHEST);
celdas[x][y].setAnimation(sprites.get(SpriteType.CHEST));
}
else if (x == 12 && y == 7) {
celdas[x][y].setType(Celda.Type.CHEST);
celdas[x][y].setAnimation(sprites.get(SpriteType.CHEST));
}*/
/*else {
for (RandomPositionList randomList : arrayList) {
if (cellCount == randomList.getPosition()) {
celdas[i][j].setType(randomList.getType());
switch (randomList.getType()) {
case ENEMY:
celdas[i][j].setAnimation(sprites.get(SpriteType.ENEMY));
break;
case CHEST:
Animation chestSprite = sprites.get(SpriteType.CHEST);
celdas[i][j].setAnimation(chestSprite);
break;
}
break;
}
}
}*/
}
/**
* Load the cell for the scene
*
* @param x The cell x position
* @param y The cell y position
* @param stringBuilder The string builder which contains the JSON
* @param cellCount Which cell to get out of the JSON
*/
private void loadScene(int x, int y, StringBuilder stringBuilder, int cellCount) {
logger.info("Load cell x: " + x + " y: " + y + " from JSON file");
JsonArray jsonArray = Jsoner.deserialize(stringBuilder.toString(), new JsonArray());
// Get the cell
JsonObject cell = (JsonObject) jsonArray.get(cellCount);
// Get the textures
JsonObject textures = (JsonObject) cell.get("textures");
// Get the type
BigDecimal bigDecimal = (BigDecimal) cell.get("type");
int type = bigDecimal.intValue();
// Create the textures needed
for (int k = 0; k < textures.size(); k++) {
int tile = Integer.parseInt(textures.get(String.valueOf(k)).toString());
try {
celdas[x][y].addTile(textureSheet.getTile(tile));
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
// Set the type and animation
if (type == Celda.Type.PLAYER.ordinal()) {
celdas[x][y].setType(Celda.Type.PLAYER);
celdas[x][y].setAnimation(sprites.get(SpriteType.PLAYER));
}
else if (type == Celda.Type.ENEMY.ordinal()) {
celdas[x][y].setType(Celda.Type.ENEMY);
celdas[x][y].setAnimation(sprites.get(SpriteType.ENEMY));
}
else if (type == Celda.Type.CHEST.ordinal()) {
celdas[x][y].setType(Celda.Type.CHEST);
celdas[x][y].setAnimation(sprites.get(SpriteType.CHEST));
}
else if (type == Celda.Type.OBSTACLE.ordinal()) {
celdas[x][y].setType(Celda.Type.OBSTACLE);
}
}
/**
* Export the scene to a JSON file
*/
private void exportScene() {
logger.info("Export scene to JSON");
JsonObject textures;
JsonObject cell;
JsonArray cells = new JsonArray();
for (int i = 0; i < HORIZONTAL_CELLS; i++) {
for (int j = 0; j < VERTICAL_CELLS; j++) {
cell = new JsonObject();
textures = new JsonObject();
textures.put("0", 0);
if (i == 0 && j == 0) {
if (x == 0 && y == 0) {
// Top left corner
textures.put("1", 33);
cell.put("type", Celda.Type.OBSTACLE.ordinal());
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTexture(textureSheet.getTexture(33), 33);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 4 && j == 3) {
else if (x == 4 && y == 3) {
// Obstacle on floor
textures.put("1", 30);
cell.put("type", Celda.Type.OBSTACLE.ordinal());
try {
celdas[x][y].setType(Celda.Type.OBSTACLE);
celdas[x][y].addTexture(textureSheet.getTexture(30), 30);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 6 && j == 6) {
else if (x == 6 && y == 6) {
// Blood on floor
textures.put("1", 12);
cell.put("type", Celda.Type.SPACE.ordinal());
try {
celdas[x][y].addTexture(textureSheet.getTexture(12), 12);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == HORIZONTAL_CELLS - 1 && j == 0) {
else if (x == HORIZONTAL_CELLS - 1 && y == 0) {
// Top right corner
textures.put("1", 37);
cell.put("type", Celda.Type.OBSTACLE.ordinal());
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTexture(textureSheet.getTexture(37), 37);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 0 && j == VERTICAL_CELLS - 1) {
else if (x == 0 && y == VERTICAL_CELLS - 1) {
// Bottom left corner
textures.put("1", 97);
cell.put("type", Celda.Type.OBSTACLE.ordinal());
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTexture(textureSheet.getTexture(97), 97);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == HORIZONTAL_CELLS - 1 && j == VERTICAL_CELLS - 1) {
else if (x == HORIZONTAL_CELLS - 1 && y == VERTICAL_CELLS - 1) {
// Bottom right corner
textures.put("1", 101);
cell.put("type", Celda.Type.OBSTACLE.ordinal());
celdas[x][y].setType(Celda.Type.OBSTACLE);
try {
celdas[x][y].addTexture(textureSheet.getTexture(101), 101);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (j == 0) {
else if (y == 0) {
// Top wall
if (i == 1) {
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (x == 1) {
// Left door frame
textures.put("1", 144);
textures.put("2", 192);
try {
celdas[x][y].addTexture(textureSheet.getTexture(144), 144);
celdas[x][y].addTexture(textureSheet.getTexture(192), 192);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 2) {
else if (x == 2) {
// Door
textures.put("1", 145);
try {
celdas[x][y].addTexture(textureSheet.getTexture(145), 145);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 3) {
else if (x == 3) {
// Right door frame
textures.put("1", 146);
textures.put("2", 194);
try {
celdas[x][y].addTexture(textureSheet.getTexture(146), 146);
celdas[x][y].addTexture(textureSheet.getTexture(194), 194);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i == 8) {
else if (x == 8) {
// Broken wall piece
textures.put("1", 105);
try {
celdas[x][y].addTexture(textureSheet.getTexture(105), 105);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else if (i % 2 == 0) {
textures.put("1", 34);
textures.put("2", 222);
else if (x % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(34), 34);
celdas[x][y].addTexture(textureSheet.getTexture(222), 222);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
textures.put("1", 35);
try {
celdas[x][y].addTexture(textureSheet.getTexture(35), 35);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
cell.put("type", Celda.Type.OBSTACLE.ordinal());
}
else if (i == 0) {
else if (x == 0) {
// Left wall
if (j % 2 == 0) {
textures.put("1", 49);
textures.put("2", 255);
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(49), 49);
celdas[x][y].addTexture(textureSheet.getTexture(255), 255);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
textures.put("1", 65);
try {
celdas[x][y].addTexture(textureSheet.getTexture(65), 65);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
cell.put("type", Celda.Type.OBSTACLE.ordinal());
}
else if (i == HORIZONTAL_CELLS - 1) {
else if (x == HORIZONTAL_CELLS - 1) {
// Right wall
if (j % 2 == 0) {
textures.put("1", 53);
textures.put("2", 238);
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (y % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(53), 53);
celdas[x][y].addTexture(textureSheet.getTexture(238), 238);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
textures.put("1", 69);
try {
celdas[x][y].addTexture(textureSheet.getTexture(69), 69);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
cell.put("type", Celda.Type.OBSTACLE.ordinal());
}
else if (j == VERTICAL_CELLS - 1) {
else if (y == VERTICAL_CELLS - 1) {
// Bottom wall
if (i % 2 == 0) {
textures.put("1", 98);
textures.put("2", 207);
celdas[x][y].setType(Celda.Type.OBSTACLE);
if (x % 2 == 0) {
try {
celdas[x][y].addTexture(textureSheet.getTexture(98), 98);
celdas[x][y].addTexture(textureSheet.getTexture(207), 207);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
else {
textures.put("1", 99);
}
cell.put("type", Celda.Type.OBSTACLE.ordinal());
}
else {
if (i == PLAYER_START_X && j == PLAYER_START_Y) {
cell.put("type", Celda.Type.PLAYER.ordinal());
}
else if (i == 10 && j == 3) {
cell.put("type", Celda.Type.ENEMY.ordinal());
}
else if (i == 10 && j == 7) {
cell.put("type", Celda.Type.ENEMY.ordinal());
}
/*else if (i == 16 && j == 1) {
cell.put("type", Celda.Type.CHEST.ordinal());
}
else if (i == 12 && j == 7) {
cell.put("type", Celda.Type.CHEST.ordinal());
}*/
else {
cell.put("type", Celda.Type.SPACE.ordinal());
try {
celdas[x][y].addTexture(textureSheet.getTexture(99), 99);
}
catch (SheetException e) {
logger.warning(e.getMessage());
}
}
}
cell.put("textures", textures);
cells.add(cell);
if (x == PLAYER_START_X && y == PLAYER_START_Y) {
celdas[x][y].setType(Celda.Type.PLAYER);
celdas[x][y].setAnimation(sprites.get(SpriteType.PLAYER));
}
else if (x == 10 && y == 3) {
celdas[x][y].setType(Celda.Type.ENEMY);
celdas[x][y].setAnimation(sprites.get(SpriteType.ENEMY));
}
else if (x == 10 && y == 7) {
celdas[x][y].setType(Celda.Type.ENEMY);
celdas[x][y].setAnimation(sprites.get(SpriteType.ENEMY));
}
/*else if (x == 16 && y == 1) {
celdas[x][y].setType(Celda.Type.CHEST);
celdas[x][y].setAnimation(sprites.get(SpriteType.CHEST));
}
else if (x == 12 && y == 7) {
celdas[x][y].setType(Celda.Type.CHEST);
celdas[x][y].setAnimation(sprites.get(SpriteType.CHEST));
}*/
}
}
// Save the new json file
File file = new File("src/res/scene.json");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(Jsoner.prettyPrint(cells.toJson()).getBytes());
fileOutputStream.close();
}
catch (IOException e) {
logger.warning(e.getMessage());
}
}
/**
@ -654,10 +479,10 @@ public class Escenario extends JComponent implements Constantes {
Sheet chestSheet = new Sheet("/res/img/chest/chests.png", 54, 63);
try {
animation = new Animation();
animation.addImage(Animation.Direction.NONE, chestSheet.getTile(54));
animation.addImage(Animation.Direction.NONE, chestSheet.getTile(66));
animation.addImage(Animation.Direction.NONE, chestSheet.getTile(78));
animation.addImage(Animation.Direction.NONE, chestSheet.getTile(80));
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(SpriteType.CHEST, animation);
}
@ -678,17 +503,17 @@ public class Escenario extends JComponent implements Constantes {
* @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.getTile(character));
animation.addImage(Animation.Direction.DOWN, characterSheet.getTile(character + 2));
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.getTile(character));
animation.addImage(Animation.Direction.LEFT, characterSheet.getTile(character + 2));
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.getTile(character));
animation.addImage(Animation.Direction.RIGHT, characterSheet.getTile(character + 2));
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.getTile(character));
animation.addImage(Animation.Direction.UP, characterSheet.getTile(character + 2));
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character));
animation.addImage(Animation.Direction.UP, characterSheet.getTexture(character + 2));
animation.setYOffset(0);
}
@ -723,7 +548,7 @@ public class Escenario extends JComponent implements Constantes {
public void keyPressed(KeyEvent event) {
if (!doorClosed) {
try {
celdas[2][0].addTile(textureSheet.getTile(193));
celdas[2][0].addTexture(textureSheet.getTexture(193), 193);
}
catch (SheetException e) {
e.printStackTrace();

View File

@ -0,0 +1,25 @@
/*
* Copyright 2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package cl.cromer.game.json;
import cl.cromer.game.Celda;
import java.util.ArrayList;
public class Cell {
public Celda.Type type;
public ArrayList<Integer> textures = new ArrayList<>();
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package cl.cromer.game.json;
import cl.cromer.game.Celda;
import cl.cromer.game.Constantes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Logger;
/**
* This class handles reading and writing of JSON objects
*/
public class Json implements Constantes {
/**
* The logger
*/
private Logger logger;
/**
* Initialize the JSON object
*/
public Json() {
logger = getLogger(this.getClass(), JSON_LOG_LEVEL);
}
/**
* Export the game cells to a JSON ready object then write it to a file
*
* @param celdas The cells of the scene to export
*/
public void exportScene(Celda[][] celdas) {
Cell[][] cells = new Cell[celdas.length][celdas[0].length];
for (int i = 0; i < celdas.length; i++) {
for (int j = 0; j < celdas[i].length; j++) {
cells[i][j] = new Cell();
cells[i][j].type = celdas[i][j].getType();
cells[i][j].textures = celdas[i][j].getTextureNumbers();
}
}
writeScene(cells);
}
/**
* Write the JSON scene to a file
*
* @param cells The JSON cells object
*/
private void writeScene(Cell[][] cells) {
GsonBuilder gsonBuilder;
if (PRETTY_JSON) {
gsonBuilder = new GsonBuilder().setPrettyPrinting();
}
else {
gsonBuilder = new GsonBuilder();
}
Gson gson = gsonBuilder.create();
String json = gson.toJson(cells);
File file = new File("src/res/scene.json");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(json.getBytes());
fileOutputStream.close();
}
catch (IOException e) {
logger.warning(e.getMessage());
}
}
}

View File

@ -28,12 +28,12 @@ import java.util.logging.Logger;
*/
public class Sheet implements Constantes {
/**
* A list of all the tile in the collection
* A list of all the textures in the collection
*/
private ArrayList<BufferedImage> images;
/**
* Initialize the tile collection and add all of its images
* Initialize the texture collection
*
* @param path The path to the image
*/
@ -59,19 +59,19 @@ public class Sheet implements Constantes {
}
/**
* Returns the selected tile
* Returns the selected texture
*
* @return Returns the current tile
* @throws SheetException Thrown when there are no images in the tile
* @return Returns the current texture
* @throws SheetException Thrown when there are no images in the texture collection
*/
public BufferedImage getTile(int tileNumber) throws SheetException {
public BufferedImage getTexture(int textureNumber) throws SheetException {
if (images.size() == 0) {
throw new SheetException("There are no images in the tile collection!");
throw new SheetException("There are no images in the texture collection!");
}
if (tileNumber < 0 || tileNumber > images.size() - 1) {
throw new SheetException("Invalid tile number!");
if (textureNumber < 0 || textureNumber > images.size() - 1) {
throw new SheetException("Invalid texture number!");
}
return images.get(tileNumber);
return images.get(textureNumber);
}
}

File diff suppressed because it is too large Load Diff