Skip to content

Commit

Permalink
Initiali Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jabo-bernardo authored Apr 11, 2020
1 parent 7db82c6 commit 5384b02
Show file tree
Hide file tree
Showing 28 changed files with 605 additions and 0 deletions.
Binary file added bin/dev/jabo/kree/Component.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Debug.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Game.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/GameObject.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/ImageLoader.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Input.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Scene.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/SceneManager.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Sprite.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Transform.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Vector2.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/Window.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/components/MeshRenderer.class
Binary file not shown.
Binary file added bin/dev/jabo/kree/components/SpriteRenderer.class
Binary file not shown.
22 changes: 22 additions & 0 deletions src/dev/jabo/kree/Component.java
Original file line number Diff line number Diff line change
@@ -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);

}
15 changes: 15 additions & 0 deletions src/dev/jabo/kree/Debug.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
115 changes: 115 additions & 0 deletions src/dev/jabo/kree/Game.java
Original file line number Diff line number Diff line change
@@ -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();

}


}
99 changes: 99 additions & 0 deletions src/dev/jabo/kree/GameObject.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

}
21 changes: 21 additions & 0 deletions src/dev/jabo/kree/ImageLoader.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
75 changes: 75 additions & 0 deletions src/dev/jabo/kree/Input.java
Original file line number Diff line number Diff line change
@@ -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];
}


}
23 changes: 23 additions & 0 deletions src/dev/jabo/kree/Scene.java
Original file line number Diff line number Diff line change
@@ -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);



}
Loading

0 comments on commit 5384b02

Please sign in to comment.