From 0cc8babc19f034f3b67ff63ba246efea905171e9 Mon Sep 17 00:00:00 2001 From: Marcus Gosselin Date: Mon, 15 Feb 2016 17:52:56 -0500 Subject: [PATCH] asdf --- .classpath | 6 ++ .project | 17 +++ .settings/org.eclipse.jdt.core.prefs | 11 ++ src/diveengine2d/DiveScript.java | 31 ++++++ src/diveengine2d/Engine.java | 150 +++++++++++++++++++++++++++ src/diveengine2d/Entity.java | 30 ++++++ src/diveengine2d/RectRenderer.java | 16 +++ src/diveengine2d/SceneManager.java | 144 +++++++++++++++++++++++++ 8 files changed, 405 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/diveengine2d/DiveScript.java create mode 100644 src/diveengine2d/Engine.java create mode 100644 src/diveengine2d/Entity.java create mode 100644 src/diveengine2d/RectRenderer.java create mode 100644 src/diveengine2d/SceneManager.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fceb480 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..1978572 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Dive Engine 2D + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/diveengine2d/DiveScript.java b/src/diveengine2d/DiveScript.java new file mode 100644 index 0000000..2a1300a --- /dev/null +++ b/src/diveengine2d/DiveScript.java @@ -0,0 +1,31 @@ +package diveengine2d; + +import java.awt.Graphics2D; + +public abstract class DiveScript { + public boolean enabled = true; + public String name; + public Entity entity; + + /** + * called every frame + */ + public void update() {} + + /** + * when initialized in the scene.
+ * SCENE WILL BE INCOMPLETE HERE. + */ + public void create() {} + + /** + * called after the scene has been initialized + */ + public void awake() {} + + /** + * render method beeboop + * @param g + */ + public void render(Graphics2D g) {} +} diff --git a/src/diveengine2d/Engine.java b/src/diveengine2d/Engine.java new file mode 100644 index 0000000..933ec31 --- /dev/null +++ b/src/diveengine2d/Engine.java @@ -0,0 +1,150 @@ +package diveengine2d; + +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.image.BufferStrategy; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import javax.swing.JFrame; + +public class Engine extends Canvas { + public static String gameFolder = null; + public static int WIDTH, HEIGHT; + public static String startScene = null; + public static String name = null; + public static BufferStrategy bs; + + public Engine(String gameFolder) { + + // setup the folder + this.gameFolder = gameFolder; + + System.out.println("Engine started with folder " + gameFolder + " ..."); + + boolean configFile = false; + try { + + // get the config values from the config file + loadConfig(); + configFile = true; + + } catch (Exception e) { + e.printStackTrace(); + } + + // if we failed, screw this. + if (!configFile) + return; + + // now, lets make our window. + + System.out.println("start scene: " + startScene); + System.out.println("resolution: " + WIDTH + " X " + HEIGHT); + + SceneManager.loadScene(startScene); + + SceneManager.entityDump(); + + JFrame frame = new JFrame(name); + frame.add(this); + this.setPreferredSize(new Dimension(WIDTH, HEIGHT)); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + createBufferStrategy(2); + bs = getBufferStrategy(); + + while(true) { + repaint(); + try{ + Thread.sleep(17); + }catch(Exception e) { + + } + } + + } + + private void loadConfig() throws Exception { + + File configFile = null; + try { + configFile = new File(gameFolder + "\\build.config"); + } catch (Exception e) { + throw new Exception("Configuration File not found"); + } + + List lines = Files.readAllLines(Paths.get(gameFolder, "build.config")); + + for (String line : lines) { + + if (line.startsWith("#")) + continue; + + String[] parts = line.split("="); + + if (parts.length != 2) { + System.out.println("line has incorrect parts length: '" + line + "'"); + System.out.println("ignoring..."); + continue; + } + + parts[0] = parts[0].trim(); + parts[1] = parts[1].trim(); + + if (parts[0].equals("StartScene")) { + this.startScene = parts[1]; + } else if (parts[0].equals("Resolution")) { + + String[] resparts = parts[1].split("x"); + + if (resparts.length != 2) { + System.out.println("line has incorrect parts length: '" + resparts + "'"); + System.out.println("ignoring..."); + continue; + } + + resparts[0] = resparts[0].trim(); + resparts[1] = resparts[1].trim(); + + try { + WIDTH = Integer.parseInt(resparts[0]); + HEIGHT = Integer.parseInt(resparts[1]); + } catch (NumberFormatException e) { + System.out.println("line has incorrect resolution: '" + parts[1] + "'"); + System.out.println("ignoring..."); + continue; + } + } else if (parts[0].equals("name")) { + name = parts[1].trim(); + } + + } + + System.out.println("Loaded Config File..."); + + } + + public void update(Graphics g) { + Graphics2D g2 = null; + try { + g2 = (Graphics2D) bs.getDrawGraphics(); + render(g2); + } finally { + g2.dispose(); + } + bs.show(); + } + + private void render(Graphics2D g) { + SceneManager.render(g); + g.setColor(Color.BLACK); + } +} diff --git a/src/diveengine2d/Entity.java b/src/diveengine2d/Entity.java new file mode 100644 index 0000000..d9fd108 --- /dev/null +++ b/src/diveengine2d/Entity.java @@ -0,0 +1,30 @@ +package diveengine2d; +import java.util.ArrayList; +import java.util.List; + +public class Entity { + public float x = 0, y = 0, rotation = 0; + public boolean enabled = true; + public String GUID = null; + public String name = null; + + public List components = new ArrayList(); + + public String toString() { + return "name: " + name + "\n" + + "GUID: " + GUID + + componentsToString(); + + } + private String componentsToString() { + String _return = ""; + for(DiveScript c : components) { + _return += "\nComponent " + c.name; + } + return _return; + } + + public void addComponent(DiveScript component) { + components.add(component); + } +} diff --git a/src/diveengine2d/RectRenderer.java b/src/diveengine2d/RectRenderer.java new file mode 100644 index 0000000..b8e2cb0 --- /dev/null +++ b/src/diveengine2d/RectRenderer.java @@ -0,0 +1,16 @@ +package diveengine2d; + +import java.awt.Color; +import java.awt.Graphics2D; + +public class RectRenderer extends DiveScript{ + + public Color color = null; + public int width, height; + + public void render(Graphics2D g) { + g.setColor(color); + g.fillRect((int)entity.x, (int)entity.y, width, height); + + } +} diff --git a/src/diveengine2d/SceneManager.java b/src/diveengine2d/SceneManager.java new file mode 100644 index 0000000..688371a --- /dev/null +++ b/src/diveengine2d/SceneManager.java @@ -0,0 +1,144 @@ +package diveengine2d; +import java.awt.Color; +import java.awt.Graphics2D; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class SceneManager { + private static ArrayList entities = new ArrayList(); + + /** + * load scene by filename without the .scene extension. + * @param scene + * @return + */ + public static boolean loadScene(String scene) { + synchronized(entities) { + try{ + List lines = Files.readAllLines(Paths.get(Engine.gameFolder, scene + ".scene")); + Stack scopeObject = new Stack(); + for (String line : lines) { + //System.out.println(line); + line = line.trim(); + if(line.startsWith("#")) continue; + if(line.startsWith("[") && line.endsWith("]")){ + String newLine = line.substring(1, line.length() - 1); + Class entityClass = null; + try{ + entityClass = Class.forName(newLine); + }catch(ClassNotFoundException e) { + System.out.println(newLine + " class not found!"); + continue; + } + Object object = entityClass.newInstance(); + if(!(object instanceof Entity)) { + System.out.println(entityClass + " is not an entity"); + continue; + } + entities.add((Entity)object); + scopeObject.push((Entity)object); + }else if(line.startsWith("$")) { + //scope variable + String[] parts = line.substring(1).split("="); + if(parts.length != 2) { + System.out.println("line: " + line + " is incorrect.\nignoring..."); + continue; + } + parts[0] = parts[0].trim(); + parts[1] = parts[1].trim(); + String key = parts[0], value = parts[1]; + + if(scopeObject.isEmpty()) { + System.out.println("no object in scope to bind " + key + " to"); + continue; + } + + if(value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length() - 1); + scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), value); + }else if(value.startsWith("#")) { + value = value.substring(1, value.length()); + //TODO CHECK IF 6 characters + int r = Integer.parseInt(value.substring(0, 2), 16); + int g = Integer.parseInt(value.substring(2, 4), 16); + int b = Integer.parseInt(value.substring(4, 6), 16); + + Color color = new Color(r, g, b); + + scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), color); + }else if(value.endsWith("F") || value.endsWith("f")) { + value = value.substring(0, value.length() - 1); + + int intValue; + try{ + intValue = Integer.parseInt(value); + }catch(Exception e) { + System.out.println(value + " is not an int"); + continue; + } + + scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), intValue); + } + + }else if(line.startsWith("Component")) { + //component + if(!(scopeObject.peek() instanceof Entity)) { + System.out.println("Con not apply a component to " + scopeObject.peek()); + continue; + } + String[] parts = line.substring(1).split(" "); + if(parts.length != 2) { + System.out.println("line: " + line + " is incorrect.\nignoring..."); + continue; + } + //we really only care about this part anyways so... + String componentClass = parts[1].trim(); + DiveScript component = null; + try { + Object maybeComponent = Class.forName(componentClass).newInstance(); + if(maybeComponent instanceof DiveScript) { + component = (DiveScript)maybeComponent; + }else { + System.out.println("" + componentClass + " is not of type component!"); + continue; + } + } catch( ClassNotFoundException e ) { + System.out.println("Component " + componentClass + " not found!"); + continue; + } + component.entity = (Entity) scopeObject.peek(); + ((Entity)scopeObject.peek()).addComponent(component); + scopeObject.push(component); + }else if (line.equals("End Component")) { + scopeObject.pop(); + } + } + }catch(Exception e) { + System.out.println(e.getMessage()); + return false; + } + System.out.println("Loaded Scene File..."); + return true; + } + } + + public static void entityDump() { + synchronized(entities) { + System.out.println("--------------------ENTITY DUMP--------------------\n"); + for(Entity entity : entities) { + System.out.println("" + entity + "\n"); + } + } + } + + public static void render(Graphics2D g) { + for(Entity e : entities) { + for(DiveScript script : e.components) { + script.render(g); + } + } + } +}