Logger is love, Logger is life

This commit is contained in:
iryupnik 2020-06-11 13:33:40 +02:00
parent 53f9333ce1
commit 3559d04741
12 changed files with 283 additions and 19 deletions

12
.gitignore vendored
View File

@ -1,3 +1,15 @@
.idea
/target
BattleLog.log
BattleLog.log.lck
doorimage.log
doorimage.log.lck
imagecell.log
imagecell.log.lck
imageperson.log
imageperson.log.lck
imagedialog.log
imagedialog.log.lck
gameOfProd.iml
Game_of_Prod.zip

View File

@ -2,6 +2,8 @@ package ruscz.game.gameOfProd.Battle;
import ruscz.game.gameOfProd.Cells.InteractionCell;
import ruscz.game.gameOfProd.Dialog.Dialog;
import ruscz.game.gameOfProd.Image.ImageCell;
import ruscz.game.gameOfProd.Scripts.Utils;
import ruscz.game.gameOfProd.Sounds.SoundManager;
import ruscz.game.gameOfProd.Windows.WindowOfBattle;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
@ -11,6 +13,7 @@ import java.awt.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Logger;
/**
* Class for Battle with graphic implementation
@ -28,10 +31,12 @@ public class Battle {
private boolean playerAttack;
private boolean timerStopped;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(Battle.class.getName()), "BattleLog");
/**
* Hide Menu and Enemy Description
* Set Attack of Player
* @param playerAttack
* @param playerAttack true if attacks
*/
public void setPlayerAttack(boolean playerAttack) {
windowOfBattle.getMenuOfActions().setVisible(false);
@ -127,6 +132,7 @@ public class Battle {
windowOfBattle.getGame().remove(windowOfBattle);
if (windowOfBattle.getMusicManager() != null) windowOfBattle.getMusicManager().stopMusic();
}
LOG.fine("End of Battle");
// SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager();
// saveManager.saveGame();
}
@ -141,6 +147,8 @@ public class Battle {
for (Component e: windowOfBattle.getMenuOfActions().getComponents()) e.setEnabled(true);
windowOfBattle.getEnemyDescription().updateDescription();
windowOfBattle.getSituationText().hideText();
LOG.fine("Control returned to player");
}
/**
@ -154,6 +162,7 @@ public class Battle {
turnBelongTo.setImageEffect("attack");
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), -10.0f);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " damage to " + e.getName()+"!");
LOG.fine(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " damage to " + e.getName()+"! HERO");
if (e.health <= 0) {
if (e.getImage()!= null) e.getKilled(this);
else enemies.remove(e);
@ -189,6 +198,8 @@ public class Battle {
Collections.shuffle(turns);
turnBelongTo = turns.getFirst();
}
LOG.fine("Next Person to Attack is " + turnBelongTo);
}
/**
@ -202,6 +213,7 @@ public class Battle {
if (windowOfBattle.getHitSound() != null) soundManager.playSound(windowOfBattle.getHitSound(), -10.0f);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() + " damage to " + e.getName()+"!");
LOG.fine(turnBelongTo.getName() + " deals " + turnBelongTo.getLevel() + " damage to " + e.getName()+"! MONSTER");
if (e.health <= 0) {
if (e.getImage()!= null) e.getKilled(this);
else heroes.remove(e);
@ -252,6 +264,7 @@ public class Battle {
* @param person Person to delete
*/
public void removePersonFromList(BattlePerson person) {
LOG.fine("Removing " + person);
if (person.getClass().getName().equals("ruscz.game.gameOfProd.Battle.Hero")) heroes.remove(person);
else enemies.remove(person);
}

View File

@ -268,4 +268,15 @@ public abstract class BattlePerson {
public int getMaxHealth() {
return maxHealth;
}
@Override
public String toString() {
return "BattlePerson{" +
"maxHealth=" + maxHealth +
", health=" + health +
", level=" + level +
", name='" + name + '\'' +
", strength=" + strength +
'}';
}
}

View File

@ -1,14 +1,17 @@
package ruscz.game.gameOfProd.BattleUI;
import ruscz.game.gameOfProd.Battle.Battle;
import ruscz.game.gameOfProd.Image.ImageCell;
import ruscz.game.gameOfProd.Inventory.InventoryManager;
import ruscz.game.gameOfProd.Inventory.Item;
import ruscz.game.gameOfProd.Inventory.ItemAction;
import ruscz.game.gameOfProd.Inventory.ItemPanel;
import ruscz.game.gameOfProd.Scripts.Utils;
import ruscz.game.gameOfProd.Windows.WindowOfBattle;
import javax.swing.*;
import java.awt.*;
import java.util.logging.Logger;
import static ruscz.game.gameOfProd.Windows.WindowOfBattle.CELL_SIZE;
@ -20,6 +23,8 @@ public class ItemsInventory extends JPanel {
private final ImageCell imageCell;
private final WindowOfBattle windowOfBattle;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(Battle.class.getName()), "BattleLog");
public ItemsInventory(WindowOfBattle window){
imageCell = new ImageCell();
imageCell.imageItem("TransparentIcons.png");
@ -75,8 +80,14 @@ public class ItemsInventory extends JPanel {
windowOfBattle.getMenuOfActions().setVisible(false);
windowOfBattle.getEnemyDescription().setVisible(false);
windowOfBattle.getSituationText().setVisible(true);
int realHeal = windowOfBattle.getBattle().getHeroes().getFirst().heal(100);
windowOfBattle.writeToOutput(windowOfBattle.getBattle().getHeroes().getFirst().getName() + " heals "
+ windowOfBattle.getBattle().getHeroes().getFirst().heal(100) + " HP");
+ realHeal + " HP");
LOG.fine(windowOfBattle.getBattle().getHeroes().getFirst().getName() + " heals "
+ realHeal + " HP");
InventoryManager.removeItem(item, 1);
windowOfBattle.getBattle().addToTurnOrder(windowOfBattle.getBattle().getHeroes().getFirst());
windowOfBattle.getBattle().setTurnBelongTo(null);

View File

@ -1,10 +1,13 @@
package ruscz.game.gameOfProd.BattleUI;
import ruscz.game.gameOfProd.Battle.Battle;
import ruscz.game.gameOfProd.Scripts.Utils;
import ruscz.game.gameOfProd.Windows.WindowOfBattle;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.logging.Logger;
import static ruscz.game.gameOfProd.Windows.WindowOfBattle.*;
@ -17,6 +20,8 @@ public class MenuOfActions extends JPanel {
private final JButton inventory;
private final JButton escape;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(Battle.class.getName()), "BattleLog");
public MenuOfActions(WindowOfBattle window) {
setLayout(new GridLayout(4,1));
@ -66,13 +71,19 @@ public class MenuOfActions extends JPanel {
escape.setEnabled(false);
if (window.isRunAwayPossible()) {
LOG.fine("Try to escape");
Random r = new Random();
if (r.nextBoolean()) window.getBattle().endBattle(true);
if (r.nextBoolean()) {
window.getBattle().endBattle(true);
LOG.fine("RunAway is successful");
}
else {
window.getMenuOfActions().setVisible(false);
window.getEnemyDescription().setVisible(false);
window.getSituationText().setVisible(true);
window.writeToOutput("Runaway was not successful.");
LOG.fine("Runaway was not successful.");
window.getBattle().addToTurnOrder(window.getBattle().getHeroes().getFirst());
window.getBattle().setTurnBelongTo(null);
window.getBattle().setDelay(2);
@ -84,6 +95,7 @@ public class MenuOfActions extends JPanel {
window.getEnemyDescription().setVisible(false);
window.getSituationText().setVisible(true);
window.writeToOutput("Runaway is not possible.");
LOG.fine("Runaway is not possible.");
window.getBattle().addToTurnOrder(window.getBattle().getHeroes().getFirst());
window.getBattle().setTurnBelongTo(null);
window.getBattle().setDelay(2);

View File

@ -1,5 +1,6 @@
package ruscz.game.gameOfProd.Image;
import ruscz.game.gameOfProd.Scripts.Utils;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import javax.imageio.ImageIO;
@ -11,6 +12,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.logging.Logger;
/**
* This class manages and holds tileset and all matrices of images to be pained
@ -24,8 +26,10 @@ public class ImageCell {
private int[][] mapBackground;
private int[][] mapForeground;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(ImageCell.class.getName()), "imagecell");
/**
* Creates new matrices based on winth and height values
* Creates new matrices based on width and height values
* @param width is width of new map
* @param height is height of new map
*/
@ -68,8 +72,9 @@ public class ImageCell {
}
}
imageObjectset(name);
LOG.fine("Map's tileset and objectset successfully parsed");
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
LOG.severe(name + "-tileset file doesn't exist in chapter " + WindowOfFreeMove.getCHAPTER());
e.printStackTrace();
}
}
@ -82,6 +87,7 @@ public class ImageCell {
try {
image = ImageIO.read(new File("src/main/resources/tilesets/" + tilesetInfo[1]));
} catch (IOException e) {
LOG.severe("Tileset image with name " + tilesetInfo[1] + " doesn't exist");
e.printStackTrace();
}
@ -96,7 +102,7 @@ public class ImageCell {
int tiles = Xtiles * Ytiles;
if (imagesOfTiles == null)
imagesOfTiles = new LinkedList<BufferedImage>();
imagesOfTiles = new LinkedList<>();
Rectangle[] rectangles = new Rectangle[tiles];
int count = 0;
@ -110,6 +116,7 @@ public class ImageCell {
for (int i = 0; i < tiles; i++) {
imagesOfTiles.add(image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width));
}
LOG.fine("Map's tileset successfully parsed");
}
/**
@ -128,7 +135,9 @@ public class ImageCell {
}
j++;
}
LOG.fine("Map's objectset successfully parsed");
} catch (FileNotFoundException e) {
LOG.severe(file + "-objectset file doesn't exist in chapter " + WindowOfFreeMove.getCHAPTER());
e.printStackTrace();
}
}
@ -155,6 +164,7 @@ public class ImageCell {
}
else
cellMap.put(cellInfo[1], Integer.parseInt(cellInfo[2]));
LOG.fine("Map's cellset successfully parsed");
}
/**
@ -171,6 +181,7 @@ public class ImageCell {
imageHeight = image.getHeight();
size = 32;
} catch (IOException e) {
LOG.severe("Item image with name " + itemInfo + " doesn't exist");
e.printStackTrace();
}
@ -187,6 +198,8 @@ public class ImageCell {
imagesOfItems.add(img);
}
}
LOG.fine("Image of Item successfully parsed");
}
public LinkedList<BufferedImage> getImagesOfItems() {

View File

@ -1,23 +1,28 @@
package ruscz.game.gameOfProd.Image;
import ruscz.game.gameOfProd.Scripts.Utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
/**
* This class holds images to be painted in dialog
*/
public class ImageDialog {
private String name;
private final String name;
private Image image;
private static Image dialogBack;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(ImageDialog.class.getName()), "imagedialog");
public Image getImage() {
return image;
}
public static Image getDialogBack() {return dialogBack;}
public static Image getDialogBack() { return dialogBack;}
public ImageDialog(String name){
this.name = name;
@ -31,6 +36,7 @@ public class ImageDialog {
try {
dialogBack = ImageIO.read(new File("src/main/resources/dialogs/DialogBox.png"));
} catch (IOException e) {
LOG.severe("Background for dialog doesn't exist");
e.printStackTrace();
}
}
@ -42,6 +48,7 @@ public class ImageDialog {
try {
image = ImageIO.read(new File("src/main/resources/characters/" + name.substring(0, name.indexOf("-face.png")) + "/" + name));
} catch (IOException e) {
LOG.severe("Image for dialog (face expression) with name " + name + " doesn't exist");
e.printStackTrace();
}
}

View File

@ -1,10 +1,13 @@
package ruscz.game.gameOfProd.Image;
import ruscz.game.gameOfProd.Scripts.Utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
/**
* This class images of doors to be painted
@ -14,6 +17,8 @@ public class ImageDoor {
private BufferedImage[] imagesOfDoor;
private int size;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(ImageDoor.class.getName()), "doorimage");
/**
* Parses images of door into an array
* @param rectangles is array of door images
@ -28,6 +33,8 @@ public class ImageDoor {
BufferedImage img = image.getSubimage(0, size * j * 2, size * 2, size * 2);
imagesOfDoor[j] = img;
}
LOG.fine("All images for door successfully parsed");
}
public ImageDoor(String door, Rectangle[] rectangles) {
@ -36,6 +43,7 @@ public class ImageDoor {
image = ImageIO.read(new File("src/main/resources/tilesets/" + doorInfo[0]));
size = Integer.parseInt(doorInfo[1]);
} catch (IOException e) {
LOG.severe("Door image with name " + door.split(",")[0] + " doesn't exist");
e.printStackTrace();
}

View File

@ -1,5 +1,6 @@
package ruscz.game.gameOfProd.Image;
import ruscz.game.gameOfProd.Scripts.Utils;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import javax.imageio.ImageIO;
@ -8,6 +9,7 @@ import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Logger;
/**
* This class holds image of hero to be painted
@ -18,6 +20,9 @@ public class ImagePerson {
private int subimageWidth;
private int subimageHeight;
private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(ImagePerson.class.getName()), "imageperson");
/**
* Parses images of hero into an array
* @param rectangles is array of hero images
@ -36,6 +41,8 @@ public class ImagePerson {
count++;
}
}
LOG.fine("All Person Images parsed successfully");
}
public ImagePerson(String name, Rectangle[] rectangles) {
@ -47,6 +54,7 @@ public class ImagePerson {
subimageHeight = Integer.parseInt(subimageSize[1]);
image = ImageIO.read(new File("src/main/resources/characters/main/" + scanner.nextLine()));
} catch (IOException e) {
LOG.severe("Image or text description of Person " + name + " doesn't exist");
e.printStackTrace();
}

View File

@ -0,0 +1,20 @@
package ruscz.game.gameOfProd.Scripts;
import java.io.IOException;
import java.util.logging.*;
public class Utils {
public static Logger setSettingsToLogger(Logger log, String nameOfLog) {
log.setUseParentHandlers(true);
try {
log.addHandler(new FileHandler(nameOfLog + ".log"));
} catch (IOException e) {
e.printStackTrace();
}
log.setLevel(Level.ALL);
return log;
}
}

View File

@ -1,15 +1,164 @@
<map>
<mapName>m1</mapName>
<events>
</events>
</map>
<map>
<mapName>m6</mapName>
<mapName>tutorial</mapName>
<events>
<trigger>
<x>11</x>
<y>11</y>
<y>6</y>
<active>false</active>
</trigger>
</events>
</map>
<map>
<mapName>openspace</mapName>
<events>
<trigger>
<x>6</x>
<y>4</y>
<active>true</active>
</trigger>
<trigger>
<x>14</x>
<y>16</y>
<active>false</active>
</trigger>
<chest>
<x>1</x>
<y>2</y>
<active>true</active>
</chest>
<chest>
<x>15</x>
<y>7</y>
<active>true</active>
</chest>
<door>
<x>8</x>
<y>4</y>
<locked>false</locked>
<open>false</open>
</dooŕ>
<door>
<x>2</x>
<y>6</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>6</x>
<y>6</y>
<locked>false</locked>
<open>false</open>
</dooŕ>
<door>
<x>16</x>
<y>10</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>8</x>
<y>13</y>
<locked>false</locked>
<open>false</open>
</dooŕ>
</events>
</map>
<map>
<mapName>office</mapName>
<events>
<trigger>
<x>4</x>
<y>2</y>
<active>false</active>
</trigger>
<chest>
<x>1</x>
<y>9</y>
<active>true</active>
</chest>
<door>
<x>12</x>
<y>1</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>17</x>
<y>1</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>7</x>
<y>12</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>12</x>
<y>12</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
<door>
<x>4</x>
<y>1</y>
<locked>false</locked>
<open>false</open>
</dooŕ>
<door>
<x>2</x>
<y>12</y>
<locked>false</locked>
<open>true</open>
</dooŕ>
<door>
<x>17</x>
<y>12</y>
<locked>true</locked>
<open>false</open>
</dooŕ>
</events>
</map>
<map>
<mapName>boss</mapName>
<events>
<trigger>
<x>4</x>
<y>18</y>
<active>true</active>
</trigger>
<door>
<x>4</x>
<y>19</y>
<locked>false</locked>
<open>false</open>
</dooŕ>
</events>
</map>
<map>
<mapName>treasure</mapName>
<events>
<trigger>
<x>6</x>
<y>1</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>storageroom</mapName>
<events>
<trigger>
<x>6</x>
<y>2</y>
<active>true</active>
</trigger>
<door>
<x>6</x>
<y>1</y>
<locked>false</locked>
<open>true</open>
</dooŕ>
</events>
</map>

View File

@ -1,6 +1,6 @@
<location>
<map>m1</map>
<x>3</x>
<y>7</y>
<direction>2</direction>
<map>storageroom</map>
<x>6</x>
<y>2</y>
<direction>0</direction>
</location>