diff --git a/Java/src/me/winspeednl/libz/actions/Action.java b/Java/src/me/winspeednl/libz/actions/Action.java new file mode 100644 index 0000000..b46eb30 --- /dev/null +++ b/Java/src/me/winspeednl/libz/actions/Action.java @@ -0,0 +1,16 @@ +package me.winspeednl.libz.actions; + +public abstract class Action { + public boolean isStarted, isFinished, isBlocking; + public long startTime, elapsed, duration; + public ActionContainer owner; + + public abstract void onStart(); + public abstract void onEnd(); + public abstract void update(); + + public void finish() { + isFinished = true; + isBlocking = false; + } +} \ No newline at end of file diff --git a/Java/src/me/winspeednl/libz/actions/ActionContainer.java b/Java/src/me/winspeednl/libz/actions/ActionContainer.java new file mode 100644 index 0000000..3409462 --- /dev/null +++ b/Java/src/me/winspeednl/libz/actions/ActionContainer.java @@ -0,0 +1,76 @@ +package me.winspeednl.libz.actions; + +import java.util.ArrayList; + +public class ActionContainer { + + private ArrayList actions = new ArrayList(); + private boolean isRunning; + + public void update() { + if (isRunning) { + ArrayList completedActions = null; + + for (Action action : actions) { + if (action.isStarted) action.update(); + else { + action.onStart(); + action.isStarted = true; + } + if (action.isBlocking) break; + if (action.isFinished) { + action.onEnd(); + if (completedActions == null) completedActions = new ArrayList(); + completedActions.add(action); + } + } + if (completedActions != null) for (Action action : completedActions) actions.remove(action); + } + } + + public void start() { + isRunning = true; + } + + public void stop() { + isRunning = false; + } + + public boolean isEmpty() { + return actions.isEmpty(); + } + + public void pushFront(Action action) { + if (actions.contains(action)) { + int id = actions.indexOf(action); + if (id + 1 < actions.size()) { + actions.remove(action); + actions.add(id + 1, action); + } + } + } + + public void pushBack(Action action) { + if (actions.contains(action)) { + int id = actions.indexOf(action); + if (id - 1 > 0) { + actions.remove(action); + actions.add(id - 1, action); + } + } + } + + public void insertAfter(Action action, Action newAction) { + int id = actions.indexOf(action); + actions.add(id + 1, newAction); + } + + public void insertBehind(Action action, Action newAction) { + int id = actions.indexOf(action); + actions.add(id - 1, newAction); + } + + public void add(Action action) { + actions.add(action); + } +} diff --git a/Java/src/me/winspeednl/libz/audio/Sound.java b/Java/src/me/winspeednl/libz/audio/Sound.java index 4df10f8..ccd7bd7 100644 --- a/Java/src/me/winspeednl/libz/audio/Sound.java +++ b/Java/src/me/winspeednl/libz/audio/Sound.java @@ -8,40 +8,46 @@ import javax.sound.sampled.DataLine; public class Sound { - AudioInputStream audioInputStream; - Clip clip; - - public Sound(String path) { - try { - BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream(path)); - audioInputStream = AudioSystem.getAudioInputStream(myStream); - - DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat()); - clip = (Clip)AudioSystem.getLine(info); - clip.open(audioInputStream); - } catch (Exception e) { - e.printStackTrace(); + private AudioInputStream audioInputStream; + private Clip clip; + private String path; + + public Sound(String path) { + this.path = path; + try { + BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream(path)); + audioInputStream = AudioSystem.getAudioInputStream(myStream); + + DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat()); + clip = (Clip)AudioSystem.getLine(info); + clip.open(audioInputStream); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void play() { + clip.setFramePosition(0); + clip.start(); + } + + public void loop() { + clip.loop(Clip.LOOP_CONTINUOUSLY); + } + + public void loop(int count) { + clip.loop(count); + } + + public void stop() { + clip.stop(); + } + + public boolean isPlaying() { + return clip.isRunning(); + } + + public String getPath() { + return path; } - } - - public void play() { - clip.setFramePosition(0); - clip.start(); - } - - public void loop() { - clip.loop(Clip.LOOP_CONTINUOUSLY); - } - - public void loop(int count) { - clip.loop(count); - } - - public void stop() { - clip.stop(); - } - - public boolean isPlaying() { - return clip.isRunning(); - } } diff --git a/Java/src/me/winspeednl/libz/core/GameCore.java b/Java/src/me/winspeednl/libz/core/GameCore.java index a8d308a..0263970 100644 --- a/Java/src/me/winspeednl/libz/core/GameCore.java +++ b/Java/src/me/winspeednl/libz/core/GameCore.java @@ -1,5 +1,7 @@ package me.winspeednl.libz.core; +import java.awt.Dimension; + import me.winspeednl.libz.screen.Render; import me.winspeednl.libz.screen.Screen; @@ -16,7 +18,7 @@ public class GameCore implements Runnable { private String title = "LibZ"; private double frameCap = 1D / 60D; - private boolean isRunning = false, fullscreen = false, undecorated = false, screenSizeLocked = false; + private boolean fullscreen = false, undecorated = false, screenSizeLocked = false; private int spriteBGColor = 0x00000000; private int fps = 0; private int offsetX, offsetY; @@ -25,24 +27,18 @@ public GameCore(LibZ game) { this.game = game; } - public void start() { - if (isRunning) - return; - + public void start() { screen = new Screen(this); renderer = new Render(this); input = new Input(this); game.init(this); - isRunning = true; thread = new Thread(this); thread.start(); } public void stop() { - if (!isRunning) - return; - isRunning = false; + thread.interrupt(); } public void run() { @@ -53,7 +49,7 @@ public void run() { double frameTime = 0; int frames = 0; - while (isRunning) { + while (!thread.isInterrupted()) { boolean render = false; currTime = System.nanoTime() / 1000000000D; @@ -131,6 +127,20 @@ public void setHeight(int height) { if (!screenSizeLocked) this.height = height; } + + public void setSize(int width, int height) { + if (!screenSizeLocked) { + this.width = width; + this.height = height; + } + } + + public void setSize(Dimension dimension) { + if (!screenSizeLocked) { + this.width = dimension.width; + this.height = dimension.height; + } + } public int getScale() { return scale; diff --git a/Java/src/me/winspeednl/libz/gui/Menu.java b/Java/src/me/winspeednl/libz/gui/Menu.java index 645eb12..12678d0 100644 --- a/Java/src/me/winspeednl/libz/gui/Menu.java +++ b/Java/src/me/winspeednl/libz/gui/Menu.java @@ -14,7 +14,7 @@ public abstract class Menu { private String guideText = ""; private int selected = 0; private Font font = Font.STANDARDX4; - private boolean keyPressed = false, useArrowKeys = false, useMouse = false; + private boolean keyPressed = false, useArrowKeys = false; public Menu(GameCore gc, ArrayList items) { this.items = items; @@ -43,19 +43,12 @@ public void update(GameCore gc) { } keyPressed = gc.getInput().isKeyPressed(KeyEvent.VK_UP) || gc.getInput().isKeyPressed(KeyEvent.VK_DOWN) || gc.getInput().isKeyPressed(KeyEvent.VK_ENTER); } - if (useMouse) { - - } } public void arrowKeys(boolean bool) { useArrowKeys = bool; } - public void mouse(boolean bool) { - useMouse = bool; - } - public void render(GameCore gc, Render r) { for (int i = 0; i < pixels.length; i++) pixels[i] = backgroundColor; diff --git a/Java/src/me/winspeednl/libz/level/Tile.java b/Java/src/me/winspeednl/libz/level/Tile.java index bd41ebd..9c59bc3 100644 --- a/Java/src/me/winspeednl/libz/level/Tile.java +++ b/Java/src/me/winspeednl/libz/level/Tile.java @@ -146,7 +146,6 @@ public int[] flip() { for(int i = 0; i < newPixels.length; i++) { newPixels[i] = 0xFF000000; } - // setPixels(newPixels); } return newPixels; } diff --git a/Java/src/me/winspeednl/libz/logger/Logger.java b/Java/src/me/winspeednl/libz/logger/Logger.java index 95f5347..c36a990 100644 --- a/Java/src/me/winspeednl/libz/logger/Logger.java +++ b/Java/src/me/winspeednl/libz/logger/Logger.java @@ -29,7 +29,7 @@ public void minimalOutput(boolean bool) { minimalOutput = bool; } - public void log(String Type, String ansiColor, String message) { + private void log(String Type, String ansiColor, String message) { LocalDateTime currentTime = LocalDateTime.now(); String timeStamp = "[" + currentTime.getHour() + ":" + currentTime.getMinute() + ":" + currentTime.getSecond() + "] "; diff --git a/Java/src/me/winspeednl/libz/pathfinding/AStar.java b/Java/src/me/winspeednl/libz/pathfinding/AStar.java index 9969b34..26b85ce 100644 --- a/Java/src/me/winspeednl/libz/pathfinding/AStar.java +++ b/Java/src/me/winspeednl/libz/pathfinding/AStar.java @@ -50,13 +50,11 @@ private void AStarCalculator(){ Node current; while(true){ current = open.poll(); - if(current == null) - break; + if(current == null) break; closed[current.getX()][current.getY()] = true; - if(current.equals(grid[endX][endY])) { - return; - } + if(current.equals(grid[endX][endY])) return; Node t; + if(current.getX() - 1 >= 0){ t = grid[current.getX() - 1][current.getY()]; checkAndUpdateCost(current, t, current.getFCost() + V_H_COST); @@ -70,15 +68,18 @@ private void AStarCalculator(){ checkAndUpdateCost(current, t, current.getFCost() + DIAGONAL_COST); } } - } + } + if(current.getY() - 1 >= 0){ t = grid[current.getX()][current.getY() - 1]; checkAndUpdateCost(current, t, current.getFCost() + V_H_COST); } + if(current.getY() + 1 < grid[0].length){ t = grid[current.getX()][current.getY() + 1]; checkAndUpdateCost(current, t, current.getFCost() + V_H_COST); } + if(current.getX() + 1 < grid.length){ t = grid[current.getX() + 1][current.getY()]; checkAndUpdateCost(current, t, current.getFCost() + V_H_COST); @@ -105,8 +106,7 @@ public ArrayList calculate(int width, int height, int sx, int sy, int ex, open = new PriorityQueue<>((Object o1, Object o2) -> { Node c1 = (Node)o1; Node c2 = (Node)o2; - return c1.getFCost() < c2.getFCost() ? - 1: - c1.getFCost() > c2.getFCost() ? 1 : 0; + return c1.getFCost() < c2.getFCost() ? -1 : c1.getFCost() > c2.getFCost() ? 1 : 0; }); setStartCell(sx, sy); @@ -114,7 +114,7 @@ public ArrayList calculate(int width, int height, int sx, int sy, int ex, for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ grid[x][y] = new Node(x, y); - grid[x][y].setHCost(Math.abs(x-ex)+Math.abs(x-ey)); + grid[x][y].setHCost(Math.abs(x - ex) + Math.abs(x - ey)); } } grid[sx][sy].setFCost(0); @@ -131,13 +131,13 @@ public ArrayList calculate(int width, int height, int sx, int sy, int ex, if(closed[endX][endY]){ Node current = grid[endX][endY]; - int X = Integer.parseInt(current.toString().split(",")[0]); - int Y = Integer.parseInt(current.toString().split(",")[1]); + int X = Integer.parseInt(current.toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[0]); + int Y = Integer.parseInt(current.toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[1]); path.add(new Node(X, Y)); while(current.getParent() != null){ - X = Integer.parseInt(current.getParent().toString().split(",")[0]); - Y = Integer.parseInt(current.getParent().toString().split(",")[1]); + X = Integer.parseInt(current.getParent().toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[0]); + Y = Integer.parseInt(current.getParent().toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[1]); path.add(new Node(X, Y)); current = current.getParent(); } diff --git a/Java/src/me/winspeednl/libz/pathfinding/Node.java b/Java/src/me/winspeednl/libz/pathfinding/Node.java index 1ca5161..9706703 100644 --- a/Java/src/me/winspeednl/libz/pathfinding/Node.java +++ b/Java/src/me/winspeednl/libz/pathfinding/Node.java @@ -53,6 +53,6 @@ public int getFCost() { @Override public String toString(){ - return this.getX() + "," + this.getY(); + return "Node(" + this.getX() + "," + this.getY() + ")"; } } diff --git a/Java/src/me/winspeednl/libz/tasks/Task.java b/Java/src/me/winspeednl/libz/tasks/Task.java index 6405b47..4c67de5 100644 --- a/Java/src/me/winspeednl/libz/tasks/Task.java +++ b/Java/src/me/winspeednl/libz/tasks/Task.java @@ -2,7 +2,7 @@ public class Task { - private String name = ""; + private String name = "Default"; private int ticks = 0, interval = 0, executedCount = 0, maxExecutes = 0; private boolean repeat = false, isAlive = true, isRunning = false;