Skip to content

Commit

Permalink
Updated to v0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
winspeednl committed Oct 21, 2016
1 parent 1783f1d commit 37e4e62
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 70 deletions.
16 changes: 16 additions & 0 deletions Java/src/me/winspeednl/libz/actions/Action.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
76 changes: 76 additions & 0 deletions Java/src/me/winspeednl/libz/actions/ActionContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package me.winspeednl.libz.actions;

import java.util.ArrayList;

public class ActionContainer {

private ArrayList<Action> actions = new ArrayList<Action>();
private boolean isRunning;

public void update() {
if (isRunning) {
ArrayList<Action> 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<Action>();
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);
}
}
76 changes: 41 additions & 35 deletions Java/src/me/winspeednl/libz/audio/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
30 changes: 20 additions & 10 deletions Java/src/me/winspeednl/libz/core/GameCore.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions Java/src/me/winspeednl/libz/gui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> items) {
this.items = items;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Java/src/me/winspeednl/libz/level/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public int[] flip() {
for(int i = 0; i < newPixels.length; i++) {
newPixels[i] = 0xFF000000;
}
// setPixels(newPixels);
}
return newPixels;
}
Expand Down
2 changes: 1 addition & 1 deletion Java/src/me/winspeednl/libz/logger/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "] ";

Expand Down
26 changes: 13 additions & 13 deletions Java/src/me/winspeednl/libz/pathfinding/AStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -105,16 +106,15 @@ public ArrayList<Node> 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);
setEndCell(ex, ey);
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);
Expand All @@ -131,13 +131,13 @@ public ArrayList<Node> 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();
}
Expand Down
2 changes: 1 addition & 1 deletion Java/src/me/winspeednl/libz/pathfinding/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public int getFCost() {

@Override
public String toString(){
return this.getX() + "," + this.getY();
return "Node(" + this.getX() + "," + this.getY() + ")";
}
}
2 changes: 1 addition & 1 deletion Java/src/me/winspeednl/libz/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 37e4e62

Please sign in to comment.