diff --git a/bin/dev/jabo/kree/Component.class b/bin/dev/jabo/kree/Component.class new file mode 100644 index 0000000..9ae8aec Binary files /dev/null and b/bin/dev/jabo/kree/Component.class differ diff --git a/bin/dev/jabo/kree/Debug.class b/bin/dev/jabo/kree/Debug.class new file mode 100644 index 0000000..2f59216 Binary files /dev/null and b/bin/dev/jabo/kree/Debug.class differ diff --git a/bin/dev/jabo/kree/Game.class b/bin/dev/jabo/kree/Game.class new file mode 100644 index 0000000..7ff5ae6 Binary files /dev/null and b/bin/dev/jabo/kree/Game.class differ diff --git a/bin/dev/jabo/kree/GameObject.class b/bin/dev/jabo/kree/GameObject.class new file mode 100644 index 0000000..eeaa037 Binary files /dev/null and b/bin/dev/jabo/kree/GameObject.class differ diff --git a/bin/dev/jabo/kree/ImageLoader.class b/bin/dev/jabo/kree/ImageLoader.class new file mode 100644 index 0000000..f520171 Binary files /dev/null and b/bin/dev/jabo/kree/ImageLoader.class differ diff --git a/bin/dev/jabo/kree/Input.class b/bin/dev/jabo/kree/Input.class new file mode 100644 index 0000000..2c21f4c Binary files /dev/null and b/bin/dev/jabo/kree/Input.class differ diff --git a/bin/dev/jabo/kree/Scene.class b/bin/dev/jabo/kree/Scene.class new file mode 100644 index 0000000..5930f8e Binary files /dev/null and b/bin/dev/jabo/kree/Scene.class differ diff --git a/bin/dev/jabo/kree/SceneManager.class b/bin/dev/jabo/kree/SceneManager.class new file mode 100644 index 0000000..a8a2cdd Binary files /dev/null and b/bin/dev/jabo/kree/SceneManager.class differ diff --git a/bin/dev/jabo/kree/Sprite.class b/bin/dev/jabo/kree/Sprite.class new file mode 100644 index 0000000..c8b68b4 Binary files /dev/null and b/bin/dev/jabo/kree/Sprite.class differ diff --git a/bin/dev/jabo/kree/Transform.class b/bin/dev/jabo/kree/Transform.class new file mode 100644 index 0000000..27e74e2 Binary files /dev/null and b/bin/dev/jabo/kree/Transform.class differ diff --git a/bin/dev/jabo/kree/Vector2.class b/bin/dev/jabo/kree/Vector2.class new file mode 100644 index 0000000..d5afc4d Binary files /dev/null and b/bin/dev/jabo/kree/Vector2.class differ diff --git a/bin/dev/jabo/kree/Window.class b/bin/dev/jabo/kree/Window.class new file mode 100644 index 0000000..f568959 Binary files /dev/null and b/bin/dev/jabo/kree/Window.class differ diff --git a/bin/dev/jabo/kree/components/MeshRenderer.class b/bin/dev/jabo/kree/components/MeshRenderer.class new file mode 100644 index 0000000..25958cd Binary files /dev/null and b/bin/dev/jabo/kree/components/MeshRenderer.class differ diff --git a/bin/dev/jabo/kree/components/SpriteRenderer.class b/bin/dev/jabo/kree/components/SpriteRenderer.class new file mode 100644 index 0000000..80f72a9 Binary files /dev/null and b/bin/dev/jabo/kree/components/SpriteRenderer.class differ diff --git a/src/dev/jabo/kree/Component.java b/src/dev/jabo/kree/Component.java new file mode 100644 index 0000000..1373e4d --- /dev/null +++ b/src/dev/jabo/kree/Component.java @@ -0,0 +1,22 @@ +/* + * + * Component + * parent of all GameObject components + * + */ + +package dev.jabo.kree; + +import java.awt.Graphics; + +public abstract class Component { + + protected GameObject gameObject; + + protected String name; + + public abstract void Update(); + + public abstract void Render(Graphics g); + +} diff --git a/src/dev/jabo/kree/Debug.java b/src/dev/jabo/kree/Debug.java new file mode 100644 index 0000000..88d2b79 --- /dev/null +++ b/src/dev/jabo/kree/Debug.java @@ -0,0 +1,15 @@ +/* + * + * Debug Class is for debugging purposes + * + */ + +package dev.jabo.kree; + +public class Debug { + + public static void Log(Object out) { + System.out.println(out); + } + +} diff --git a/src/dev/jabo/kree/Game.java b/src/dev/jabo/kree/Game.java new file mode 100644 index 0000000..bcfb14d --- /dev/null +++ b/src/dev/jabo/kree/Game.java @@ -0,0 +1,115 @@ +package dev.jabo.kree; + +import java.awt.Graphics; +import java.awt.image.BufferStrategy; + +public class Game implements Runnable { + + private Thread thread; + private boolean gameRunning; + + private Graphics g; + private BufferStrategy bs; + + private Window window; + + public static Input INPUT = new Input(); + + public Game(Window window) { + this.window = window; + } + + public void Initialize() { + window.getWindow().addKeyListener(INPUT); + window.getCanvas().addMouseListener(INPUT); + window.getCanvas().addMouseMotionListener(INPUT); + } + + public void Update() { + + SceneManager.Update(); + + } + + public void Render() { + + bs = window.getCanvas().getBufferStrategy(); + if(bs == null) { + window.getCanvas().createBufferStrategy(3); + return; + } + g = bs.getDrawGraphics(); + g.clearRect(0, 0, window.getWindowWidth(), window.getWindowHeight()); + // DRAW HERE + + SceneManager.Render(g); + + // END + bs.show(); + g.dispose(); + + } + + public synchronized void start() { + if(gameRunning) + return; + gameRunning = true; + thread = new Thread(this); + thread.run(); + } + + public synchronized void stop() { + if(!gameRunning) + return; + gameRunning = false; + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.exit(0); + } + + @Override + public void run() { + + Initialize(); + + // Handles the FPS + int fps = 60; + double timePerTick = 1000000000 / fps; + double delta = 0; + long now; + long lastTime = System.nanoTime(); + long timer = 0; + int ticks = 0; + + while(gameRunning) { + + now = System.nanoTime(); + delta += (now - lastTime) / timePerTick; + timer += now - lastTime; + lastTime = now; + + if(delta >= 1){ + Update(); + Render(); + ticks++; + delta--; + } + + if(timer >= 1000000000){ + System.out.println("FPS: " + ticks); + ticks = 0; + timer = 0; + } + + } + + stop(); + + } + + +} diff --git a/src/dev/jabo/kree/GameObject.java b/src/dev/jabo/kree/GameObject.java new file mode 100644 index 0000000..de9ee21 --- /dev/null +++ b/src/dev/jabo/kree/GameObject.java @@ -0,0 +1,99 @@ +package dev.jabo.kree; + +import java.awt.Graphics; + +public class GameObject { + + public Transform transform = new Transform(); + + public Component[] components = new Component[0]; + + public float id; + public String name; + + private Scene parentScene; + + public GameObject(Scene parentScene, String name) { + this.name = name; + this.parentScene = parentScene; + + // Add to Scene GameObjects + GameObject[] newGameObjectList = new GameObject[parentScene.gameObjects.length + 1]; + for(int i = 0; i < parentScene.gameObjects.length; i++) { + newGameObjectList[i] = parentScene.gameObjects[i]; + } + newGameObjectList[parentScene.gameObjects.length] = this; + parentScene.gameObjects = newGameObjectList; + // End + + this.id = (float) (Math.random() * 100); + } + + public void Update() { + for(int i = 0; i < components.length; i++) { + components[i].Update(); + } + } + + public void Render(Graphics g) { + for(int i = 0; i < components.length; i++) { + components[i].Render(g); + } + } + + public Component GetComponent(String componentName) { + for(int i = 0; i < components.length; i++) { + if(components[i].name == componentName) { + return components[i]; + } + } + return null; + } + + public void AddComponent(Component component) { + + component.gameObject = this; + + // Add to GameObject's Component + Component[] newComponentList = new Component[components.length + 1]; + for(int i = 0; i < components.length; i++) { + newComponentList[i] = components[i]; + } + newComponentList[components.length] = component; + components = newComponentList; + // End + } + + // Set GameObject's position + public void SetPosition(Vector2 position) { + transform.position = position; + } + + // Set GameObject's dimension + public void SetScale(Vector2 scale) { + transform.scale = scale; + } + + public void Destroy() { + for(int i = 0; i < parentScene.gameObjects.length; i++) { + if(parentScene.gameObjects[i].id == this.id) { + parentScene.gameObjects[i] = null; + + // Remove GameObject from Scene + GameObject[] newGameObjectList = new GameObject[parentScene.gameObjects.length - 1]; + int c = 0; + for(int j = 0; j < parentScene.gameObjects.length; j++) { + if(parentScene.gameObjects[j] != null) { + newGameObjectList[c] = parentScene.gameObjects[j]; + c++; + } + } + parentScene.gameObjects = newGameObjectList; + // End + + return; + } + } + } + +} diff --git a/src/dev/jabo/kree/ImageLoader.java b/src/dev/jabo/kree/ImageLoader.java new file mode 100644 index 0000000..394f045 --- /dev/null +++ b/src/dev/jabo/kree/ImageLoader.java @@ -0,0 +1,21 @@ +package dev.jabo.kree; + +import java.awt.image.BufferedImage; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class ImageLoader { + + public static BufferedImage LoadImage(String path) { + + try { + return ImageIO.read(ImageLoader.class.getResource(path)); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/src/dev/jabo/kree/Input.java b/src/dev/jabo/kree/Input.java new file mode 100644 index 0000000..246388d --- /dev/null +++ b/src/dev/jabo/kree/Input.java @@ -0,0 +1,75 @@ +package dev.jabo.kree; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +public class Input implements KeyListener, MouseMotionListener, MouseListener { + + public static boolean mouseClicked; + public static int mouseX, mouseY; + public static boolean[] keys = new boolean[256]; + + @Override + public void keyPressed(KeyEvent arg0) { + keys[arg0.getKeyCode()] = true; + } + + @Override + public void keyReleased(KeyEvent arg0) { + keys[arg0.getKeyCode()] = false; + } + + @Override + public void keyTyped(KeyEvent arg0) { + + } + + @Override + public void mouseClicked(MouseEvent arg0) { + + } + + @Override + public void mouseEntered(MouseEvent arg0) { + + } + + @Override + public void mouseExited(MouseEvent arg0) { + + } + + @Override + public void mousePressed(MouseEvent arg0) { + mouseClicked = true; + } + + @Override + public void mouseReleased(MouseEvent arg0) { + mouseClicked = false; + } + + @Override + public void mouseDragged(MouseEvent arg0) { + + } + + @Override + public void mouseMoved(MouseEvent arg0) { + mouseX = arg0.getX(); + mouseY = arg0.getY(); + } + + public static Vector2 GetMouse() { + return new Vector2(mouseX, mouseY); + } + + public static boolean IsKeyPressed(int keyCode) { + return keys[keyCode]; + } + + +} diff --git a/src/dev/jabo/kree/Scene.java b/src/dev/jabo/kree/Scene.java new file mode 100644 index 0000000..a42dc0c --- /dev/null +++ b/src/dev/jabo/kree/Scene.java @@ -0,0 +1,23 @@ +package dev.jabo.kree; + +import java.awt.Graphics; + +public abstract class Scene { + + protected Game game; + + protected GameObject[] gameObjects = new GameObject[0]; + + public Scene(Game game) { + this.game = game; + } + + public abstract void Initialize(); + + public abstract void Update(); + + public abstract void Render(Graphics g); + + + +} diff --git a/src/dev/jabo/kree/SceneManager.java b/src/dev/jabo/kree/SceneManager.java new file mode 100644 index 0000000..4909f3e --- /dev/null +++ b/src/dev/jabo/kree/SceneManager.java @@ -0,0 +1,36 @@ +package dev.jabo.kree; + +import java.awt.Graphics; + +public class SceneManager { + + public static Scene activeScene; + + public static Scene GetScene() { + return activeScene; + } + + public static void SetScene(Scene scene) { + scene.Initialize(); + activeScene = scene; + } + + public static void Update() { + if(SceneManager.GetScene() != null) { + SceneManager.GetScene().Update(); + for(GameObject obj : activeScene.gameObjects) { + obj.Update(); + } + } + } + + public static void Render(Graphics g) { + if(SceneManager.GetScene() != null) { + SceneManager.GetScene().Render(g); + for(GameObject obj : activeScene.gameObjects) { + obj.Render(g); + } + } + } + +} diff --git a/src/dev/jabo/kree/Sprite.java b/src/dev/jabo/kree/Sprite.java new file mode 100644 index 0000000..675ade5 --- /dev/null +++ b/src/dev/jabo/kree/Sprite.java @@ -0,0 +1,15 @@ +package dev.jabo.kree; + +import java.awt.image.BufferedImage; + +public class Sprite { + + public BufferedImage image; + + public Sprite(String path) { + + this.image = ImageLoader.LoadImage(path); + + } + +} diff --git a/src/dev/jabo/kree/Transform.java b/src/dev/jabo/kree/Transform.java new file mode 100644 index 0000000..96ea84f --- /dev/null +++ b/src/dev/jabo/kree/Transform.java @@ -0,0 +1,17 @@ +package dev.jabo.kree; + +public class Transform { + + public Vector2 position; + public Vector2 scale; + + public Transform() { + position = new Vector2(0, 0); + scale = new Vector2(0, 0); + } + + public void Translate(Vector2 position) { + Vector2.Add(this.position, position); + } + +} diff --git a/src/dev/jabo/kree/Vector2.java b/src/dev/jabo/kree/Vector2.java new file mode 100644 index 0000000..ff379f6 --- /dev/null +++ b/src/dev/jabo/kree/Vector2.java @@ -0,0 +1,48 @@ +package dev.jabo.kree; + +public class Vector2 { + + public static Vector2 left = new Vector2(-1, 0); + public static Vector2 right = new Vector2(1, 0); + public static Vector2 up = new Vector2(0, -1); + public static Vector2 down = new Vector2(0, 1); + + public int x, y; + + public Vector2(int x, int y) { + this.x = x; + this.y = y; + } + + static int Distance(Vector2 v1, Vector2 v2) { + return (int) Math.floor( + Math.sqrt( + Math.pow(v2.x - v1.x, 2) + + Math.pow(v2.y - v1.y, 2) + ) + ); + } + + static void Add(Vector2 v1, Vector2 v2) { + v1.x += v2.x; + v1.y += v2.y; + } + + public void MoveTowards(Vector2 to, float speed) { + if(to == null) + return; + + if(Distance(this, to) < speed) { + return; + } + + int fX = to.x - this.x; + int fY = to.y - this.y; + float dist = (float) Math.sqrt( fX*fX + fY*fY ); + float step = (speed / dist); + this.x += fX * step; + this.y += fY * step; + + } + +} diff --git a/src/dev/jabo/kree/Window.java b/src/dev/jabo/kree/Window.java new file mode 100644 index 0000000..ec8a371 --- /dev/null +++ b/src/dev/jabo/kree/Window.java @@ -0,0 +1,66 @@ +package dev.jabo.kree; + +import java.awt.Canvas; +import java.awt.Dimension; + +import javax.swing.JFrame; + +public class Window { + + private JFrame window; + private Canvas canvas; + + private String windowTitle; + private int windowWidth, windowHeight; + + public Window(String windowTitle, int windowWidth, int windowHeight) { + + this.windowTitle = windowTitle; + this.windowWidth = windowWidth; + this.windowHeight = windowHeight; + + createWindow(); + + } + + private void createWindow() { + + window = new JFrame(windowTitle); + window.setSize(new Dimension(windowWidth, windowHeight)); + window.setLocationRelativeTo(null); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setResizable(false); + window.setVisible(true); + + canvas = new Canvas(); + canvas.setPreferredSize(new Dimension(windowWidth, windowHeight)); + canvas.setMinimumSize(new Dimension(windowWidth, windowHeight)); + canvas.setMaximumSize(new Dimension(windowWidth, windowHeight)); + canvas.setFocusable(false); + + window.add(canvas); + window.pack(); + + } + + public int getWindowWidth() { + return windowWidth; + } + + public int getWindowHeight() { + return windowHeight; + } + + public String getWindowTitle() { + return windowTitle; + } + + public JFrame getWindow() { + return window; + } + + public Canvas getCanvas() { + return canvas; + } + +} diff --git a/src/dev/jabo/kree/components/MeshRenderer.java b/src/dev/jabo/kree/components/MeshRenderer.java new file mode 100644 index 0000000..d23a1d0 --- /dev/null +++ b/src/dev/jabo/kree/components/MeshRenderer.java @@ -0,0 +1,25 @@ +package dev.jabo.kree.components; + +import java.awt.Graphics; + +import dev.jabo.kree.Component; + +public class MeshRenderer extends Component { + + public MeshRenderer() { + + } + + @Override + public void Update() { + + } + + @Override + public void Render(Graphics g) { + if(gameObject != null) { + g.fillRect(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.scale.x, gameObject.transform.scale.y); + } + } + +} diff --git a/src/dev/jabo/kree/components/SpriteRenderer.java b/src/dev/jabo/kree/components/SpriteRenderer.java new file mode 100644 index 0000000..8c069f3 --- /dev/null +++ b/src/dev/jabo/kree/components/SpriteRenderer.java @@ -0,0 +1,28 @@ +package dev.jabo.kree.components; + +import java.awt.Graphics; + +import dev.jabo.kree.Component; +import dev.jabo.kree.Sprite; + +public class SpriteRenderer extends Component { + + private Sprite sprite; + + public SpriteRenderer(String path) { + + this.sprite = new Sprite(path); + + } + + @Override + public void Update() { + + } + + @Override + public void Render(Graphics g) { + g.drawImage(sprite.image, gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.scale.x, gameObject.transform.scale.y, null); + } + +}