Change log system initialization
Remove sound threads Refactor variable names Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
parent
4f65862177
commit
2092690206
@ -77,7 +77,7 @@ public class Celda extends JComponent implements Constantes {
|
|||||||
this.yPixels = yPixels;
|
this.yPixels = yPixels;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
logger = getLogger(this.getClass(), CELDA_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.CELDA);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,24 +34,35 @@ public interface Constantes {
|
|||||||
* The name of the game
|
* The name of the game
|
||||||
*/
|
*/
|
||||||
String TITLE = "La Aventura de Azaraka";
|
String TITLE = "La Aventura de Azaraka";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The level of logs to record
|
* Get a logger object to use for debugging
|
||||||
|
*
|
||||||
|
* @param logClass The class that is in need of a logger
|
||||||
|
* @param logLevel What log level to use
|
||||||
|
* @return Returns the logger
|
||||||
*/
|
*/
|
||||||
Level GLOBAL_LOG_LEVEL = Level.WARNING;
|
default Logger getLogger(Class logClass, LogLevel logLevel) {
|
||||||
Level MAIN_LOG_LEVEL = Level.WARNING;
|
String className = logClass.getName();
|
||||||
Level VENTANA_PRINCIPAL_LOG_LEVEL = Level.WARNING;
|
Logger logger;
|
||||||
Level LIENZO_LOG_LEVEL = Level.WARNING;
|
if (GLOBAL_LOG) {
|
||||||
Level ESCENARIO_LOG_LEVEL = Level.WARNING;
|
logger = Logger.getGlobal();
|
||||||
Level PLAYER_LOG_LEVEL = Level.WARNING;
|
}
|
||||||
Level ENEMY_LOG_LEVEL = Level.WARNING;
|
else {
|
||||||
Level CHEST_LOG_LEVEL = Level.WARNING;
|
logger = Logger.getLogger(className);
|
||||||
Level CONFIG_LOG_LEVEL = Level.WARNING;
|
}
|
||||||
Level SOUND_LOG_LEVEL = Level.WARNING;
|
if (logger.getHandlers().length == 0) {
|
||||||
Level IMAGE_LOG_LEVEL = Level.WARNING;
|
initializeLogger(logClass);
|
||||||
Level CELDA_LOG_LEVEL = Level.WARNING;
|
}
|
||||||
Level KEY_LOG_LEVEL = Level.WARNING;
|
if (GLOBAL_LOG) {
|
||||||
Level JSON_LOG_LEVEL = Level.WARNING;
|
logger.setLevel(LogLevel.GLOBAL.getLevel());
|
||||||
Level PORTAL_LOG_LEVEL = Level.WARNING;
|
}
|
||||||
|
else {
|
||||||
|
logger.setLevel(logLevel.getLevel());
|
||||||
|
}
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use a global log if true or individual logs if false
|
* Use a global log if true or individual logs if false
|
||||||
*/
|
*/
|
||||||
@ -195,30 +206,47 @@ public interface Constantes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a logger object to use for debugging
|
* This enum contains all the levels used for logging
|
||||||
*
|
|
||||||
* @param logClass The class that is in need of a logger
|
|
||||||
* @param level What log level to use
|
|
||||||
* @return Returns the logger
|
|
||||||
*/
|
*/
|
||||||
default Logger getLogger(Class logClass, Level level) {
|
enum LogLevel {
|
||||||
String className = logClass.getName();
|
GLOBAL(Level.WARNING),
|
||||||
Logger logger;
|
MAIN(Level.WARNING),
|
||||||
if (GLOBAL_LOG) {
|
VENTANA_PRINCIPAL(Level.WARNING),
|
||||||
logger = Logger.getGlobal();
|
LIENZO(Level.WARNING),
|
||||||
|
ESCENARIO(Level.WARNING),
|
||||||
|
PLAYER(Level.WARNING),
|
||||||
|
ENEMY(Level.WARNING),
|
||||||
|
CHEST(Level.WARNING),
|
||||||
|
CONFIG(Level.WARNING),
|
||||||
|
SOUND(Level.WARNING),
|
||||||
|
ANIMATION(Level.WARNING),
|
||||||
|
SHEET(Level.WARNING),
|
||||||
|
CELDA(Level.WARNING),
|
||||||
|
KEY(Level.WARNING),
|
||||||
|
JSON(Level.WARNING),
|
||||||
|
PORTAL(Level.WARNING);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The level of log for the enum
|
||||||
|
*/
|
||||||
|
private Level level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the log level enum
|
||||||
|
*
|
||||||
|
* @param level The level for each element
|
||||||
|
*/
|
||||||
|
LogLevel(Level level) {
|
||||||
|
this.level = level;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
logger = Logger.getLogger(className);
|
/**
|
||||||
|
* Get the level for the specific part
|
||||||
|
*
|
||||||
|
* @return Returns the level
|
||||||
|
*/
|
||||||
|
public Level getLevel() {
|
||||||
|
return this.level;
|
||||||
}
|
}
|
||||||
if (logger.getHandlers().length == 0) {
|
|
||||||
initializeLogger(logClass);
|
|
||||||
}
|
|
||||||
if (GLOBAL_LOG) {
|
|
||||||
logger.setLevel(GLOBAL_LOG_LEVEL);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
logger.setLevel(level);
|
|
||||||
}
|
|
||||||
return logger;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -104,7 +104,7 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
* @param canvas The canvas that this scene is in
|
* @param canvas The canvas that this scene is in
|
||||||
*/
|
*/
|
||||||
public Escenario(Lienzo canvas) {
|
public Escenario(Lienzo canvas) {
|
||||||
logger = getLogger(this.getClass(), ESCENARIO_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.ESCENARIO);
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
loadResources();
|
loadResources();
|
||||||
|
|
||||||
@ -189,6 +189,10 @@ public class Escenario extends JComponent implements Constantes {
|
|||||||
int random_x;
|
int random_x;
|
||||||
int random_y;
|
int random_y;
|
||||||
ArrayList<RandomPositionList> arrayList = new ArrayList<>();
|
ArrayList<RandomPositionList> arrayList = new ArrayList<>();
|
||||||
|
|
||||||
|
arrayList.add(new RandomPositionList(5, 5, Celda.Type.CHEST));
|
||||||
|
arrayList.add(new RandomPositionList(6, 5, Celda.Type.CHEST));
|
||||||
|
|
||||||
for (int i = 0; i < ENEMIES; i++) {
|
for (int i = 0; i < ENEMIES; i++) {
|
||||||
random_x = random(0, HORIZONTAL_CELLS - 1);
|
random_x = random(0, HORIZONTAL_CELLS - 1);
|
||||||
random_y = random(0, VERTICAL_CELLS - 1);
|
random_y = random(0, VERTICAL_CELLS - 1);
|
||||||
|
@ -82,10 +82,6 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
* The background music of the game
|
* The background music of the game
|
||||||
*/
|
*/
|
||||||
private Sound backgroundMusic;
|
private Sound backgroundMusic;
|
||||||
/**
|
|
||||||
* The background music thread
|
|
||||||
*/
|
|
||||||
private Thread backgroundMusicThread;
|
|
||||||
/**
|
/**
|
||||||
* Game over
|
* Game over
|
||||||
*/
|
*/
|
||||||
@ -99,7 +95,7 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
* Initialize the canvas
|
* Initialize the canvas
|
||||||
*/
|
*/
|
||||||
public Lienzo() {
|
public Lienzo() {
|
||||||
logger = getLogger(this.getClass(), LIENZO_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.LIENZO);
|
||||||
escenario = new Escenario(this);
|
escenario = new Escenario(this);
|
||||||
setBackground(Color.black);
|
setBackground(Color.black);
|
||||||
setSize(escenario.width, escenario.height);
|
setSize(escenario.width, escenario.height);
|
||||||
@ -163,8 +159,7 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
try {
|
try {
|
||||||
backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND);
|
backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND);
|
||||||
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
|
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
|
||||||
backgroundMusicThread = new Thread(backgroundMusic);
|
backgroundMusic.run();
|
||||||
backgroundMusicThread.start();
|
|
||||||
}
|
}
|
||||||
catch (SoundException e) {
|
catch (SoundException e) {
|
||||||
logger.warning(e.getMessage());
|
logger.warning(e.getMessage());
|
||||||
@ -295,7 +290,6 @@ public class Lienzo extends Canvas implements Constantes {
|
|||||||
try {
|
try {
|
||||||
if (backgroundMusic.isPlaying()) {
|
if (backgroundMusic.isPlaying()) {
|
||||||
backgroundMusic.stop();
|
backgroundMusic.stop();
|
||||||
backgroundMusicThread.interrupt();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SoundException e) {
|
catch (SoundException e) {
|
||||||
|
@ -27,7 +27,7 @@ public class Main implements Constantes {
|
|||||||
* Initialize the main class
|
* Initialize the main class
|
||||||
*/
|
*/
|
||||||
public Main() {
|
public Main() {
|
||||||
Logger logger = getLogger(this.getClass(), MAIN_LOG_LEVEL);
|
Logger logger = getLogger(this.getClass(), LogLevel.MAIN);
|
||||||
|
|
||||||
logger.info("Load main window");
|
logger.info("Load main window");
|
||||||
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();
|
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();
|
||||||
|
@ -34,7 +34,7 @@ public class VentanaPrincipal extends JFrame implements Constantes {
|
|||||||
* Initialize the main window
|
* Initialize the main window
|
||||||
*/
|
*/
|
||||||
public VentanaPrincipal() {
|
public VentanaPrincipal() {
|
||||||
Logger logger = getLogger(this.getClass(), VENTANA_PRINCIPAL_LOG_LEVEL);
|
Logger logger = getLogger(this.getClass(), LogLevel.VENTANA_PRINCIPAL);
|
||||||
|
|
||||||
logger.info("Create panels");
|
logger.info("Create panels");
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ public class Json implements Constantes {
|
|||||||
* Initialize the JSON object
|
* Initialize the JSON object
|
||||||
*/
|
*/
|
||||||
public Json() {
|
public Json() {
|
||||||
logger = getLogger(this.getClass(), JSON_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.JSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +42,7 @@ public class Chest extends Object implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Chest(Escenario escenario, Celda celda) {
|
public Chest(Escenario escenario, Celda celda) {
|
||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
logger = getLogger(this.getClass(), CHEST_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.CHEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +56,7 @@ public class Enemy extends Object implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Enemy(Escenario escenario, Celda celda, Lock lock) {
|
public Enemy(Escenario escenario, Celda celda, Lock lock) {
|
||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
logger = getLogger(this.getClass(), ENEMY_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.ENEMY);
|
||||||
this.lock = lock;
|
this.lock = lock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ public class Key extends Object implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Key(Escenario escenario, Celda celda) {
|
public Key(Escenario escenario, Celda celda) {
|
||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
logger = getLogger(this.getClass(), KEY_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,6 +19,7 @@ import cl.cromer.azaraka.Celda;
|
|||||||
import cl.cromer.azaraka.Constantes;
|
import cl.cromer.azaraka.Constantes;
|
||||||
import cl.cromer.azaraka.Escenario;
|
import cl.cromer.azaraka.Escenario;
|
||||||
import cl.cromer.azaraka.sound.Sound;
|
import cl.cromer.azaraka.sound.Sound;
|
||||||
|
import cl.cromer.azaraka.sound.SoundException;
|
||||||
import cl.cromer.azaraka.sprite.Animation;
|
import cl.cromer.azaraka.sprite.Animation;
|
||||||
import cl.cromer.azaraka.sprite.AnimationException;
|
import cl.cromer.azaraka.sprite.AnimationException;
|
||||||
|
|
||||||
@ -55,7 +56,7 @@ public class Player extends Object implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Player(Escenario escenario, Celda celda) {
|
public Player(Escenario escenario, Celda celda) {
|
||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
logger = getLogger(this.getClass(), PLAYER_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.PLAYER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -383,7 +384,16 @@ public class Player extends Object implements Constantes {
|
|||||||
logger.warning(e.getMessage());
|
logger.warning(e.getMessage());
|
||||||
}
|
}
|
||||||
// Play sound
|
// Play sound
|
||||||
new Thread(getEscenario().getSounds().get(Sound.SoundType.GET_KEY)).start();
|
Sound keySound = getEscenario().getSounds().get(Sound.SoundType.GET_KEY);
|
||||||
|
try {
|
||||||
|
if (keySound.isPlaying()) {
|
||||||
|
keySound.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SoundException e) {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
}
|
||||||
|
keySound.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -398,7 +408,16 @@ public class Player extends Object implements Constantes {
|
|||||||
if (hasKey()) {
|
if (hasKey()) {
|
||||||
logger.info("Player opened chest");
|
logger.info("Player opened chest");
|
||||||
|
|
||||||
new Thread(getEscenario().getSounds().get(Sound.SoundType.OPEN_CHEST)).start();
|
Sound chestSound = getEscenario().getSounds().get(Sound.SoundType.OPEN_CHEST);
|
||||||
|
try {
|
||||||
|
if (chestSound.isPlaying()) {
|
||||||
|
chestSound.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SoundException e) {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
}
|
||||||
|
chestSound.run();
|
||||||
|
|
||||||
gainHealth(1);
|
gainHealth(1);
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ public class Portal extends Object implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Portal(Escenario escenario, Celda celda) {
|
public Portal(Escenario escenario, Celda celda) {
|
||||||
super(escenario, celda);
|
super(escenario, celda);
|
||||||
logger = getLogger(this.getClass(), PORTAL_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.PORTAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,7 +44,7 @@ public class Config extends JPanel implements Constantes {
|
|||||||
public Config(Game gamePanel) {
|
public Config(Game gamePanel) {
|
||||||
this.gamePanel = gamePanel;
|
this.gamePanel = gamePanel;
|
||||||
|
|
||||||
logger = getLogger(this.getClass(), CONFIG_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.CONFIG);
|
||||||
|
|
||||||
JLabel speed = new JLabel("Speed");
|
JLabel speed = new JLabel("Speed");
|
||||||
speed.setForeground(Color.yellow);
|
speed.setForeground(Color.yellow);
|
||||||
|
@ -39,6 +39,7 @@ public class Sound implements Runnable, Constantes {
|
|||||||
* The logger
|
* The logger
|
||||||
*/
|
*/
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the sound
|
* Load the sound
|
||||||
*
|
*
|
||||||
@ -47,7 +48,7 @@ public class Sound implements Runnable, Constantes {
|
|||||||
*/
|
*/
|
||||||
public Sound(String path) throws SoundException {
|
public Sound(String path) throws SoundException {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
logger = getLogger(this.getClass(), SOUND_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.SOUND);
|
||||||
InputStream inputStream = this.getClass().getResourceAsStream(path);
|
InputStream inputStream = this.getClass().getResourceAsStream(path);
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
throw new SoundException("Could not load sound: " + path);
|
throw new SoundException("Could not load sound: " + path);
|
||||||
|
@ -59,7 +59,7 @@ public class Animation implements Cloneable, Constantes {
|
|||||||
*/
|
*/
|
||||||
public Animation() {
|
public Animation() {
|
||||||
imageHash = new HashMap<>();
|
imageHash = new HashMap<>();
|
||||||
logger = getLogger(this.getClass(), IMAGE_LOG_LEVEL);
|
logger = getLogger(this.getClass(), LogLevel.ANIMATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,13 +71,12 @@ public class Animation implements Cloneable, Constantes {
|
|||||||
* @return Returns the scaled image
|
* @return Returns the scaled image
|
||||||
*/
|
*/
|
||||||
static public BufferedImage scaleImage(BufferedImage image, int width, int height) {
|
static public BufferedImage scaleImage(BufferedImage image, int width, int height) {
|
||||||
Image tmp = image.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
|
Image tmpImage = image.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
|
||||||
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||||
Graphics2D g2d = resized.createGraphics();
|
Graphics2D graphics2D = resized.createGraphics();
|
||||||
g2d.drawImage(tmp, 0, 0, null);
|
graphics2D.drawImage(tmpImage, 0, 0, null);
|
||||||
g2d.dispose();
|
graphics2D.dispose();
|
||||||
image = resized;
|
return resized;
|
||||||
return image;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,7 +37,7 @@ public class AnimationMap extends HashMap<Animation.SpriteType, Animation> imple
|
|||||||
return (Animation) super.get(key).clone();
|
return (Animation) super.get(key).clone();
|
||||||
}
|
}
|
||||||
catch (CloneNotSupportedException e) {
|
catch (CloneNotSupportedException e) {
|
||||||
Logger logger = getLogger(this.getClass(), IMAGE_LOG_LEVEL);
|
Logger logger = getLogger(this.getClass(), LogLevel.ANIMATION);
|
||||||
logger.warning(e.getMessage());
|
logger.warning(e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -41,7 +41,7 @@ public class Sheet implements Constantes {
|
|||||||
*/
|
*/
|
||||||
public Sheet(String path, int height, int width) {
|
public Sheet(String path, int height, int width) {
|
||||||
images = new ArrayList<>();
|
images = new ArrayList<>();
|
||||||
Logger logger = getLogger(this.getClass(), IMAGE_LOG_LEVEL);
|
Logger logger = getLogger(this.getClass(), LogLevel.SHEET);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
|
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
|
||||||
|
Loading…
Reference in New Issue
Block a user