templating

rotation
Marcus Gosselin 2016-02-16 06:20:24 -05:00
parent 394fcd6a45
commit 7499b82596
1 changed files with 129 additions and 113 deletions

View File

@ -18,139 +18,155 @@ public class SceneManager {
*/ */
public static boolean loadScene(String scene) { public static boolean loadScene(String scene) {
synchronized(entities) { synchronized(entities) {
try{ entities = getScene(scene + ".scene");
List<String> lines = Files.readAllLines(Paths.get(Engine.gameFolder, scene + ".scene"), Charset.forName("UTF-8")); System.out.println("Loaded Scene File...");
Stack<Object> scopeObject = new Stack<Object>(); return true;
for (String line : lines) { }
//System.out.println(line); }
line = line.trim();
if(line.startsWith("#")) continue; private static Entity getTemplate(String name) {
if(line.startsWith("[") && line.endsWith("]")){ return getScene(name + ".template").get(0);
String newLine = line.substring(1, line.length() - 1); }
Class<?> entityClass = null;
private static ArrayList<Entity> getScene(String scene) {
ArrayList<Entity> entities = new ArrayList<Entity>();
try{
List<String> lines = Files.readAllLines(Paths.get(Engine.gameFolder, scene), Charset.forName("UTF-8"));
Stack<Object> scopeObject = new Stack<Object>();
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("I") || value.endsWith("i")) {
value = value.substring(0, value.length() - 1);
int intValue;
try{ try{
entityClass = Class.forName(newLine); intValue = Integer.parseInt(value);
}catch(ClassNotFoundException e) { }catch(Exception e) {
System.out.println(newLine + " class not found!"); System.out.println(value + " is not an int");
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; continue;
} }
if(value.startsWith("\"") && value.endsWith("\"")) { scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), intValue);
value = value.substring(1, value.length() - 1); }else if(value.endsWith("F") || value.endsWith("f")) {
scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), value); value = value.substring(0, value.length() - 1);
}else if(value.startsWith("#")) {
value = value.substring(1, value.length()); float intValue;
//TODO CHECK IF 6 characters try{
int r = Integer.parseInt(value.substring(0, 2), 16); intValue = Float.parseFloat(value);
int g = Integer.parseInt(value.substring(2, 4), 16); }catch(Exception e) {
int b = Integer.parseInt(value.substring(4, 6), 16); System.out.println(value + " is not a float");
continue;
Color color = new Color(r, g, b);
scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), color);
}else if(value.endsWith("I") || value.endsWith("i")) {
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(value.endsWith("F") || value.endsWith("f")) {
value = value.substring(0, value.length() - 1);
float intValue;
try{
intValue = Float.parseFloat(value);
}catch(Exception e) {
System.out.println(value + " is not a float");
continue;
}
scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), intValue);
} }
}else if(line.startsWith("Component")) { scopeObject.peek().getClass().getField(key).set(scopeObject.peek(), intValue);
//component }
if(!(scopeObject.peek() instanceof Entity)) {
System.out.println("Con not apply a component to " + scopeObject.peek()); }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; continue;
} }
String[] parts = line.substring(1).split(" "); } catch( ClassNotFoundException e) {
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 { try {
Object maybeComponent = Class.forName(componentClass).newInstance(); Object maybeComponent = Class.forName("diveengine2d." + componentClass).newInstance();
if(maybeComponent instanceof DiveScript) { if(maybeComponent instanceof DiveScript) {
component = (DiveScript)maybeComponent; component = (DiveScript)maybeComponent;
}else { }else {
System.out.println("" + componentClass + " is not of type component!"); System.out.println("" + componentClass + " is not of type component!");
continue; continue;
} }
} catch( ClassNotFoundException e) { } catch( ClassNotFoundException ex) {
try { System.out.println("Component " + componentClass + " not found!");
Object maybeComponent = Class.forName("diveengine2d." + componentClass).newInstance(); continue;
if(maybeComponent instanceof DiveScript) {
component = (DiveScript)maybeComponent;
}else {
System.out.println("" + componentClass + " is not of type component!");
continue;
}
} catch( ClassNotFoundException ex) {
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();
} }
component.entity = (Entity) scopeObject.peek();
((Entity)scopeObject.peek()).addComponent(component);
scopeObject.push(component);
}else if (line.equals("End Component")) {
scopeObject.pop();
}else if (line.startsWith("Template")) {
String[] parts = line.split(" ");
String template = parts[1].trim();
Entity entity = getTemplate(template);
entities.add(entity);
} }
for(Entity e : entities) {
for(DiveScript script : e.components) {
script.create();
}
}
}catch(Exception e) {
System.out.println(e.getMessage());
return false;
} }
System.out.println("Loaded Scene File..."); for(Entity e : entities) {
return true; for(DiveScript script : e.components) {
script.create();
}
}
}catch(Exception e) {
System.out.println(e.getMessage());
return null;
} }
return entities;
} }
public static void entityDump() { public static void entityDump() {