Skip to content

Commit 37e4e62

Browse files
author
winspeednl
committed
Updated to v0.9
1 parent 1783f1d commit 37e4e62

File tree

10 files changed

+170
-70
lines changed

10 files changed

+170
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package me.winspeednl.libz.actions;
2+
3+
public abstract class Action {
4+
public boolean isStarted, isFinished, isBlocking;
5+
public long startTime, elapsed, duration;
6+
public ActionContainer owner;
7+
8+
public abstract void onStart();
9+
public abstract void onEnd();
10+
public abstract void update();
11+
12+
public void finish() {
13+
isFinished = true;
14+
isBlocking = false;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package me.winspeednl.libz.actions;
2+
3+
import java.util.ArrayList;
4+
5+
public class ActionContainer {
6+
7+
private ArrayList<Action> actions = new ArrayList<Action>();
8+
private boolean isRunning;
9+
10+
public void update() {
11+
if (isRunning) {
12+
ArrayList<Action> completedActions = null;
13+
14+
for (Action action : actions) {
15+
if (action.isStarted) action.update();
16+
else {
17+
action.onStart();
18+
action.isStarted = true;
19+
}
20+
if (action.isBlocking) break;
21+
if (action.isFinished) {
22+
action.onEnd();
23+
if (completedActions == null) completedActions = new ArrayList<Action>();
24+
completedActions.add(action);
25+
}
26+
}
27+
if (completedActions != null) for (Action action : completedActions) actions.remove(action);
28+
}
29+
}
30+
31+
public void start() {
32+
isRunning = true;
33+
}
34+
35+
public void stop() {
36+
isRunning = false;
37+
}
38+
39+
public boolean isEmpty() {
40+
return actions.isEmpty();
41+
}
42+
43+
public void pushFront(Action action) {
44+
if (actions.contains(action)) {
45+
int id = actions.indexOf(action);
46+
if (id + 1 < actions.size()) {
47+
actions.remove(action);
48+
actions.add(id + 1, action);
49+
}
50+
}
51+
}
52+
53+
public void pushBack(Action action) {
54+
if (actions.contains(action)) {
55+
int id = actions.indexOf(action);
56+
if (id - 1 > 0) {
57+
actions.remove(action);
58+
actions.add(id - 1, action);
59+
}
60+
}
61+
}
62+
63+
public void insertAfter(Action action, Action newAction) {
64+
int id = actions.indexOf(action);
65+
actions.add(id + 1, newAction);
66+
}
67+
68+
public void insertBehind(Action action, Action newAction) {
69+
int id = actions.indexOf(action);
70+
actions.add(id - 1, newAction);
71+
}
72+
73+
public void add(Action action) {
74+
actions.add(action);
75+
}
76+
}

Java/src/me/winspeednl/libz/audio/Sound.java

+41-35
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,46 @@
88
import javax.sound.sampled.DataLine;
99

1010
public class Sound {
11-
AudioInputStream audioInputStream;
12-
Clip clip;
13-
14-
public Sound(String path) {
15-
try {
16-
BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream(path));
17-
audioInputStream = AudioSystem.getAudioInputStream(myStream);
18-
19-
DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
20-
clip = (Clip)AudioSystem.getLine(info);
21-
clip.open(audioInputStream);
22-
} catch (Exception e) {
23-
e.printStackTrace();
11+
private AudioInputStream audioInputStream;
12+
private Clip clip;
13+
private String path;
14+
15+
public Sound(String path) {
16+
this.path = path;
17+
try {
18+
BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream(path));
19+
audioInputStream = AudioSystem.getAudioInputStream(myStream);
20+
21+
DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
22+
clip = (Clip)AudioSystem.getLine(info);
23+
clip.open(audioInputStream);
24+
} catch (Exception e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
public void play() {
30+
clip.setFramePosition(0);
31+
clip.start();
32+
}
33+
34+
public void loop() {
35+
clip.loop(Clip.LOOP_CONTINUOUSLY);
36+
}
37+
38+
public void loop(int count) {
39+
clip.loop(count);
40+
}
41+
42+
public void stop() {
43+
clip.stop();
44+
}
45+
46+
public boolean isPlaying() {
47+
return clip.isRunning();
48+
}
49+
50+
public String getPath() {
51+
return path;
2452
}
25-
}
26-
27-
public void play() {
28-
clip.setFramePosition(0);
29-
clip.start();
30-
}
31-
32-
public void loop() {
33-
clip.loop(Clip.LOOP_CONTINUOUSLY);
34-
}
35-
36-
public void loop(int count) {
37-
clip.loop(count);
38-
}
39-
40-
public void stop() {
41-
clip.stop();
42-
}
43-
44-
public boolean isPlaying() {
45-
return clip.isRunning();
46-
}
4753
}

Java/src/me/winspeednl/libz/core/GameCore.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.winspeednl.libz.core;
22

3+
import java.awt.Dimension;
4+
35
import me.winspeednl.libz.screen.Render;
46
import me.winspeednl.libz.screen.Screen;
57

@@ -16,7 +18,7 @@ public class GameCore implements Runnable {
1618
private String title = "LibZ";
1719

1820
private double frameCap = 1D / 60D;
19-
private boolean isRunning = false, fullscreen = false, undecorated = false, screenSizeLocked = false;
21+
private boolean fullscreen = false, undecorated = false, screenSizeLocked = false;
2022
private int spriteBGColor = 0x00000000;
2123
private int fps = 0;
2224
private int offsetX, offsetY;
@@ -25,24 +27,18 @@ public GameCore(LibZ game) {
2527
this.game = game;
2628
}
2729

28-
public void start() {
29-
if (isRunning)
30-
return;
31-
30+
public void start() {
3231
screen = new Screen(this);
3332
renderer = new Render(this);
3433
input = new Input(this);
3534
game.init(this);
3635

37-
isRunning = true;
3836
thread = new Thread(this);
3937
thread.start();
4038
}
4139

4240
public void stop() {
43-
if (!isRunning)
44-
return;
45-
isRunning = false;
41+
thread.interrupt();
4642
}
4743

4844
public void run() {
@@ -53,7 +49,7 @@ public void run() {
5349
double frameTime = 0;
5450
int frames = 0;
5551

56-
while (isRunning) {
52+
while (!thread.isInterrupted()) {
5753
boolean render = false;
5854

5955
currTime = System.nanoTime() / 1000000000D;
@@ -131,6 +127,20 @@ public void setHeight(int height) {
131127
if (!screenSizeLocked)
132128
this.height = height;
133129
}
130+
131+
public void setSize(int width, int height) {
132+
if (!screenSizeLocked) {
133+
this.width = width;
134+
this.height = height;
135+
}
136+
}
137+
138+
public void setSize(Dimension dimension) {
139+
if (!screenSizeLocked) {
140+
this.width = dimension.width;
141+
this.height = dimension.height;
142+
}
143+
}
134144

135145
public int getScale() {
136146
return scale;

Java/src/me/winspeednl/libz/gui/Menu.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public abstract class Menu {
1414
private String guideText = "";
1515
private int selected = 0;
1616
private Font font = Font.STANDARDX4;
17-
private boolean keyPressed = false, useArrowKeys = false, useMouse = false;
17+
private boolean keyPressed = false, useArrowKeys = false;
1818

1919
public Menu(GameCore gc, ArrayList<String> items) {
2020
this.items = items;
@@ -43,19 +43,12 @@ public void update(GameCore gc) {
4343
}
4444
keyPressed = gc.getInput().isKeyPressed(KeyEvent.VK_UP) || gc.getInput().isKeyPressed(KeyEvent.VK_DOWN) || gc.getInput().isKeyPressed(KeyEvent.VK_ENTER);
4545
}
46-
if (useMouse) {
47-
48-
}
4946
}
5047

5148
public void arrowKeys(boolean bool) {
5249
useArrowKeys = bool;
5350
}
5451

55-
public void mouse(boolean bool) {
56-
useMouse = bool;
57-
}
58-
5952
public void render(GameCore gc, Render r) {
6053
for (int i = 0; i < pixels.length; i++)
6154
pixels[i] = backgroundColor;

Java/src/me/winspeednl/libz/level/Tile.java

-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ public int[] flip() {
146146
for(int i = 0; i < newPixels.length; i++) {
147147
newPixels[i] = 0xFF000000;
148148
}
149-
// setPixels(newPixels);
150149
}
151150
return newPixels;
152151
}

Java/src/me/winspeednl/libz/logger/Logger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void minimalOutput(boolean bool) {
2929
minimalOutput = bool;
3030
}
3131

32-
public void log(String Type, String ansiColor, String message) {
32+
private void log(String Type, String ansiColor, String message) {
3333
LocalDateTime currentTime = LocalDateTime.now();
3434
String timeStamp = "[" + currentTime.getHour() + ":" + currentTime.getMinute() + ":" + currentTime.getSecond() + "] ";
3535

Java/src/me/winspeednl/libz/pathfinding/AStar.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ private void AStarCalculator(){
5050
Node current;
5151
while(true){
5252
current = open.poll();
53-
if(current == null)
54-
break;
53+
if(current == null) break;
5554
closed[current.getX()][current.getY()] = true;
56-
if(current.equals(grid[endX][endY])) {
57-
return;
58-
}
55+
if(current.equals(grid[endX][endY])) return;
5956
Node t;
57+
6058
if(current.getX() - 1 >= 0){
6159
t = grid[current.getX() - 1][current.getY()];
6260
checkAndUpdateCost(current, t, current.getFCost() + V_H_COST);
@@ -70,15 +68,18 @@ private void AStarCalculator(){
7068
checkAndUpdateCost(current, t, current.getFCost() + DIAGONAL_COST);
7169
}
7270
}
73-
}
71+
}
72+
7473
if(current.getY() - 1 >= 0){
7574
t = grid[current.getX()][current.getY() - 1];
7675
checkAndUpdateCost(current, t, current.getFCost() + V_H_COST);
7776
}
77+
7878
if(current.getY() + 1 < grid[0].length){
7979
t = grid[current.getX()][current.getY() + 1];
8080
checkAndUpdateCost(current, t, current.getFCost() + V_H_COST);
8181
}
82+
8283
if(current.getX() + 1 < grid.length){
8384
t = grid[current.getX() + 1][current.getY()];
8485
checkAndUpdateCost(current, t, current.getFCost() + V_H_COST);
@@ -105,16 +106,15 @@ public ArrayList<Node> calculate(int width, int height, int sx, int sy, int ex,
105106
open = new PriorityQueue<>((Object o1, Object o2) -> {
106107
Node c1 = (Node)o1;
107108
Node c2 = (Node)o2;
108-
return c1.getFCost() < c2.getFCost() ? - 1:
109-
c1.getFCost() > c2.getFCost() ? 1 : 0;
109+
return c1.getFCost() < c2.getFCost() ? -1 : c1.getFCost() > c2.getFCost() ? 1 : 0;
110110
});
111111

112112
setStartCell(sx, sy);
113113
setEndCell(ex, ey);
114114
for(int x = 0; x < width; x++){
115115
for(int y = 0; y < height; y++){
116116
grid[x][y] = new Node(x, y);
117-
grid[x][y].setHCost(Math.abs(x-ex)+Math.abs(x-ey));
117+
grid[x][y].setHCost(Math.abs(x - ex) + Math.abs(x - ey));
118118
}
119119
}
120120
grid[sx][sy].setFCost(0);
@@ -131,13 +131,13 @@ public ArrayList<Node> calculate(int width, int height, int sx, int sy, int ex,
131131

132132
if(closed[endX][endY]){
133133
Node current = grid[endX][endY];
134-
int X = Integer.parseInt(current.toString().split(",")[0]);
135-
int Y = Integer.parseInt(current.toString().split(",")[1]);
134+
int X = Integer.parseInt(current.toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[0]);
135+
int Y = Integer.parseInt(current.toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[1]);
136136
path.add(new Node(X, Y));
137137

138138
while(current.getParent() != null){
139-
X = Integer.parseInt(current.getParent().toString().split(",")[0]);
140-
Y = Integer.parseInt(current.getParent().toString().split(",")[1]);
139+
X = Integer.parseInt(current.getParent().toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[0]);
140+
Y = Integer.parseInt(current.getParent().toString().replaceAll("Node(", "").replaceAll(")", "").split(",")[1]);
141141
path.add(new Node(X, Y));
142142
current = current.getParent();
143143
}

Java/src/me/winspeednl/libz/pathfinding/Node.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public int getFCost() {
5353

5454
@Override
5555
public String toString(){
56-
return this.getX() + "," + this.getY();
56+
return "Node(" + this.getX() + "," + this.getY() + ")";
5757
}
5858
}

Java/src/me/winspeednl/libz/tasks/Task.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class Task {
44

5-
private String name = "";
5+
private String name = "Default";
66
private int ticks = 0, interval = 0, executedCount = 0, maxExecutes = 0;
77
private boolean repeat = false, isAlive = true, isRunning = false;
88

0 commit comments

Comments
 (0)