Initial commit
This commit is contained in:
104
Java/GameEngine_old/src/de/craftix/engine/Display.java
Normal file
104
Java/GameEngine_old/src/de/craftix/engine/Display.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import de.craftix.engine.var.Inputs;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Display extends JLabel {
|
||||
private static Display instance;
|
||||
private static int framesD;
|
||||
private final int width, height;
|
||||
public static Timer framesT;
|
||||
|
||||
public Display(int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
instance = this;
|
||||
framesD = 0;
|
||||
framesT = new Timer(1000, e -> {
|
||||
GameEngine.frames = framesD;
|
||||
framesD = 0;
|
||||
});
|
||||
InputManager inputManager = new InputManager();
|
||||
setSize(width, height);
|
||||
setFocusable(true);
|
||||
addKeyListener(inputManager);
|
||||
addMouseListener(inputManager);
|
||||
requestFocus();
|
||||
}
|
||||
|
||||
public void addInputs(Inputs inputs) {
|
||||
addKeyListener(inputs);
|
||||
addMouseListener(inputs);
|
||||
addMouseMotionListener(inputs);
|
||||
addMouseWheelListener(inputs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
framesD++;
|
||||
GameEngine.getInstance().update();
|
||||
GameEngine.getScene().update();
|
||||
super.paintComponent(g);
|
||||
|
||||
if (GameEngine.antialiasing()) ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
else ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
||||
|
||||
if (GameEngine.getScene().getBackgroundAsImage() != null) {
|
||||
if (GameEngine.getScene().getBackgroundAsImage().getWidth() == getWidth() && GameEngine.getScene().getBackgroundAsImage().getHeight() == getHeight())
|
||||
g.drawImage(GameEngine.getScene().getBackgroundAsImage(), 0, 0, null);
|
||||
else g.drawImage(Resizer.AVERAGE.resize(GameEngine.getScene().getBackgroundAsImage(), getWidth(), getHeight()), 0, 0, null);
|
||||
}
|
||||
if (GameEngine.getScene().getBackgroundAsColor() != null) { g.setColor(GameEngine.getScene().getBackgroundAsColor()); g.fillRect(0, 0, getWidth(), getHeight()); }
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
if (GameEngine.showGrid()) {
|
||||
for (int x = 0; x < (getWidth() / GameEngine.getGridSize()) + 1; x++) g.drawLine(x * GameEngine.getGridSize(), 0, x * GameEngine.getGridSize(), getHeight());
|
||||
for (int y = 0; y < (getHeight() / GameEngine.getGridSize()) + 1; y++) g.drawLine(0, y * GameEngine.getGridSize(), getWidth(), y * GameEngine.getGridSize());
|
||||
}
|
||||
|
||||
GameEngine.getScene().renderComponents((Graphics2D) g);
|
||||
|
||||
if (GameEngine.showFrames()) {
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillRect(9, 2, 50, 9);
|
||||
g.setColor(Color.WHITE);
|
||||
g.setFont(new Font("Tacoma", Font.BOLD, 10));
|
||||
g.drawString("FPS: " + GameEngine.frames, 10, 10);
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
public static Rectangle calculateDisplayPosition(Vector2 pos, Vector2 size) {
|
||||
Vector2 res = new Vector2();
|
||||
Vector2 mid = new Vector2(instance.getWidth(), instance.getHeight()).divide(2, 2);
|
||||
Vector2 s = size.copy().divide(2, 2);
|
||||
res.subtract(GameEngine.getCamera().x, -GameEngine.getCamera().y);
|
||||
res.subtract(-pos.x, pos.y);
|
||||
res.subtract(s);
|
||||
res.add(mid);
|
||||
return new Rectangle(res.convert().x, res.convert().y, size.convert().x, size.convert().y);
|
||||
}
|
||||
|
||||
public static Vector2 calculateVirtualPosition(Vector2 pos) {
|
||||
Vector2 res = new Vector2();
|
||||
Vector2 mid = new Vector2(instance.getWidth(), instance.getHeight()).divide(2, 2);
|
||||
res.add(GameEngine.getCamera().x, -GameEngine.getCamera().y);
|
||||
res.add(pos.x, pos.y);
|
||||
res.subtract(mid);
|
||||
res.multiply(1, -1);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static boolean containsObject(Rectangle screenRect) {
|
||||
Rectangle self = new Rectangle(0, 0, instance.getWidth(), instance.getHeight());
|
||||
return self.intersects(screenRect);
|
||||
}
|
||||
|
||||
public static Display getInstance() { return instance; }
|
||||
public int getWidth() { return width; }
|
||||
public int getHeight() { return height; }
|
||||
}
|
||||
170
Java/GameEngine_old/src/de/craftix/engine/GameEngine.java
Normal file
170
Java/GameEngine_old/src/de/craftix/engine/GameEngine.java
Normal file
@@ -0,0 +1,170 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import de.craftix.engine.objects.GameObject;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.var.Inputs;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import javax.swing.Timer;
|
||||
import java.awt.*;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
public class GameEngine extends JFrame {
|
||||
public static int frames = 0;
|
||||
private static GameEngine instance;
|
||||
private static boolean showFrames;
|
||||
private static boolean showGrid;
|
||||
private static boolean antialiasing;
|
||||
private static int gridSize;
|
||||
private static Timer timer;
|
||||
private static Timer physics;
|
||||
private static boolean running = false;
|
||||
private static Vector2 camera;
|
||||
private static HashMap<String, Float> layer;
|
||||
|
||||
private static Display screen;
|
||||
private static Scene scene;
|
||||
private static Inputs inputs;
|
||||
|
||||
public static void setup(int width, int height, String title, int tps, GameEngine instance) {
|
||||
GameEngine.instance = instance;
|
||||
instance.setSize(width + 17, height + 40);
|
||||
instance.setResizable(false);
|
||||
instance.setBackground(Color.BLACK);
|
||||
instance.setTitle(title);
|
||||
instance.setLocationRelativeTo(null);
|
||||
instance.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
scene = new Scene();
|
||||
screen = new Display(width, height);
|
||||
camera = new Vector2();
|
||||
layer = new HashMap<>();
|
||||
layer.put("Background", -10f);
|
||||
layer.put("Default", 0f);
|
||||
layer.put("Foreground", 10f);
|
||||
showFrames(false);
|
||||
showGrid(false);
|
||||
setAntialiasing(false);
|
||||
setGridSize(100);
|
||||
|
||||
instance.addWindowListener(new WindowListener() {
|
||||
@Override
|
||||
public void windowOpened(WindowEvent e) { }
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) { stopGame(); }
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) { }
|
||||
@Override
|
||||
public void windowIconified(WindowEvent e) { }
|
||||
@Override
|
||||
public void windowDeiconified(WindowEvent e) { }
|
||||
@Override
|
||||
public void windowActivated(WindowEvent e) { }
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent e) { }
|
||||
});
|
||||
|
||||
timer = new Timer(1000 / tps, e -> { scene.fixedUpdate(); instance.fixedUpdate(); });
|
||||
physics = new Timer(5, e -> instance.physicsUpdate());
|
||||
}
|
||||
|
||||
public static void startGame() {
|
||||
instance.initialise();
|
||||
running = true;
|
||||
timer.start();
|
||||
physics.start();
|
||||
Display.framesT.start();
|
||||
instance.setContentPane(screen);
|
||||
instance.start();
|
||||
scene.start();
|
||||
instance.setVisible(true);
|
||||
}
|
||||
|
||||
public static void stopGame() {
|
||||
instance.setVisible(false);
|
||||
running = false;
|
||||
timer.stop();
|
||||
physics.stop();
|
||||
Display.framesT.stop();
|
||||
instance.stop();
|
||||
scene.stop();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public static void setIcon(BufferedImage image) {
|
||||
ImageIcon icon = new ImageIcon(image);
|
||||
instance.setIconImage(icon.getImage());
|
||||
}
|
||||
|
||||
public static File loadFile(String path) {
|
||||
try {
|
||||
return new File(GameEngine.class.getClassLoader().getResource(path).getPath());
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
public static File loadExternalFile(String path) {
|
||||
try {
|
||||
return new File(path);
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static BufferedImage loadImage(String path) {
|
||||
try {
|
||||
return ImageIO.read(Objects.requireNonNull(GameEngine.class.getClassLoader().getResource(path)));
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
public static BufferedImage loadExternalImage(String path) {
|
||||
try {
|
||||
return ImageIO.read(new File(path));
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
private void physicsUpdate() {
|
||||
for (GameObject object : getScene().getGameObjects()) {
|
||||
if (object.getPhysics() != null) object.getPhysics().calculatePhysics();
|
||||
}
|
||||
}
|
||||
|
||||
public static GameEngine getInstance() { return instance; }
|
||||
public static boolean isRunning() { return running; }
|
||||
public static Scene getScene() { return scene; }
|
||||
public static UIManager getUIManager() { return scene.getUIManager(); }
|
||||
public static Display getScreen() { return screen; }
|
||||
public static Inputs getInputs() { return inputs; }
|
||||
public static boolean showFrames() { return showFrames; }
|
||||
public static boolean showGrid() { return showGrid; }
|
||||
public static boolean antialiasing() { return antialiasing; }
|
||||
public static int getGridSize() { return gridSize; }
|
||||
public static Vector2 getCamera() { return camera; }
|
||||
public static float getLayer(String name) { return layer.get(name); }
|
||||
public static List<Float> getSortedLayers() {
|
||||
List<Float> mapValues = new ArrayList<>(layer.values());
|
||||
Collections.sort(mapValues);
|
||||
return mapValues;
|
||||
}
|
||||
|
||||
public static void setScene(Scene scene) { GameEngine.scene = scene; }
|
||||
public static void showFrames(boolean value) { showFrames = value; }
|
||||
public static void showGrid(boolean value) { showGrid = value; }
|
||||
public static void setAntialiasing(boolean value) { antialiasing = value; }
|
||||
public static void setGridSize(int gridSize) { GameEngine.gridSize = gridSize; }
|
||||
public static void setCamera(float x, float y) { camera = new Vector2(x, y); }
|
||||
public static void setInputs(Inputs inputs) { GameEngine.inputs = inputs; screen.addInputs(inputs); }
|
||||
public static void addLayer(String name, float layer) { GameEngine.layer.put(name, layer); }
|
||||
public static void removeLayer(String name) { if (!name.equalsIgnoreCase("Default")) GameEngine.layer.remove(name); }
|
||||
|
||||
protected void fixedUpdate() {}
|
||||
protected void update() {}
|
||||
protected void start() {}
|
||||
protected void stop() {}
|
||||
protected void initialise() {}
|
||||
}
|
||||
33
Java/GameEngine_old/src/de/craftix/engine/InputManager.java
Normal file
33
Java/GameEngine_old/src/de/craftix/engine/InputManager.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import de.craftix.engine.var.Inputs;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class InputManager extends Inputs {
|
||||
|
||||
private static final boolean[] keys = new boolean[256];
|
||||
private static final boolean[] mouseButtons = new boolean[MouseInfo.getNumberOfButtons()];
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) { mouseButtons[e.getButton() - 1] = true; }
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) { mouseButtons[e.getButton() - 1] = false; }
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) { keys[e.getKeyCode()] = true; }
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) { keys[e.getKeyCode()] = false; }
|
||||
|
||||
public static Vector2 getMouseRaw() {
|
||||
Point mouse = new Point(MouseInfo.getPointerInfo().getLocation());
|
||||
SwingUtilities.convertPointFromScreen(mouse, GameEngine.getScreen());
|
||||
return new Vector2(mouse);
|
||||
}
|
||||
public static Vector2 getMousePos() { return Display.calculateVirtualPosition(getMouseRaw()); }
|
||||
public static boolean isKeyPressed(int key) { return keys[key]; }
|
||||
public static boolean isMouseClicked(int button) { return mouseButtons[button - 1]; }
|
||||
}
|
||||
98
Java/GameEngine_old/src/de/craftix/engine/Resizer.java
Normal file
98
Java/GameEngine_old/src/de/craftix/engine/Resizer.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public enum Resizer {
|
||||
|
||||
NEAREST_NEIGHBOR {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
return commonResize(source, width, height,
|
||||
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
||||
}
|
||||
},
|
||||
BILINEAR {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
return commonResize(source, width, height,
|
||||
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
}
|
||||
},
|
||||
BICUBIC {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
return commonResize(source, width, height,
|
||||
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
}
|
||||
},
|
||||
PROGRESSIVE_BILINEAR {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
return progressiveResize(source, width, height,
|
||||
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
}
|
||||
},
|
||||
PROGRESSIVE_BICUBIC {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
return progressiveResize(source, width, height,
|
||||
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
}
|
||||
},
|
||||
AVERAGE {
|
||||
@Override
|
||||
public BufferedImage resize(BufferedImage source,
|
||||
int width, int height) {
|
||||
Image img2 = source.getScaledInstance(width, height,
|
||||
Image.SCALE_AREA_AVERAGING);
|
||||
BufferedImage img = new BufferedImage(width, height,
|
||||
source.getType());
|
||||
Graphics2D g = img.createGraphics();
|
||||
try {
|
||||
g.drawImage(img2, 0, 0, width, height, null);
|
||||
} finally {
|
||||
g.dispose();
|
||||
}
|
||||
return img;
|
||||
}
|
||||
};
|
||||
|
||||
public abstract BufferedImage resize(BufferedImage source,
|
||||
int width, int height);
|
||||
|
||||
private static BufferedImage progressiveResize(BufferedImage source,
|
||||
int width, int height, Object hint) {
|
||||
int w = Math.max(source.getWidth()/2, width);
|
||||
int h = Math.max(source.getHeight()/2, height);
|
||||
BufferedImage img = commonResize(source, w, h, hint);
|
||||
while (w != width || h != height) {
|
||||
BufferedImage prev = img;
|
||||
w = Math.max(w/2, width);
|
||||
h = Math.max(h/2, height);
|
||||
img = commonResize(prev, w, h, hint);
|
||||
prev.flush();
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
private static BufferedImage commonResize(BufferedImage source,
|
||||
int width, int height, Object hint) {
|
||||
BufferedImage img = new BufferedImage(width, height,
|
||||
source.getType());
|
||||
Graphics2D g = img.createGraphics();
|
||||
try {
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
|
||||
g.drawImage(source, 0, 0, width, height, null);
|
||||
} finally {
|
||||
g.dispose();
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
}
|
||||
91
Java/GameEngine_old/src/de/craftix/engine/Scene.java
Normal file
91
Java/GameEngine_old/src/de/craftix/engine/Scene.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import de.craftix.engine.objects.ColliderObject;
|
||||
import de.craftix.engine.objects.GameObject;
|
||||
import de.craftix.engine.objects.Texture;
|
||||
import de.craftix.engine.ui.UIManager;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Scene {
|
||||
|
||||
private BufferedImage backgroundImage;
|
||||
private Color backgroundColor;
|
||||
private final ArrayList<GameObject> objects = new ArrayList<>();
|
||||
private final ArrayList<ColliderObject> colObjects = new ArrayList<>();
|
||||
private final ArrayList<Texture> textures = new ArrayList<>();
|
||||
private final UIManager uiManager = new UIManager();
|
||||
|
||||
public void setBackground(BufferedImage image) { this.backgroundImage = image; }
|
||||
public void setBackground(Color color) { this.backgroundColor = color; }
|
||||
public BufferedImage getBackgroundAsImage() { return backgroundImage; }
|
||||
public Color getBackgroundAsColor() { return backgroundColor; }
|
||||
|
||||
public void clearScene() { objects.clear(); colObjects.clear(); }
|
||||
|
||||
public void addGameObject(GameObject object) { objects.add(object); object.start(); }
|
||||
public void removeGameObject(GameObject object) { objects.remove(object); object.stop(); }
|
||||
public boolean containsGameObject(GameObject object) { return objects.contains(object); }
|
||||
public GameObject[] getGameObjects() { return objects.toArray(new GameObject[0]); }
|
||||
public GameObject[] getGameObjects(Vector2 pos) { ArrayList<GameObject> a = new ArrayList<>(); for (GameObject object : objects) if (object.getPosition().equals(pos)) a.add(object); return a.toArray(new GameObject[0]); }
|
||||
public GameObject[] getGameObjects(float layer) { ArrayList<GameObject> a = new ArrayList<>(); for (GameObject object : objects) if (object.getLayer() == layer) a.add(object); return a.toArray(new GameObject[0]); }
|
||||
public GameObject getGameObject(Vector2 pos) { for (GameObject object : objects) if (object.getPosition().equals(pos)) return object; return null; }
|
||||
public GameObject getGameObjectByRect(Vector2 pos) { for (GameObject object : objects) if (object.getRectangle().contains(pos.convert())) return object; return null; }
|
||||
|
||||
public void addColliderObject(ColliderObject object) { colObjects.add(object); object.start(); }
|
||||
public void removeColliderObject(ColliderObject object) { colObjects.remove(object); object.stop(); }
|
||||
public boolean containsColliderObject(ColliderObject object) { return colObjects.contains(object); }
|
||||
public ColliderObject[] getColliderObjects() { return colObjects.toArray(new ColliderObject[0]); }
|
||||
public ColliderObject[] getColliderObjects(Vector2 pos) { ArrayList<ColliderObject> a = new ArrayList<>(); for (ColliderObject object : colObjects) if (object.getPosition().equals(pos)) a.add(object); return a.toArray(new ColliderObject[0]); }
|
||||
public ColliderObject getColliderObject(Vector2 pos) { for (ColliderObject object : colObjects) if (object.getPosition().equals(pos)) return object; return null; }
|
||||
|
||||
public void addTexture(Texture object) { textures.add(object); }
|
||||
public void removeTexture(Texture object) { textures.remove(object); }
|
||||
public boolean containsTexture(Texture object) { return textures.contains(object); }
|
||||
public Texture[] getTextures() { return textures.toArray(new Texture[0]); }
|
||||
public Texture[] getTextures(Vector2 pos) { ArrayList<Texture> a = new ArrayList<>(); for (Texture object : textures) if (object.getPosition().equals(pos)) a.add(object); return a.toArray(new Texture[0]); }
|
||||
public Texture[] getTextures(float layer) { ArrayList<Texture> a = new ArrayList<>(); for (Texture object : textures) if (object.getLayer().equals(layer)) a.add(object); return a.toArray(new Texture[0]); }
|
||||
public Texture getTexture(Vector2 pos) { for (Texture object : textures) if (object.getPosition().equals(pos)) return object; return null; }
|
||||
public Texture getTextureByRect(Vector2 pos) { for (Texture object : textures) if (object.getRectangle().contains(pos.convert())) return object; return null; }
|
||||
|
||||
public UIManager getUIManager() { return uiManager; }
|
||||
|
||||
public void renderComponents(Graphics2D g) {
|
||||
for (float layer : GameEngine.getSortedLayers()) {
|
||||
for (Texture texture : getTextures(layer)) {
|
||||
if (Display.containsObject(texture.getScreenRect())) texture.render(g);
|
||||
}
|
||||
for (GameObject object : getGameObjects(layer)) {
|
||||
if (Display.containsObject(object.getScreenRect())) object.render(g);
|
||||
}
|
||||
}
|
||||
uiManager.render(g);
|
||||
}
|
||||
|
||||
protected void fixedUpdate() {
|
||||
for (GameObject object : objects) object.fixedUpdate();
|
||||
for (ColliderObject object : colObjects) object.fixedUpdate();
|
||||
}
|
||||
protected void update() {
|
||||
for (GameObject object : objects) {
|
||||
object.update();
|
||||
if (object.getCollider() != null) object.getCollider().calculateCollisions();
|
||||
}
|
||||
for (ColliderObject object : colObjects) {
|
||||
object.update();
|
||||
object.calculateCollisions();
|
||||
}
|
||||
}
|
||||
protected void start() {
|
||||
for (GameObject object : objects) object.start();
|
||||
for (ColliderObject object : colObjects) object.start();
|
||||
}
|
||||
protected void stop() {
|
||||
for (GameObject object : objects) object.stop();
|
||||
for (ColliderObject object : colObjects) object.stop();
|
||||
}
|
||||
|
||||
}
|
||||
31
Java/GameEngine_old/src/de/craftix/engine/SpriteMap.java
Normal file
31
Java/GameEngine_old/src/de/craftix/engine/SpriteMap.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package de.craftix.engine;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class SpriteMap {
|
||||
|
||||
private final int cols;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final BufferedImage sprite;
|
||||
|
||||
public SpriteMap(int cols, BufferedImage sprite, int width, int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.cols = cols;
|
||||
this.sprite = sprite;
|
||||
}
|
||||
|
||||
public BufferedImage getTexture(int id){
|
||||
int row = (id / cols);
|
||||
int col = (id % cols);
|
||||
return getTexture(col, row);
|
||||
}
|
||||
|
||||
public BufferedImage getTexture(int col, int row){
|
||||
return sprite.getSubimage(col * width, row * height, width, height);
|
||||
}
|
||||
|
||||
public int getCols() { return cols; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.SpriteMap;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Animation {
|
||||
|
||||
private int state;
|
||||
private int frame = 0;
|
||||
private int frames;
|
||||
private final long delay;
|
||||
private long startTime;
|
||||
private final SpriteMap sprite;
|
||||
private boolean started;
|
||||
|
||||
public Animation(SpriteMap sprite, int state, int frames, long delay){
|
||||
this.sprite = sprite;
|
||||
this.delay = delay;
|
||||
start(state, frames);
|
||||
}
|
||||
|
||||
public Animation(SpriteMap sprite, long delay) {
|
||||
this.sprite = sprite;
|
||||
this.delay = delay;
|
||||
started = false;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (started && System.currentTimeMillis() - startTime >= delay) {
|
||||
frame++;
|
||||
if (frame == frames) frame = 0;
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public void start(int state, int frames) {
|
||||
this.frames = frames;
|
||||
this.state = state;
|
||||
startTime = System.currentTimeMillis();
|
||||
frame = 0;
|
||||
started = true;
|
||||
}
|
||||
|
||||
public void stop() { started = false; }
|
||||
|
||||
public BufferedImage getImage() { return sprite.getTexture(frame, state); }
|
||||
public int getState() { return state; }
|
||||
public void setImages(int state, int frames) {
|
||||
this.state = state;
|
||||
this.frames = frames;
|
||||
frame = 0;
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public boolean isRunning() { return started; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.GameEngine;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Collider {
|
||||
|
||||
private final GameObject gameObject;
|
||||
private final boolean trigger;
|
||||
private final boolean considerLayers;
|
||||
private boolean colliding;
|
||||
|
||||
public Collider(GameObject gameObject, boolean trigger, boolean considerLayers) { this.gameObject = gameObject; this.trigger = trigger; this.colliding = false; this.considerLayers = considerLayers; }
|
||||
public Collider(GameObject gameObject, boolean trigger) { this.gameObject = gameObject; this.trigger = trigger; this.colliding = false; this.considerLayers = false; }
|
||||
|
||||
|
||||
public void calculateCollisions() {
|
||||
boolean isColliding = false;
|
||||
Rectangle self = gameObject.getScreenRect();
|
||||
for (GameObject object : GameEngine.getScene().getGameObjects()) {
|
||||
if (object.getCollider() == null) continue;
|
||||
if (object.equals(gameObject)) continue;
|
||||
if (object.getCollider().considerLayers() && object.getLayer() != gameObject.getLayer()) continue;
|
||||
Collider otherCol = object.getCollider();
|
||||
Rectangle other = object.getScreenRect();
|
||||
if (self.intersects(other)) {
|
||||
if (!otherCol.isTrigger()) isColliding = true;
|
||||
if (otherCol.isTrigger()) gameObject.onTrigger(otherCol);
|
||||
else gameObject.onCollision(otherCol);
|
||||
}
|
||||
}
|
||||
for (ColliderObject object : GameEngine.getScene().getColliderObjects()) {
|
||||
Rectangle other = object.getScreenRect();
|
||||
if (other.intersects(self)) {
|
||||
if (!object.isTrigger()) isColliding = true;
|
||||
if (object.isTrigger()) gameObject.onTrigger(object);
|
||||
else gameObject.onCollision(object);
|
||||
}
|
||||
}
|
||||
colliding = isColliding;
|
||||
}
|
||||
|
||||
public GameObject getGameObject() { return gameObject; }
|
||||
public boolean isTrigger() { return trigger; }
|
||||
public boolean isColliding() { return colliding; }
|
||||
public boolean considerLayers() { return considerLayers; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.Display;
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ColliderObject {
|
||||
|
||||
private Vector2 position;
|
||||
private Vector2 size;
|
||||
private final boolean trigger;
|
||||
private boolean colliding;
|
||||
|
||||
public ColliderObject(Vector2 position, Vector2 size, boolean isTrigger) { this.position = position; this.size = size; this.trigger = isTrigger; }
|
||||
|
||||
public void calculateCollisions() {
|
||||
boolean isColliding = false;
|
||||
Rectangle self = getRectangle();
|
||||
for (GameObject object : GameEngine.getScene().getGameObjects()) {
|
||||
if (object.getCollider() == null) continue;
|
||||
Collider otherCol = object.getCollider();
|
||||
Rectangle other = object.getRectangle();
|
||||
if (self.intersects(other)) {
|
||||
if (!otherCol.isTrigger()) isColliding = true;
|
||||
if (otherCol.isTrigger()) onTrigger(otherCol);
|
||||
else onCollision(otherCol);
|
||||
}
|
||||
}
|
||||
for (ColliderObject object : GameEngine.getScene().getColliderObjects()) {
|
||||
if (object.equals(this)) continue;
|
||||
Rectangle other = object.getRectangle();
|
||||
if (other.intersects(self)) {
|
||||
if (!object.isTrigger()) isColliding = true;
|
||||
if (object.isTrigger()) onTrigger(object);
|
||||
else onCollision(object);
|
||||
}
|
||||
}
|
||||
colliding = isColliding;
|
||||
}
|
||||
|
||||
public void setPosition(Vector2 position) { this.position = position; }
|
||||
public void setPosition(float x, float y) { this.position = new Vector2(x, y); }
|
||||
public void setSize(Vector2 size) { this.size = size; }
|
||||
public void setSize(float width, float height) { this.size = new Vector2(width, height); }
|
||||
|
||||
public Vector2 getPosition() { return position; }
|
||||
public Vector2 getSize() { return size; }
|
||||
public Rectangle getRectangle() { return new Rectangle(position.convert().x, position.convert().y, size.convert().x, size.convert().y); }
|
||||
public Rectangle getScreenRect() { return Display.calculateDisplayPosition(position, size); }
|
||||
public boolean isTrigger() { return trigger; }
|
||||
public boolean isColliding() { return colliding; }
|
||||
|
||||
public void fixedUpdate() {}
|
||||
public void update() {}
|
||||
public void start() {}
|
||||
public void stop() {}
|
||||
public void onCollision(Collider other) {}
|
||||
public void onTrigger(Collider other) {}
|
||||
public void onCollision(ColliderObject other) {}
|
||||
public void onTrigger(ColliderObject other) {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.Display;
|
||||
import de.craftix.engine.Resizer;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class GameObject {
|
||||
|
||||
private Vector2 position;
|
||||
private Vector2 size;
|
||||
private Collider collider;
|
||||
private Physics physics;
|
||||
private BufferedImage texture;
|
||||
private Animation animation;
|
||||
private Color color;
|
||||
private float layer;
|
||||
|
||||
public GameObject() { this(new Vector2(), new Vector2(), null, null, null, null, null, 0f); }
|
||||
public GameObject(Vector2 position, Vector2 size) { this(position, size, null, null, null, null, null, 0f); }
|
||||
public GameObject(Vector2 position, Vector2 size, float layer) { this(position, size, null, null, null, null, null, layer); }
|
||||
public GameObject(Vector2 position, Vector2 size, Color color) { this(position, size, null, null, null, null, color, 0f); }
|
||||
public GameObject(Vector2 position, Vector2 size, BufferedImage texture) { this(position, size, null, null, texture, null, null, 0f); }
|
||||
public GameObject(Vector2 position, Vector2 size, Animation animation) { this(position, size, null, null, null, animation, null, 0f); }
|
||||
public GameObject(Vector2 position, Vector2 size, boolean isCinematic) { this(position, size); this.physics = new Physics(this, isCinematic); }
|
||||
public GameObject(Vector2 position, Vector2 size, boolean isCinematic, boolean isTrigger, boolean considerLayer) { this(position, size, isCinematic); this.collider = new Collider(this, isTrigger, considerLayer); }
|
||||
|
||||
private GameObject(Vector2 position, Vector2 size, Collider collider, Physics physics, BufferedImage texture, Animation animation, Color color, float layer) {
|
||||
this.position = position;
|
||||
this.size = size;
|
||||
this.collider = collider;
|
||||
this.physics = physics;
|
||||
this.texture = texture;
|
||||
this.animation = animation;
|
||||
this.color = color;
|
||||
this.layer = layer;
|
||||
}
|
||||
|
||||
public void render(Graphics2D g) {
|
||||
if (animation != null) animation.update();
|
||||
Rectangle pos = Display.calculateDisplayPosition(position, size);
|
||||
if (animation != null) {
|
||||
if (animation.getImage().getWidth() == size.convert().x && animation.getImage().getHeight() == size.convert().y)
|
||||
g.drawImage(animation.getImage(), pos.x, pos.y, null);
|
||||
else g.drawImage(Resizer.AVERAGE.resize(animation.getImage(), size.convert().x, size.convert().y), pos.x, pos.y, null);
|
||||
}
|
||||
else if (texture != null) {
|
||||
if (texture.getWidth() == size.convert().x && texture.getHeight() == size.convert().y)
|
||||
g.drawImage(texture, pos.x, pos.y, null);
|
||||
else g.drawImage(Resizer.AVERAGE.resize(texture, size.convert().x, size.convert().y), pos.x, pos.y, null);
|
||||
}
|
||||
else if (color != null) { g.setColor(Color.WHITE); g.fill(pos);}
|
||||
else { g.setColor(Color.RED); g.draw(pos); }
|
||||
}
|
||||
|
||||
public GameObject copy() { return new GameObject(position, size, collider, physics, texture, animation, color, layer); }
|
||||
|
||||
public void setPosition(Vector2 position) { this.position = position; }
|
||||
public void setPosition(float x, float y) { this.position = new Vector2(x, y); }
|
||||
public void setSize(Vector2 size) { this.size = size; }
|
||||
public void setSize(float width, float height) { this.size = new Vector2(width, height); }
|
||||
public void setTexture(BufferedImage texture) { this.texture = texture; }
|
||||
public void setAnimation(Animation animation) { this.animation = animation; }
|
||||
public void setColor(Color color) { this.color = color; }
|
||||
public void setCollider(Collider collider) { this.collider = collider; }
|
||||
public void setPhysics(Physics physics) { this.physics = physics; }
|
||||
public void setLayer(float layer) { this.layer = layer; }
|
||||
|
||||
public Vector2 getPosition() { return position; }
|
||||
public Vector2 getSize() { return size; }
|
||||
public Rectangle getRectangle() { return new Rectangle(position.convert().x, position.convert().y, size.convert().x, size.convert().y); }
|
||||
public Rectangle getScreenRect() { return Display.calculateDisplayPosition(position, size); }
|
||||
public BufferedImage getTexture() { return texture; }
|
||||
public Animation getAnimation() { return animation; }
|
||||
public Color getColor() { return color; }
|
||||
public Collider getCollider() { return collider; }
|
||||
public Physics getPhysics() { return physics; }
|
||||
public float getLayer() { return layer; }
|
||||
|
||||
public void fixedUpdate() {}
|
||||
public void update() {}
|
||||
public void start() {}
|
||||
public void stop() {}
|
||||
public void onCollision(Collider other) {}
|
||||
public void onTrigger(Collider other) {}
|
||||
public void onCollision(ColliderObject other) {}
|
||||
public void onTrigger(ColliderObject other) {}
|
||||
|
||||
}
|
||||
113
Java/GameEngine_old/src/de/craftix/engine/objects/Physics.java
Normal file
113
Java/GameEngine_old/src/de/craftix/engine/objects/Physics.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.Display;
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Physics {
|
||||
private static final float defaultGravity = 1.5f;
|
||||
private static final float maxForce = 5;
|
||||
|
||||
private final GameObject gameObject;
|
||||
private final boolean cinematic;
|
||||
private final float gravity;
|
||||
private boolean onGround;
|
||||
|
||||
private final Vector2 force = new Vector2();
|
||||
|
||||
public Physics(GameObject gameObject, float gravity, boolean cinematic) { this.gameObject = gameObject; this.gravity = gravity; this.cinematic = cinematic; }
|
||||
public Physics(GameObject gameObject, boolean cinematic) { this(gameObject, defaultGravity, cinematic); }
|
||||
public Physics(GameObject gameObject, float gravity) { this(gameObject, gravity, false); }
|
||||
|
||||
public void calculatePhysics() {
|
||||
if (cinematic) return;
|
||||
float dist = calculateDistanceToObjectBelow();
|
||||
Vector2 result = new Vector2();
|
||||
if (dist > 0) force.add(0, -gravity);
|
||||
|
||||
if (force.y > maxForce || force.y < -maxForce) {
|
||||
if (force.y < 0) {
|
||||
result.y -= maxForce;
|
||||
force.y += maxForce;
|
||||
}else {
|
||||
result.y += maxForce;
|
||||
force.y -= maxForce;
|
||||
}
|
||||
}else {
|
||||
result.y += force.y;
|
||||
force.y = 0;
|
||||
}
|
||||
|
||||
if (force.x > maxForce || force.x < -maxForce) {
|
||||
if (force.x < 0) {
|
||||
result.x -= maxForce;
|
||||
force.x += maxForce;
|
||||
}else {
|
||||
result.x += maxForce;
|
||||
force.x -= maxForce;
|
||||
}
|
||||
}else {
|
||||
result.x += force.x;
|
||||
force.x = 0;
|
||||
}
|
||||
if (precalculateCollision(result.copy(), gameObject.getSize())) result = new Vector2(0, 0);
|
||||
|
||||
onGround = (dist <= 0);
|
||||
|
||||
gameObject.getPosition().add(result);
|
||||
}
|
||||
|
||||
private float calculateDistanceToObjectBelow() {
|
||||
float dist = Integer.MAX_VALUE;
|
||||
float sl = gameObject.getPosition().x - (gameObject.getSize().x / 2);
|
||||
float sr = gameObject.getPosition().x + (gameObject.getSize().x / 2);
|
||||
for (GameObject object : GameEngine.getScene().getGameObjects()) {
|
||||
if (object.equals(gameObject) || object.getPosition().y > gameObject.getPosition().y) continue;
|
||||
if (object.getCollider() == null || object.getCollider().isTrigger()) continue;
|
||||
float ol = object.getPosition().x - (object.getSize().x / 2);
|
||||
float or = object.getPosition().x + (object.getSize().x / 2);
|
||||
if (or < sl || ol > sr) continue;
|
||||
float d = (gameObject.getPosition().y - (gameObject.getSize().y / 2)) - (object.getPosition().y + (object.getSize().y / 2));
|
||||
if (d < dist) dist = d;
|
||||
}
|
||||
for (ColliderObject object : GameEngine.getScene().getColliderObjects()) {
|
||||
if (object.getPosition().y >= gameObject.getPosition().y) continue;
|
||||
if (object.isTrigger()) continue;
|
||||
float ol = object.getPosition().x - (object.getSize().x / 2);
|
||||
float or = object.getPosition().x + (object.getSize().x / 2);
|
||||
if (or < sl || ol > sr) continue;
|
||||
float d = (gameObject.getPosition().y - (gameObject.getSize().y / 2)) - (object.getPosition().y + (object.getSize().y / 2));
|
||||
if (d < dist) dist = d;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
|
||||
private boolean precalculateCollision(Vector2 pos, Vector2 size) {
|
||||
pos.add(gameObject.getPosition());
|
||||
Rectangle self = Display.calculateDisplayPosition(pos, size);
|
||||
for (GameObject object : GameEngine.getScene().getGameObjects()) {
|
||||
if (object.getCollider() == null) continue;
|
||||
if (object.equals(gameObject)) continue;
|
||||
if (object.getCollider().isTrigger()) continue;
|
||||
Rectangle other = object.getScreenRect();
|
||||
if (self.intersects(other)) return true;
|
||||
}
|
||||
for (ColliderObject object : GameEngine.getScene().getColliderObjects()) {
|
||||
if (object.isTrigger()) continue;
|
||||
Rectangle other = object.getScreenRect();
|
||||
if (self.intersects(other)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public GameObject getGameObject() { return gameObject; }
|
||||
public boolean isCinematic() { return cinematic; }
|
||||
public float getGravity() { return gravity; }
|
||||
public boolean isOnGround() { return onGround; }
|
||||
|
||||
public void addForce(float x, float y) { force.add(x, y); }
|
||||
public void addForce(Vector2 force) { this.force.add(force); }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package de.craftix.engine.objects;
|
||||
|
||||
import de.craftix.engine.Display;
|
||||
import de.craftix.engine.Resizer;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Texture {
|
||||
|
||||
private final BufferedImage texture;
|
||||
private final Color color;
|
||||
private final Vector2 position;
|
||||
private final Vector2 size;
|
||||
private float layer;
|
||||
|
||||
public Texture(BufferedImage texture) { this(texture, new Vector2(), new Vector2(texture.getWidth(), texture.getHeight())); }
|
||||
public Texture(BufferedImage texture, Vector2 position, Vector2 size, float layer) { this.texture = texture; this.color = null; this.position = position; this.size = size; this.layer = layer; }
|
||||
public Texture(BufferedImage texture, Vector2 position, Vector2 size) { this(texture, position, size, 0); }
|
||||
public Texture(BufferedImage texture, Vector2 position) { this(texture, position, new Vector2(texture.getWidth(), texture.getHeight())); }
|
||||
public Texture(Color color, Vector2 position, Vector2 size, float layer) { this.color = color; this.texture = null; this.position = position; this.size = size; this.layer = layer; }
|
||||
public Texture(Color color, Vector2 position, Vector2 size) { this(color, position, size, 0); }
|
||||
|
||||
private Texture(BufferedImage texture, Color color, Vector2 position, Vector2 size, float layer) {
|
||||
this.texture =texture;
|
||||
this.color = color;
|
||||
this.position = position;
|
||||
this.size = size;
|
||||
this.layer = layer;
|
||||
}
|
||||
|
||||
public void render(Graphics2D g) {
|
||||
if (color != null) g.setColor(color);
|
||||
Rectangle dimensions = Display.calculateDisplayPosition(position, size);
|
||||
if (texture != null) {
|
||||
if (texture.getWidth() == size.convert().x && texture.getHeight() == size.convert().y)
|
||||
g.drawImage(texture, dimensions.x, dimensions.y, null);
|
||||
else g.drawImage(Resizer.AVERAGE.resize(texture, size.convert().x, size.convert().y), dimensions.x, dimensions.y, null);
|
||||
}
|
||||
else g.fill(dimensions);
|
||||
}
|
||||
|
||||
public Texture copy() { return new Texture(texture, color, position, size, layer); }
|
||||
|
||||
public void setPosition(int x, int y) { this.position.x = x; this.position.y = y; }
|
||||
public void setPosition(Vector2 v) { this.position.x = v.x; this.position.y = v.y; }
|
||||
public void setSize(int width, int height) { this.size.x = width; this.size.y = height; }
|
||||
public void setSize(Vector2 v) { this.size.x = v.x; this.size.y = v.y; }
|
||||
public void setLayer(float layer) { this.layer = layer; }
|
||||
|
||||
public Rectangle getRectangle() { return new Rectangle(position.convert().x, position.convert().y, size.convert().x, size.convert().y); }
|
||||
public Rectangle getScreenRect() { return Display.calculateDisplayPosition(position, size); }
|
||||
public BufferedImage getTexture() { return texture; }
|
||||
public Color getColor() { return color; }
|
||||
public Vector2 getPosition() { return position; }
|
||||
public Vector2 getSize() { return size; }
|
||||
public Float getLayer() { return layer; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.craftix.engine.objects.particles;
|
||||
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Particle {
|
||||
private final Vector2 velocity = new Vector2();
|
||||
private final Vector2 position;
|
||||
private final Vector2 size;
|
||||
private Color color;
|
||||
private BufferedImage texture;
|
||||
|
||||
public Particle(Vector2 position, Vector2 size, Color color, BufferedImage texture) {
|
||||
this.position = position;
|
||||
this.size = size;
|
||||
this.color = color;
|
||||
this.texture = texture;
|
||||
}
|
||||
|
||||
public Vector2 getVelocity() { return velocity; }
|
||||
public Vector2 getPosition() { return position; }
|
||||
public Vector2 getSize() { return size; }
|
||||
public Color getColor() { return color; }
|
||||
public BufferedImage getTexture() { return texture; }
|
||||
public void setVelocity(float x, float y) { velocity.x = x; velocity.y = y; }
|
||||
public void setPosition(float x, float y) { position.x = x; position.y = y; }
|
||||
public void setSize(float width, float height) { size.x = width; size.y = height; }
|
||||
public void setColor(Color c) { this.color = c; }
|
||||
public void setTexture(BufferedImage img) { this.texture = img; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package de.craftix.engine.objects.particles;
|
||||
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
public class ParticleSystem {
|
||||
private final Particle[] particles;
|
||||
private final int maxAmount;
|
||||
private final Vector2 spawn;
|
||||
private final Vector2 globalVelocity;
|
||||
|
||||
public ParticleSystem(Vector2 spawn, int maxAmount) {
|
||||
this.spawn = spawn;
|
||||
this.maxAmount = maxAmount;
|
||||
globalVelocity = new Vector2();
|
||||
particles = new Particle[maxAmount];
|
||||
}
|
||||
|
||||
public Particle[] getParticles() { return particles; }
|
||||
public int getMaxAmount() { return maxAmount; }
|
||||
public Vector2 getSpawn() { return spawn; }
|
||||
public Vector2 getGlobalVelocity() { return globalVelocity; }
|
||||
|
||||
public void setSpawn(float x, float y) { spawn.x = x; spawn.y = y; }
|
||||
public void setGlobalVelocity(float x, float y) { globalVelocity.x = x; globalVelocity.y = y; }
|
||||
|
||||
}
|
||||
55
Java/GameEngine_old/src/de/craftix/engine/ui/Dimensions.java
Normal file
55
Java/GameEngine_old/src/de/craftix/engine/ui/Dimensions.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package de.craftix.engine.ui;
|
||||
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Dimensions implements Serializable {
|
||||
public static Dimensions parse(String str) {
|
||||
String[] parts = str.replace("[", "").replace("]", "").split(" - ");
|
||||
String[] pos = parts[0].split(":");
|
||||
String[] size = parts[1].split(":");
|
||||
return new Dimensions(Integer.parseInt(pos[0]), Integer.parseInt(pos[1]), Integer.parseInt(size[0]), Integer.parseInt(size[1]));
|
||||
}
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public Dimensions(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }
|
||||
public Dimensions(Point pos, Point size) { this(pos.x, pos.y, size.x, size.y); }
|
||||
public Dimensions(Rectangle rect) { this(rect.x, rect.y, rect.width, rect.height); }
|
||||
|
||||
public boolean contains(Vector2 other) { return convert().contains(other.convert()); }
|
||||
public boolean intersects(Dimensions other) { return convert().intersects(other.convert()); }
|
||||
|
||||
public Rectangle convert() { return new Rectangle(x, y, width, height); }
|
||||
public Dimensions copy() { return new Dimensions(x, y, width, height); }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Dimensions that = (Dimensions) o;
|
||||
return x == that.x && y == that.y && width == that.width && height == that.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() { return Objects.hash(x, y, width, height); }
|
||||
|
||||
public void setX(int x) { this.x = x; }
|
||||
public void setY(int y) { this.y = y; }
|
||||
public void setWidth(int width) { this.width = width; }
|
||||
public void setHeight(int height) { this.height = height; }
|
||||
|
||||
public int getX() { return x; }
|
||||
public int getY() { return y; }
|
||||
public int getWidth() { return width; }
|
||||
public int getHeight() { return height; }
|
||||
|
||||
@Override
|
||||
public String toString() { return "[" + x + ":" + y + " - " + width + ":" + height + "]"; }
|
||||
}
|
||||
76
Java/GameEngine_old/src/de/craftix/engine/ui/UIButton.java
Normal file
76
Java/GameEngine_old/src/de/craftix/engine/ui/UIButton.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package de.craftix.engine.ui;
|
||||
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.Resizer;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class UIButton extends UIComponent {
|
||||
private static final int offset = 2;
|
||||
private static final Color NBG = Color.DARK_GRAY;
|
||||
private static final Color HBG = Color.LIGHT_GRAY;
|
||||
private static final Color PBG = Color.WHITE;
|
||||
private static final Color FG = Color.GRAY;
|
||||
|
||||
private boolean hover = false;
|
||||
private boolean pressed = false;
|
||||
private boolean renderPressed = false;
|
||||
private final ActionListener al;
|
||||
private final UIText text;
|
||||
|
||||
private BufferedImage NIBG;
|
||||
private BufferedImage HIBG;
|
||||
private BufferedImage PBTX;
|
||||
|
||||
public UIButton(Dimensions dimensions, UIText text, ActionListener al) { this.dimensions = dimensions; this.al = al; this.text = text; }
|
||||
public UIButton(Dimensions dimensions, ActionListener al) { this(dimensions, null, al); }
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
Dimensions screen = new Dimensions(calculatePosition().x, calculatePosition().y, dimensions.getWidth(), dimensions.getHeight());
|
||||
hover = screen.contains(InputManager.getMouseRaw());
|
||||
if (InputManager.isMouseClicked(MouseEvent.BUTTON1) && hover && !pressed) {
|
||||
pressed = true;
|
||||
if (al != null) al.actionPerformed(new ActionEvent(this, 0, "Button Clicked"));
|
||||
renderPressed = true;
|
||||
new Thread(() -> { try { Thread.sleep(200); } catch (Exception ignored) {} renderPressed = false; }).start();
|
||||
}
|
||||
if (!InputManager.isMouseClicked(MouseEvent.BUTTON1)) pressed = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Graphics2D g) {
|
||||
Point pos = calculatePosition();
|
||||
if (HIBG == null || NIBG == null || PBTX == null) {
|
||||
if (renderPressed) g.setColor(PBG);
|
||||
else if (hover) g.setColor(HBG);
|
||||
else g.setColor(NBG);
|
||||
g.fill(new Rectangle(pos.x, pos.y, dimensions.getWidth(), dimensions.getHeight()));
|
||||
pos.x += offset;
|
||||
pos.y += offset;
|
||||
g.setColor(FG);
|
||||
g.fill(new Rectangle(pos.x, pos.y, dimensions.getWidth() - offset * 2, dimensions.getHeight() - offset * 2));
|
||||
}else {
|
||||
if (NIBG.getWidth() != dimensions.getWidth() || NIBG.getHeight() != dimensions.getHeight())
|
||||
NIBG = Resizer.AVERAGE.resize(NIBG, dimensions.getWidth(), dimensions.getHeight());
|
||||
if (HIBG.getWidth() != dimensions.getWidth() || HIBG.getHeight() != dimensions.getHeight())
|
||||
HIBG = Resizer.AVERAGE.resize(HIBG, dimensions.getWidth(), dimensions.getHeight());
|
||||
if (PBTX.getWidth() != dimensions.getWidth() || PBTX.getHeight() != dimensions.getHeight())
|
||||
PBTX = Resizer.AVERAGE.resize(PBTX, dimensions.getWidth(), dimensions.getHeight());
|
||||
if (renderPressed) g.drawImage(PBTX, pos.x, pos.y, null);
|
||||
else if (!hover) g.drawImage(NIBG, pos.x, pos.y, null);
|
||||
else g.drawImage(HIBG, pos.x, pos.y, null);
|
||||
}
|
||||
if (text != null) {
|
||||
text.setDimensions(new Dimensions(dimensions.getX(), dimensions.getY(), 0, 0));
|
||||
text.render(g);
|
||||
}
|
||||
}
|
||||
|
||||
public UIText getText() { return text; }
|
||||
public void setTexture(BufferedImage normal, BufferedImage hover, BufferedImage pressed) { this.NIBG = normal; this.HIBG = hover; this.PBTX = pressed; }
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package de.craftix.engine.ui;
|
||||
|
||||
import de.craftix.engine.Display;
|
||||
import de.craftix.engine.Resizer;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class UIComponent {
|
||||
|
||||
protected Dimensions dimensions;
|
||||
protected BufferedImage texture;
|
||||
protected Integer layer;
|
||||
|
||||
public UIComponent() { this(new Dimensions(0, 0, 50, 50), null, 0); }
|
||||
public UIComponent(Dimensions dimensions, BufferedImage texture, int layer) { this.dimensions = dimensions; this.texture = texture; this.layer = layer; }
|
||||
public UIComponent(int x, int y, int width, int height, BufferedImage texture) { this(new Dimensions(x, y, width, height), texture, 0); }
|
||||
public UIComponent(int x, int y, BufferedImage texture) { this(x, y, texture.getWidth(), texture.getHeight(), texture); }
|
||||
|
||||
public void render(Graphics2D g) {
|
||||
g.setColor(Color.RED);
|
||||
if (texture != null) {
|
||||
if (texture.getHeight() != dimensions.getHeight() || texture.getWidth() != dimensions.getWidth())
|
||||
texture = Resizer.AVERAGE.resize(texture, dimensions.getWidth(), dimensions.getHeight());
|
||||
Point pos = calculatePosition();
|
||||
g.drawImage(texture, pos.x, pos.y, null);
|
||||
}else g.draw(dimensions.convert());
|
||||
}
|
||||
|
||||
protected Point calculatePosition() {
|
||||
Point mid = new Vector2(Display.getInstance().getWidth(), Display.getInstance().getHeight()).divide(2, 2).convert();
|
||||
Point p = new Point(dimensions.getX() + (dimensions.getWidth() / 2), dimensions.getY() + (dimensions.getHeight() / 2));
|
||||
p.x -= mid.x;
|
||||
p.y -= mid.y;
|
||||
p.x *= -1;
|
||||
p.y *= -1;
|
||||
return p;
|
||||
}
|
||||
|
||||
public void setDimensions(Dimensions dimensions) { this.dimensions = dimensions; }
|
||||
public void setTexture(BufferedImage texture) { this.texture = texture; }
|
||||
public void setLayer(int layer) { this.layer = layer; }
|
||||
|
||||
public Dimensions getDimensions() { return dimensions; }
|
||||
public BufferedImage getTexture() { return texture; }
|
||||
public Integer getLayer() { return layer; }
|
||||
|
||||
public void update() {}
|
||||
|
||||
}
|
||||
36
Java/GameEngine_old/src/de/craftix/engine/ui/UIManager.java
Normal file
36
Java/GameEngine_old/src/de/craftix/engine/ui/UIManager.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package de.craftix.engine.ui;
|
||||
|
||||
import de.craftix.engine.var.Inputs;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class UIManager extends Inputs {
|
||||
|
||||
private final ArrayList<Integer> layers = new ArrayList<>();
|
||||
private final ArrayList<UIComponent> components = new ArrayList<>();
|
||||
|
||||
public UIManager() { layers.add(-10); layers.add(0); layers.add(10); }
|
||||
|
||||
public void render(Graphics2D g) {
|
||||
List<Integer> sortLayer = new ArrayList<>(layers);
|
||||
Collections.sort(sortLayer);
|
||||
for (Integer layer : sortLayer) {
|
||||
for (UIComponent component : components) {
|
||||
if (!component.getLayer().equals(layer)) continue;
|
||||
component.update();
|
||||
component.render(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addComponent(UIComponent component) { components.add(component); }
|
||||
public void removeComponent(UIComponent component) { components.remove(component); }
|
||||
public void addLayer(int layer) { layers.add(layer); }
|
||||
public void removeLayer(int layer) { if (layer != 0) layers.remove((Object)layer); }
|
||||
public UIComponent[] getComponents() { return components.toArray(new UIComponent[0]); }
|
||||
public Integer[] getLayers() { return layers.toArray(new Integer[0]); }
|
||||
|
||||
}
|
||||
46
Java/GameEngine_old/src/de/craftix/engine/ui/UIText.java
Normal file
46
Java/GameEngine_old/src/de/craftix/engine/ui/UIText.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package de.craftix.engine.ui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.font.GlyphVector;
|
||||
|
||||
public class UIText extends UIComponent {
|
||||
|
||||
private String text;
|
||||
private Font font;
|
||||
private Color TC;
|
||||
|
||||
public UIText() { this(0, 0, null, null, null); }
|
||||
|
||||
public UIText(int x, int y, String text, Font font, Color color) { this.dimensions = new Dimensions(new Point(x, y), new Point()); this.text = text; this.font = font; this.TC = color; }
|
||||
public UIText(int x, int y, String text, Color color) { this(x, y, text, null, color); }
|
||||
public UIText(int x, int y, String text, Font font) { this(x, y, text, font, Color.BLACK); }
|
||||
public UIText(int x, int y, String text) { this(x, y, text, null, Color.BLACK); }
|
||||
|
||||
public UIText(String text, Font font, Color color) { this(0, 0, text, font, color); }
|
||||
public UIText(String text, Color color) { this(text, null, color); }
|
||||
public UIText(String text, Font font) { this(text, font, Color.BLACK); }
|
||||
public UIText(String text) { this(text, null, Color.BLACK); }
|
||||
|
||||
@Override
|
||||
public void render(Graphics2D g) {
|
||||
if (text != null) {
|
||||
g.setColor(TC);
|
||||
if (font != null) g.setFont(font);
|
||||
FontRenderContext render = g.getFontRenderContext();
|
||||
GlyphVector vector = g.getFont().createGlyphVector(render, text);
|
||||
Rectangle bounds = vector.getPixelBounds(null, 0, 0);
|
||||
int x = calculatePosition().x - (bounds.width / 2);
|
||||
int y = calculatePosition().y + (bounds.height / 2);
|
||||
g.drawString(text, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
public void setText(String text) { this.text = text; }
|
||||
public void setFont(Font font) { this.font = font; }
|
||||
public void setColor(Color color) { this.TC = color; }
|
||||
public String getText() { return text; }
|
||||
public Font getFont() { return font; }
|
||||
public Color getColor() { return TC; }
|
||||
|
||||
}
|
||||
28
Java/GameEngine_old/src/de/craftix/engine/var/Inputs.java
Normal file
28
Java/GameEngine_old/src/de/craftix/engine/var/Inputs.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package de.craftix.engine.var;
|
||||
|
||||
import java.awt.event.*;
|
||||
|
||||
public class Inputs implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) { }
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) { }
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) { }
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) { }
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) { }
|
||||
@Override
|
||||
public void mouseWheelMoved(MouseWheelEvent e) { }
|
||||
}
|
||||
98
Java/GameEngine_old/src/de/craftix/engine/var/Mathf.java
Normal file
98
Java/GameEngine_old/src/de/craftix/engine/var/Mathf.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package de.craftix.engine.var;
|
||||
|
||||
public final class Mathf {
|
||||
private Mathf() {}
|
||||
|
||||
public static float map(float min, float max, float minValue, float maxValue, float value) {
|
||||
float normalized = normalize(minValue, maxValue, value);
|
||||
return (normalized * (max - min)) + min;
|
||||
}
|
||||
|
||||
public static float normalize(float min, float max, float value) {
|
||||
return (value - min) / (max - min);
|
||||
}
|
||||
|
||||
//Normal java.lang.Math methods
|
||||
public static final double E = 2.7182818284590452354;
|
||||
public static final double PI = 3.14159265358979323846;
|
||||
|
||||
public static double sin(double a) { return Math.sin(a); }
|
||||
public static double cos(double a) { return Math.cos(a); }
|
||||
public static double tan(double a) { return Math.tan(a); }
|
||||
public static double asin(double a) { return Math.asin(a); }
|
||||
public static double acos(double a) { return Math.acos(a); }
|
||||
public static double atan(double a) { return Math.atan(a); }
|
||||
public static double toRadians(double angdeg) { return Math.toRadians(angdeg); }
|
||||
public static double toDegrees(double angrad) { return Math.toDegrees(angrad); }
|
||||
public static double exp(double a) { return Math.exp(a); }
|
||||
public static double log(double a) { return Math.log(a); }
|
||||
public static double log10(double a) { return Math.log10(a); }
|
||||
public static double sqrt(double a) { return Math.sqrt(a); }
|
||||
public static double cbrt(double a) { return Math.cbrt(a); }
|
||||
public static double IEEEremainder(double f1, double f2) { return Math.IEEEremainder(f1, f2); }
|
||||
public static double ceil(double a) { return Math.ceil(a); }
|
||||
public static double floor(double a) { return Math.floor(a); }
|
||||
public static double rint(double a) { return Math.rint(a); }
|
||||
public static double atan2(double y, double x) { return Math.atan2(y, x); }
|
||||
public static double pow(double a, double b) { return Math.pow(a, b); }
|
||||
public static int round(float a) { return Math.round(a); }
|
||||
public static long round(double a) { return Math.round(a); }
|
||||
public static double random() { return Math.random(); }
|
||||
public static int addExact(int x, int y) { return Math.addExact(x, y); }
|
||||
public static long addExact(long x, long y) { return Math.addExact(x, y); }
|
||||
public static int subtractExact(int x, int y) { return Math.subtractExact(x, y); }
|
||||
public static long subtractExact(long x, long y) { return Math.subtractExact(x, y); }
|
||||
public static int multiplyExact(int x, int y) { return Math.multiplyExact(x, y); }
|
||||
public static long multiplyExact(long x, int y) { return Math.multiplyExact(x, y); }
|
||||
public static long multiplyExact(long x, long y) { return Math.multiplyExact(x, y); }
|
||||
public static int incrementExact(int a) { return Math.incrementExact(a); }
|
||||
public static long incrementExact(long a) { return Math.incrementExact(a); }
|
||||
public static int decrementExact(int a) { return Math.decrementExact(a); }
|
||||
public static long decrementExact(long a) { return Math.decrementExact(a); }
|
||||
public static int negateExact(int a) { return Math.negateExact(a); }
|
||||
public static long negateExact(long a) { return Math.negateExact(a); }
|
||||
public static int toIntExact(long value) { return Math.toIntExact(value); }
|
||||
public static long multiplyHigh(long x, long y) { return Math.multiplyHigh(x, y); }
|
||||
public static int floorDiv(int x, int y) { return Math.floorDiv(x, y); }
|
||||
public static long floorDiv(long x, int y) { return Math.floorDiv(x, y); }
|
||||
public static long floorDiv(long x, long y) { return Math.floorDiv(x, y); }
|
||||
public static int floorMod(int x, int y) { return Math.floorMod(x, y); }
|
||||
public static int floorMod(long x, int y) { return Math.floorMod(x, y); }
|
||||
public static long floorMod(long x, long y) { return Math.floorMod(x, y); }
|
||||
public static int abs(int a) { return Math.abs(a); }
|
||||
public static long abs(long a) { return Math.abs(a); }
|
||||
public static float abs(float a) { return Math.abs(a); }
|
||||
public static double abs(double a) { return Math.abs(a); }
|
||||
public static int max(int a, int b) { return Math.max(a, b); }
|
||||
public static long max(long a, long b) { return Math.max(a, b); }
|
||||
public static float max(float a, float b) { return Math.max(a, b); }
|
||||
public static double max(double a, double b) { return Math.max(a, b); }
|
||||
public static int min(int a, int b) { return Math.min(a, b); }
|
||||
public static long min(long a, long b) { return Math.min(a, b); }
|
||||
public static float min(float a, float b) { return Math.min(a, b); }
|
||||
public static double min(double a, double b) { return Math.min(a, b); }
|
||||
public static double fma(double a, double b, double c) { return Math.fma(a, b, c); }
|
||||
public static float fma(float a, float b, float c) { return Math.fma(a, b, c); }
|
||||
public static double ulp(double d) { return Math.ulp(d); }
|
||||
public static float ulp(float f) { return Math.ulp(f); }
|
||||
public static double signum(double d) { return Math.signum(d); }
|
||||
public static float signum(float f) { return Math.signum(f); }
|
||||
public static double sinh(double x) { return Math.sinh(x); }
|
||||
public static double cosh(double x) { return Math.cosh(x); }
|
||||
public static double tanh(double x) { return Math.tanh(x); }
|
||||
public static double hypot(double x, double y) { return Math.hypot(x, y); }
|
||||
public static double expm1(double x) { return Math.expm1(x); }
|
||||
public static double log1p(double x) { return Math.log1p(x); }
|
||||
public static double copySign(double magnitude, double sign) { return Math.copySign(magnitude, sign); }
|
||||
public static float copySign(float magnitude, float sign) { return Math.copySign(magnitude, sign); }
|
||||
public static int getExponent(float f) { return Math.getExponent(f); }
|
||||
public static int getExponent(double d) { return Math.getExponent(d); }
|
||||
public static double nextAfter(double start, double direction) { return Math.nextAfter(start, direction); }
|
||||
public static float nextAfter(float start, double direction) { return Math.nextAfter(start, direction); }
|
||||
public static double nextUp(double d) { return Math.nextUp(d); }
|
||||
public static float nextUp(float f) { return Math.nextUp(f); }
|
||||
public static double nextDown(double d) { return Math.nextDown(d); }
|
||||
public static float nextDown(float f) { return Math.nextDown(f); }
|
||||
public static double scalb(double d, int scaleFactor) { return Math.scalb(d, scaleFactor); }
|
||||
public static float scalb(float f, int scaleFactor) { return Math.scalb(f, scaleFactor); }
|
||||
}
|
||||
59
Java/GameEngine_old/src/de/craftix/engine/var/MySQL.java
Normal file
59
Java/GameEngine_old/src/de/craftix/engine/var/MySQL.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package de.craftix.engine.var;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class MySQL {
|
||||
|
||||
protected String server;
|
||||
protected int port;
|
||||
protected String database;
|
||||
protected String username;
|
||||
protected String password;
|
||||
protected Connection con;
|
||||
|
||||
public MySQL(String server, int port, String database, String username, String password) {
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
this.database = database;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public MySQL(String server, String database, String username, String password) { this(server, 3306, database, username, password); }
|
||||
|
||||
public void connect() {
|
||||
if (isConnected()) return;
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, username, password);
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (!isConnected()) return;
|
||||
try {
|
||||
con.close();
|
||||
con = null;
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public boolean isConnected() { return con != null; }
|
||||
|
||||
public void insert(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
con.prepareStatement(qry).executeUpdate();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public ResultSet getData(String qry) {
|
||||
if (!isConnected()) throw new NullPointerException("MySQL not connected");
|
||||
try {
|
||||
return con.prepareStatement(qry).executeQuery();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
41
Java/GameEngine_old/src/de/craftix/engine/var/Sound.java
Normal file
41
Java/GameEngine_old/src/de/craftix/engine/var/Sound.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*package de.craftix.engine.var;
|
||||
|
||||
import javazoom.jl.player.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Sound {
|
||||
|
||||
protected File file;
|
||||
protected Player player;
|
||||
protected Thread thread;
|
||||
|
||||
public Sound(File file) { this.file = file; }
|
||||
|
||||
public void play() {
|
||||
if (isPlaying()) return;
|
||||
thread = new Thread(() -> {
|
||||
try {
|
||||
InputStream stream = new FileInputStream(file);
|
||||
assert stream != null;
|
||||
player = new Player(stream);
|
||||
player.play();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (!isPlaying()) return;
|
||||
try {
|
||||
player.close();
|
||||
player = null;
|
||||
thread.interrupt();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public boolean isPlaying() { return player != null; }
|
||||
|
||||
}*/
|
||||
38
Java/GameEngine_old/src/de/craftix/engine/var/Vector2.java
Normal file
38
Java/GameEngine_old/src/de/craftix/engine/var/Vector2.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package de.craftix.engine.var;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Vector2 implements Serializable {
|
||||
|
||||
public static Vector2 parseVector2(String vector) {
|
||||
vector = vector.replace("[", "");
|
||||
vector = vector.replace("]", "");
|
||||
String[] vars = vector.split(":");
|
||||
return new Vector2(Float.parseFloat(vars[0]), Float.parseFloat(vars[2]));
|
||||
}
|
||||
|
||||
public float x;
|
||||
public float y;
|
||||
|
||||
public Vector2(float x, float y) { this.x = x; this.y = y; }
|
||||
public Vector2(Vector2 v) { this(v.x, v.y); }
|
||||
public Vector2(Point p) { this(p.x, p.y); }
|
||||
public Vector2() { this(0, 0); }
|
||||
|
||||
public Vector2 copy() { return new Vector2(x, y); }
|
||||
public boolean equals(Object v) { if (!(v instanceof Vector2)) return false; Vector2 v2 = (Vector2) v; return (v2.x == x && v2.y == y); }
|
||||
public String toString() { return "[" + x + ":" + y + "]"; }
|
||||
|
||||
public Vector2 add(Vector2 v) { this.x += v.x; this.y += v.y; return this; }
|
||||
public Vector2 add(float x, float y) { this.x += x; this.y += y; return this; }
|
||||
public Vector2 subtract(Vector2 v) { this.x -= v.x; this.y -= v.y; return this; }
|
||||
public Vector2 subtract(float x, float y) { this.x -= x; this.y -= y; return this; }
|
||||
public Vector2 multiply(Vector2 v) { this.x *= v.x; this.y *= v.y; return this; }
|
||||
public Vector2 multiply(float x, float y) { this.x *= x; this.y *= y; return this; }
|
||||
public Vector2 divide(Vector2 v) { this.x /= v.x; this.y /= v.y; return this; }
|
||||
public Vector2 divide(float x, float y) { this.x /= x; this.y /= y; return this; }
|
||||
|
||||
public Point convert() { return new Point(Mathf.round(x), Mathf.round(y)); }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package de.craftix.engine.var;
|
||||
|
||||
import de.craftix.engine.ui.Dimensions;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class YamlConfiguration {
|
||||
|
||||
public static YamlConfiguration createConfiguration(String path, String name) {
|
||||
File file = new File(path + "/" + name + ".yml");
|
||||
if (file.exists()) return new YamlConfiguration(file.getPath());
|
||||
try {
|
||||
new File(path).mkdirs();
|
||||
if (!file.exists()) file.createNewFile();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
return new YamlConfiguration(file.getPath());
|
||||
}
|
||||
|
||||
private final HashMap<String, Object> variables = new HashMap<>();
|
||||
private final File file;
|
||||
|
||||
public YamlConfiguration(String path) {
|
||||
file = new File(path);
|
||||
if (!file.exists()) throw new NullPointerException("File does not exists");
|
||||
loadVariables();
|
||||
}
|
||||
|
||||
private void loadVariables() {
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
try (Stream<String> stream = Files.lines(file.toPath(), StandardCharsets.UTF_8)) {
|
||||
stream.forEach(lines::add);
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
for (String line : lines) {
|
||||
String[] separator = line.split(": ");
|
||||
variables.put(separator[0], separator[1]);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveVariables() {
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
for (String path : variables.keySet()) lines.add(path + ": " + variables.get(path).toString());
|
||||
Collections.sort(lines);
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
out.append(lines.get(i));
|
||||
if (i != lines.size() - 1) out.append("\n");
|
||||
}
|
||||
try {
|
||||
if (file.delete()) file.createNewFile();
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
||||
writer.write(out.toString());
|
||||
writer.close();
|
||||
}catch (Exception e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
for (String path : variables.keySet()) lines.add(path + ": " + variables.get(path).toString());
|
||||
Collections.sort(lines);
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
if (i != lines.size() - 1) out.append(lines.get(i)).append("\n");
|
||||
else out.append(lines.get(i));
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public void saveConfiguration() { saveVariables(); }
|
||||
|
||||
public void setValue(String path, Object value) {
|
||||
variables.remove(path);
|
||||
variables.put(path, value);
|
||||
}
|
||||
|
||||
public Object getObject(String path) { return variables.get(path); }
|
||||
public String getString(String path) { return variables.get(path).toString(); }
|
||||
public Integer getInt(String path) { return Integer.parseInt(variables.get(path).toString()); }
|
||||
public Float getFloat(String path) { return Float.parseFloat(variables.get(path).toString()); }
|
||||
public Double getDouble(String path) { return Double.parseDouble(variables.get(path).toString()); }
|
||||
public Boolean getBoolean(String path) { return Boolean.getBoolean(variables.get(path).toString()); }
|
||||
public Long getLong(String path) { return Long.parseLong(variables.get(path).toString()); }
|
||||
public Vector2 getVector(String path) { return Vector2.parseVector2(variables.get(path).toString()); }
|
||||
public Dimensions getDimensions(String path) { return Dimensions.parse(variables.get(path).toString()); }
|
||||
|
||||
}
|
||||
BIN
Java/GameEngine_old/src/test/Cool.mp3
Normal file
BIN
Java/GameEngine_old/src/test/Cool.mp3
Normal file
Binary file not shown.
49
Java/GameEngine_old/src/test/Main.java
Normal file
49
Java/GameEngine_old/src/test/Main.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package test;
|
||||
|
||||
import de.craftix.engine.GameEngine;
|
||||
import de.craftix.engine.InputManager;
|
||||
import de.craftix.engine.SpriteMap;
|
||||
import de.craftix.engine.objects.ColliderObject;
|
||||
import de.craftix.engine.objects.GameObject;
|
||||
import de.craftix.engine.objects.Texture;
|
||||
import de.craftix.engine.ui.Dimensions;
|
||||
import de.craftix.engine.ui.UIButton;
|
||||
import de.craftix.engine.ui.UIText;
|
||||
import de.craftix.engine.var.Vector2;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
public class Main extends GameEngine {
|
||||
static SpriteMap blocks = new SpriteMap(5, loadImage("test/terrain.png"), 16, 16);
|
||||
static GameObject player = new GameObject(new Vector2(0, 0), new Vector2(50, 50), false, false, false);
|
||||
|
||||
public static void main(String[] args) {
|
||||
player.setTexture(blocks.getTexture(3));
|
||||
setup(800, 600, "GameEngine", 60, new Main());
|
||||
startGame();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initialise() {
|
||||
setIcon(blocks.getTexture(4));
|
||||
setAntialiasing(true);
|
||||
showFrames(true);
|
||||
getScene().setBackground(new Color(146, 189, 221));
|
||||
getScene().addTexture(new Texture(blocks.getTexture(1), new Vector2(0, -200), new Vector2(getScreen().getWidth(), 200)));
|
||||
getScene().addColliderObject(new ColliderObject(new Vector2(0, -200), new Vector2(getScreen().getWidth(), 0), false));
|
||||
getScene().addGameObject(player);
|
||||
|
||||
UIButton button = new UIButton(new Dimensions(0, 0, 50, 50), new UIText("Test", new Font("Calibre", Font.BOLD, 20)), null);
|
||||
button.setTexture(blocks.getTexture(1), blocks.getTexture(2), blocks.getTexture(3));
|
||||
getUIManager().addComponent(button);
|
||||
getUIManager().addComponent(new UIText(0, 200, "Ur Mom Gae", new Font("Comic Sans MS", Font.PLAIN, 50)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void fixedUpdate() {
|
||||
if (InputManager.isKeyPressed(KeyEvent.VK_D)) player.getPhysics().addForce(5, 0);
|
||||
if (InputManager.isKeyPressed(KeyEvent.VK_A)) player.getPhysics().addForce(-5, 0);
|
||||
if (InputManager.isKeyPressed(KeyEvent.VK_SPACE) && player.getPhysics().isOnGround()) player.getPhysics().addForce(0, 200);
|
||||
}
|
||||
}
|
||||
BIN
Java/GameEngine_old/src/test/terrain.png
Normal file
BIN
Java/GameEngine_old/src/test/terrain.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Reference in New Issue
Block a user