chore(logging): Implemented basic logging
This commit is contained in:
@ -7,19 +7,23 @@ import cz.jzitnik.game.threads.ThreadProvider;
|
||||
import cz.jzitnik.game.ui.*;
|
||||
import cz.jzitnik.game.SpriteLoader;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
log.info("Setting up terminal");
|
||||
Terminal terminal = TerminalBuilder.terminal();
|
||||
terminal.enterRawMode();
|
||||
|
||||
if (!terminal.hasMouseSupport()) {
|
||||
System.out.println("Error: This terminal does not support mouse.");
|
||||
log.error("This terminal does not support mouse");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
@ -56,6 +60,7 @@ public class Main {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
log.info("Closing terminal");
|
||||
terminal.trackMouse(Terminal.MouseTracking.Off);
|
||||
terminal.close();
|
||||
} catch (IOException | InterruptedException ignored) {
|
||||
|
@ -2,6 +2,9 @@ package cz.jzitnik.game;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class GameSaver {
|
||||
public void save(Game game) {
|
||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("world.ser"))) {
|
||||
@ -11,7 +14,9 @@ public class GameSaver {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This will need rewrite
|
||||
public static Game load() {
|
||||
log.info("Loading game");
|
||||
File file = new File("world.ser");
|
||||
|
||||
if (!file.isFile()) {
|
||||
@ -19,6 +24,7 @@ public class GameSaver {
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("Loading game from save file");
|
||||
FileInputStream fileIn = new FileInputStream("world.ser");
|
||||
|
||||
ObjectInputStream in = new ObjectInputStream(fileIn);
|
||||
|
@ -3,9 +3,11 @@ package cz.jzitnik.game;
|
||||
import cz.jzitnik.game.sprites.*;
|
||||
import cz.jzitnik.tui.Sprite;
|
||||
import cz.jzitnik.tui.SpriteList;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@Slf4j
|
||||
public class SpriteLoader {
|
||||
public enum SPRITES {
|
||||
// BLOCKS
|
||||
@ -364,6 +366,7 @@ public class SpriteLoader {
|
||||
}
|
||||
|
||||
public static SpriteList<SPRITES> load() {
|
||||
log.info("Loading sprites");
|
||||
return new SpriteList<>(SPRITES.class, SPRITES_MAP);
|
||||
}
|
||||
}
|
||||
|
@ -6,18 +6,23 @@ import java.util.Set;
|
||||
|
||||
import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.annotations.CustomLogic;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@Slf4j
|
||||
public class CustomLogicProvider {
|
||||
private final List<CustomLogicInterface> logicList = new ArrayList<>();
|
||||
|
||||
public void update(Game game) {
|
||||
for (CustomLogicInterface logicInterface : logicList) {
|
||||
log.debug("Running logic {}.", logicInterface.getClass().getSimpleName());
|
||||
logicInterface.nextIteration(game);
|
||||
}
|
||||
}
|
||||
|
||||
public CustomLogicProvider() {
|
||||
log.info("Loading logic");
|
||||
registerHandlers();
|
||||
}
|
||||
|
||||
@ -30,6 +35,7 @@ public class CustomLogicProvider {
|
||||
try {
|
||||
CustomLogicInterface instance = (CustomLogicInterface) clazz.getDeclaredConstructor().newInstance();
|
||||
logicList.add(instance);
|
||||
log.info("Loaded custom logic with name {}", clazz.getSimpleName());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -10,8 +10,11 @@ import cz.jzitnik.game.annotations.EntityLogic;
|
||||
import cz.jzitnik.game.entities.Block;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@Slf4j
|
||||
public class EntityLogicProvider {
|
||||
private static final int CHUNK_SIZE = 20;
|
||||
public final HashMap<String, EntityLogicInterface> logicList = new HashMap<>();
|
||||
@ -26,6 +29,7 @@ public class EntityLogicProvider {
|
||||
}
|
||||
|
||||
public void update(Game game) {
|
||||
log.debug("Updating all entities");
|
||||
int[] playerLocation = game.getPlayerCords();
|
||||
int playerX = playerLocation[0];
|
||||
int playerY = playerLocation[1];
|
||||
@ -56,11 +60,15 @@ public class EntityLogicProvider {
|
||||
if (!logicList.containsKey(entityLogicMobDTO.getMob().getBlockId())) {
|
||||
return;
|
||||
}
|
||||
var logic = logicList.get(entityLogicMobDTO.getMob().getBlockId());
|
||||
|
||||
logicList.get(entityLogicMobDTO.getMob().getBlockId()).nextIteration(entityLogicMobDTO);
|
||||
log.debug("Updating entity {} using logic {} on coordinates x:{},y:{}", entityLogicMobDTO.getMob().getBlockId(),
|
||||
logic.getClass().getSimpleName(), entityLogicMobDTO.getX(), entityLogicMobDTO.getY());
|
||||
logic.nextIteration(entityLogicMobDTO);
|
||||
}
|
||||
|
||||
public EntityLogicProvider() {
|
||||
log.info("Loading entity logic");
|
||||
registerHandlers();
|
||||
}
|
||||
|
||||
@ -74,6 +82,7 @@ public class EntityLogicProvider {
|
||||
EntityLogicInterface instance = (EntityLogicInterface) clazz.getDeclaredConstructor().newInstance();
|
||||
EntityLogic annotation = clazz.getAnnotation(EntityLogic.class);
|
||||
logicList.put(annotation.value(), instance);
|
||||
log.info("Loaded logic for entity {}", annotation.value());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -8,10 +8,12 @@ import cz.jzitnik.game.Game;
|
||||
import cz.jzitnik.game.annotations.ThreadRegistry;
|
||||
import cz.jzitnik.game.entities.Player;
|
||||
import cz.jzitnik.tui.ScreenRenderer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@Slf4j
|
||||
public class ThreadProvider {
|
||||
private final Game game;
|
||||
private final ScreenRenderer screenRenderer;
|
||||
@ -20,6 +22,7 @@ public class ThreadProvider {
|
||||
private final List<Thread> list = new ArrayList<>();
|
||||
|
||||
public void start() {
|
||||
log.info("Loading all the threads");
|
||||
for (Thread thread : list) {
|
||||
thread.start();
|
||||
}
|
||||
|
@ -4,8 +4,12 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ResourceLoader {
|
||||
public static String loadResource(String fileName) {
|
||||
log.debug("Loading resource: {}", "textures/" + fileName);
|
||||
try (InputStream inputStream = ResourceLoader.class.getClassLoader()
|
||||
.getResourceAsStream("textures/" + fileName)) {
|
||||
if (inputStream == null) {
|
||||
|
@ -11,12 +11,15 @@ import cz.jzitnik.game.ui.Healthbar;
|
||||
import cz.jzitnik.tui.utils.SpriteCombiner;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class ScreenRenderer {
|
||||
private final SpriteList spriteList;
|
||||
private final Terminal terminal;
|
||||
@ -43,6 +46,7 @@ public class ScreenRenderer {
|
||||
}
|
||||
|
||||
public synchronized void render(Game game) {
|
||||
log.debug("Rendering frame");
|
||||
var world = game.getWorld();
|
||||
StringBuilder main = new StringBuilder();
|
||||
main.append("\033[H\033[2J");
|
||||
@ -154,6 +158,7 @@ public class ScreenRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("Frame rendered");
|
||||
System.out.println(main);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user