Change log system initialization

Remove sound threads
Refactor variable names

Signed-off-by: Chris Cromer <chris@cromer.cl>
This commit is contained in:
Chris Cromer 2019-09-30 12:49:20 -03:00
parent 4f65862177
commit 2092690206
17 changed files with 114 additions and 69 deletions

View File

@ -77,7 +77,7 @@ public class Celda extends JComponent implements Constantes {
this.yPixels = yPixels;
this.x = x;
this.y = y;
logger = getLogger(this.getClass(), CELDA_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.CELDA);
}
/**

View File

@ -34,24 +34,35 @@ public interface Constantes {
* The name of the game
*/
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;
Level MAIN_LOG_LEVEL = Level.WARNING;
Level VENTANA_PRINCIPAL_LOG_LEVEL = Level.WARNING;
Level LIENZO_LOG_LEVEL = Level.WARNING;
Level ESCENARIO_LOG_LEVEL = Level.WARNING;
Level PLAYER_LOG_LEVEL = Level.WARNING;
Level ENEMY_LOG_LEVEL = Level.WARNING;
Level CHEST_LOG_LEVEL = Level.WARNING;
Level CONFIG_LOG_LEVEL = Level.WARNING;
Level SOUND_LOG_LEVEL = Level.WARNING;
Level IMAGE_LOG_LEVEL = Level.WARNING;
Level CELDA_LOG_LEVEL = Level.WARNING;
Level KEY_LOG_LEVEL = Level.WARNING;
Level JSON_LOG_LEVEL = Level.WARNING;
Level PORTAL_LOG_LEVEL = Level.WARNING;
default Logger getLogger(Class logClass, LogLevel logLevel) {
String className = logClass.getName();
Logger logger;
if (GLOBAL_LOG) {
logger = Logger.getGlobal();
}
else {
logger = Logger.getLogger(className);
}
if (logger.getHandlers().length == 0) {
initializeLogger(logClass);
}
if (GLOBAL_LOG) {
logger.setLevel(LogLevel.GLOBAL.getLevel());
}
else {
logger.setLevel(logLevel.getLevel());
}
return logger;
}
/**
* 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
*
* @param logClass The class that is in need of a logger
* @param level What log level to use
* @return Returns the logger
* This enum contains all the levels used for logging
*/
default Logger getLogger(Class logClass, Level level) {
String className = logClass.getName();
Logger logger;
if (GLOBAL_LOG) {
logger = Logger.getGlobal();
enum LogLevel {
GLOBAL(Level.WARNING),
MAIN(Level.WARNING),
VENTANA_PRINCIPAL(Level.WARNING),
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;
}
}

View File

@ -104,7 +104,7 @@ public class Escenario extends JComponent implements Constantes {
* @param canvas The canvas that this scene is in
*/
public Escenario(Lienzo canvas) {
logger = getLogger(this.getClass(), ESCENARIO_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.ESCENARIO);
this.canvas = canvas;
loadResources();
@ -189,6 +189,10 @@ public class Escenario extends JComponent implements Constantes {
int random_x;
int random_y;
ArrayList<RandomPositionList> arrayList = new ArrayList<>();
arrayList.add(new RandomPositionList(5, 5, Celda.Type.CHEST));
arrayList.add(new RandomPositionList(6, 5, Celda.Type.CHEST));
for (int i = 0; i < ENEMIES; i++) {
random_x = random(0, HORIZONTAL_CELLS - 1);
random_y = random(0, VERTICAL_CELLS - 1);

View File

@ -82,10 +82,6 @@ public class Lienzo extends Canvas implements Constantes {
* The background music of the game
*/
private Sound backgroundMusic;
/**
* The background music thread
*/
private Thread backgroundMusicThread;
/**
* Game over
*/
@ -99,7 +95,7 @@ public class Lienzo extends Canvas implements Constantes {
* Initialize the canvas
*/
public Lienzo() {
logger = getLogger(this.getClass(), LIENZO_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.LIENZO);
escenario = new Escenario(this);
setBackground(Color.black);
setSize(escenario.width, escenario.height);
@ -163,8 +159,7 @@ public class Lienzo extends Canvas implements Constantes {
try {
backgroundMusic = escenario.getSounds().get(Sound.SoundType.BACKGROUND);
backgroundMusic.setLoops(Clip.LOOP_CONTINUOUSLY);
backgroundMusicThread = new Thread(backgroundMusic);
backgroundMusicThread.start();
backgroundMusic.run();
}
catch (SoundException e) {
logger.warning(e.getMessage());
@ -295,7 +290,6 @@ public class Lienzo extends Canvas implements Constantes {
try {
if (backgroundMusic.isPlaying()) {
backgroundMusic.stop();
backgroundMusicThread.interrupt();
}
}
catch (SoundException e) {

View File

@ -27,7 +27,7 @@ public class Main implements Constantes {
* Initialize the main class
*/
public Main() {
Logger logger = getLogger(this.getClass(), MAIN_LOG_LEVEL);
Logger logger = getLogger(this.getClass(), LogLevel.MAIN);
logger.info("Load main window");
VentanaPrincipal ventanaPrincipal = new VentanaPrincipal();

View File

@ -34,7 +34,7 @@ public class VentanaPrincipal extends JFrame implements Constantes {
* Initialize the main window
*/
public VentanaPrincipal() {
Logger logger = getLogger(this.getClass(), VENTANA_PRINCIPAL_LOG_LEVEL);
Logger logger = getLogger(this.getClass(), LogLevel.VENTANA_PRINCIPAL);
logger.info("Create panels");

View File

@ -38,7 +38,7 @@ public class Json implements Constantes {
* Initialize the JSON object
*/
public Json() {
logger = getLogger(this.getClass(), JSON_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.JSON);
}
/**

View File

@ -42,7 +42,7 @@ public class Chest extends Object implements Constantes {
*/
public Chest(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), CHEST_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.CHEST);
}
/**

View File

@ -56,7 +56,7 @@ public class Enemy extends Object implements Constantes {
*/
public Enemy(Escenario escenario, Celda celda, Lock lock) {
super(escenario, celda);
logger = getLogger(this.getClass(), ENEMY_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.ENEMY);
this.lock = lock;
}

View File

@ -39,7 +39,7 @@ public class Key extends Object implements Constantes {
*/
public Key(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), KEY_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.KEY);
}
/**

View File

@ -19,6 +19,7 @@ import cl.cromer.azaraka.Celda;
import cl.cromer.azaraka.Constantes;
import cl.cromer.azaraka.Escenario;
import cl.cromer.azaraka.sound.Sound;
import cl.cromer.azaraka.sound.SoundException;
import cl.cromer.azaraka.sprite.Animation;
import cl.cromer.azaraka.sprite.AnimationException;
@ -55,7 +56,7 @@ public class Player extends Object implements Constantes {
*/
public Player(Escenario escenario, Celda 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());
}
// 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()) {
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);

View File

@ -44,7 +44,7 @@ public class Portal extends Object implements Constantes {
*/
public Portal(Escenario escenario, Celda celda) {
super(escenario, celda);
logger = getLogger(this.getClass(), PORTAL_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.PORTAL);
}
/**

View File

@ -44,7 +44,7 @@ public class Config extends JPanel implements Constantes {
public Config(Game gamePanel) {
this.gamePanel = gamePanel;
logger = getLogger(this.getClass(), CONFIG_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.CONFIG);
JLabel speed = new JLabel("Speed");
speed.setForeground(Color.yellow);

View File

@ -39,6 +39,7 @@ public class Sound implements Runnable, Constantes {
* The logger
*/
private Logger logger;
/**
* Load the sound
*
@ -47,7 +48,7 @@ public class Sound implements Runnable, Constantes {
*/
public Sound(String path) throws SoundException {
this.path = path;
logger = getLogger(this.getClass(), SOUND_LOG_LEVEL);
logger = getLogger(this.getClass(), LogLevel.SOUND);
InputStream inputStream = this.getClass().getResourceAsStream(path);
if (inputStream == null) {
throw new SoundException("Could not load sound: " + path);

View File

@ -59,7 +59,7 @@ public class Animation implements Cloneable, Constantes {
*/
public Animation() {
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
*/
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);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
image = resized;
return image;
Graphics2D graphics2D = resized.createGraphics();
graphics2D.drawImage(tmpImage, 0, 0, null);
graphics2D.dispose();
return resized;
}
/**

View File

@ -37,7 +37,7 @@ public class AnimationMap extends HashMap<Animation.SpriteType, Animation> imple
return (Animation) super.get(key).clone();
}
catch (CloneNotSupportedException e) {
Logger logger = getLogger(this.getClass(), IMAGE_LOG_LEVEL);
Logger logger = getLogger(this.getClass(), LogLevel.ANIMATION);
logger.warning(e.getMessage());
}
return null;

View File

@ -41,7 +41,7 @@ public class Sheet implements Constantes {
*/
public Sheet(String path, int height, int width) {
images = new ArrayList<>();
Logger logger = getLogger(this.getClass(), IMAGE_LOG_LEVEL);
Logger logger = getLogger(this.getClass(), LogLevel.SHEET);
try {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));