diff --git a/assets/Data/terrainObjects.json b/assets/Data/terrainObjects.json new file mode 100644 index 00000000..00f9c3c3 --- /dev/null +++ b/assets/Data/terrainObjects.json @@ -0,0 +1,16 @@ +{ + "terrainObjects" : [ + + { + "objectId" : "terrain1", + "modelPath" : "Models/startingarea1.fbx", + "tokens" : [ + ], + "collidable": null + } + + ], + "files" : [ + "Data/objects/floatingisland.json" + ] +} \ No newline at end of file diff --git a/assets/Models/startingarea1.fbx b/assets/Models/startingarea1.fbx new file mode 100644 index 00000000..d1d4dc34 Binary files /dev/null and b/assets/Models/startingarea1.fbx differ diff --git a/assets/Scenes/testscene1/someScript.js b/assets/Scenes/testscene1/someScript.js new file mode 100644 index 00000000..c2804e3a --- /dev/null +++ b/assets/Scenes/testscene1/someScript.js @@ -0,0 +1 @@ +console.log("Loaded testscene1"); \ No newline at end of file diff --git a/assets/Scenes/testscene1/testscene1.json b/assets/Scenes/testscene1/testscene1.json new file mode 100644 index 00000000..99c6c52c --- /dev/null +++ b/assets/Scenes/testscene1/testscene1.json @@ -0,0 +1,19 @@ +{ + "entities": [ + { + "type": "item", + "subtype": "Katana", + "posX": 3, + "posY": 5, + "posZ": 5, + "rotX": 0, + "rotY": 0, + "rotZ": 0, + "rotW": 1 + } + ], + "scriptPaths": [ + "Scenes/testscene1/someScript.js" + ], + "initScriptPath": "Scenes/testscene1/someScript.js" +} \ No newline at end of file diff --git a/src/main/java/electrosphere/engine/LoadingThread.java b/src/main/java/electrosphere/engine/LoadingThread.java index a31775b5..73486677 100644 --- a/src/main/java/electrosphere/engine/LoadingThread.java +++ b/src/main/java/electrosphere/engine/LoadingThread.java @@ -16,6 +16,7 @@ import electrosphere.controls.ControlHandler; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; +import electrosphere.entity.scene.SceneLoader; import electrosphere.entity.state.movement.ApplyRotationTree; import electrosphere.entity.types.camera.CameraEntityUtils; import electrosphere.entity.types.creature.CreatureTemplate; @@ -766,6 +767,9 @@ public class LoadingThread extends Thread { myCube.putData(EntityDataStrings.DRAW_TRANSPARENT_PASS, true); EntityUtils.getPosition(myCube).set(3,1,3); + SceneLoader loader = new SceneLoader(); + loader.serverInstantiateSceneFile("Scenes/testscene1/testscene1.json"); + // Globals.entityManager.registerBehaviorTree(new BehaviorTree() { // int i = 0; // public void simulate(){ diff --git a/src/main/java/electrosphere/entity/scene/EntityDescriptor.java b/src/main/java/electrosphere/entity/scene/EntityDescriptor.java new file mode 100644 index 00000000..2fd0db2e --- /dev/null +++ b/src/main/java/electrosphere/entity/scene/EntityDescriptor.java @@ -0,0 +1,67 @@ +package electrosphere.entity.scene; + +/** + * Descriptor of an entity in a scene + */ +public class EntityDescriptor { + + /** + * The different types of entities that can be defined in an entity descriptor + * Principally used in SceneLoader.java's serverInstantiateSceneFile function + */ + public static final String TYPE_CREATURE = "creature"; + public static final String TYPE_ITEM = "item"; + public static final String TYPE_OBJECT = "object"; + + //the type of entity (creature, item, etc) + String type; + //the subtype (eg human, katana, etc) + String subtype; + //position of the entity in the scene + double posX; + double posY; + double posZ; + //rotation of the entity in the scene + double rotX; + double rotY; + double rotZ; + double rotW; + + public String getType(){ + return type; + } + + public String getSubtype(){ + return subtype; + } + + public double getPosX(){ + return posX; + } + + public double getPosY(){ + return posY; + } + + public double getPosZ(){ + return posZ; + } + + public double getRotX(){ + return rotX; + } + + public double getRotY(){ + return rotY; + } + + public double getRotZ(){ + return rotZ; + } + + public double getRotW(){ + return rotW; + } + + +} diff --git a/src/main/java/electrosphere/entity/scene/Scene.java b/src/main/java/electrosphere/entity/scene/Scene.java new file mode 100644 index 00000000..06aa150a --- /dev/null +++ b/src/main/java/electrosphere/entity/scene/Scene.java @@ -0,0 +1,5 @@ +package electrosphere.entity.scene; + +public class Scene { + +} diff --git a/src/main/java/electrosphere/entity/scene/SceneFile.java b/src/main/java/electrosphere/entity/scene/SceneFile.java new file mode 100644 index 00000000..0576a173 --- /dev/null +++ b/src/main/java/electrosphere/entity/scene/SceneFile.java @@ -0,0 +1,31 @@ +package electrosphere.entity.scene; + +import java.util.List; + +/** + * Model class for scene files + */ +public class SceneFile { + + //the entities in the scene + List entities; + + //the paths relative to the assets folder of each script to be loaded when the scene is loaded + List scriptPaths; + + //the initial script to run when the scene is loaded into the engine + String initScriptPath; + + public List getScriptPaths(){ + return scriptPaths; + } + + public List getEntities(){ + return entities; + } + + public String getInitScriptPath(){ + return initScriptPath; + } + +} diff --git a/src/main/java/electrosphere/entity/scene/SceneLoader.java b/src/main/java/electrosphere/entity/scene/SceneLoader.java new file mode 100644 index 00000000..cd2a8d90 --- /dev/null +++ b/src/main/java/electrosphere/entity/scene/SceneLoader.java @@ -0,0 +1,50 @@ +package electrosphere.entity.scene; + +import electrosphere.entity.Entity; +import electrosphere.entity.EntityUtils; +import electrosphere.entity.types.creature.CreatureUtils; +import electrosphere.entity.types.item.ItemUtils; +import electrosphere.main.Globals; +import electrosphere.util.FileUtils; + +/** + * Used to load scene files into the engine + */ +public class SceneLoader { + + /** + * Loads a scene file on the server + * @param path The path in the assets directory to a scene file + */ + public void serverInstantiateSceneFile(String path){ + SceneFile file = FileUtils.loadObjectFromAssetPath(path, SceneFile.class); + //spawn initial entities + for(EntityDescriptor descriptor : file.getEntities()){ + //spawn entity somehow + switch(descriptor.getType()){ + + case EntityDescriptor.TYPE_CREATURE: { + Entity newEntity = CreatureUtils.spawnBasicCreature(descriptor.subtype, null); + EntityUtils.getPosition(newEntity).set(descriptor.posX,descriptor.posY,descriptor.posZ); + EntityUtils.getRotation(newEntity).set((float)descriptor.rotX, (float)descriptor.rotY, (float)descriptor.rotZ, (float)descriptor.rotW); + } break; + + case EntityDescriptor.TYPE_ITEM: { + Entity newEntity = ItemUtils.spawnBasicItem(descriptor.subtype); + EntityUtils.getPosition(newEntity).set(descriptor.posX,descriptor.posY,descriptor.posZ); + EntityUtils.getRotation(newEntity).set((float)descriptor.rotX, (float)descriptor.rotY, (float)descriptor.rotZ, (float)descriptor.rotW); + } break; + + case EntityDescriptor.TYPE_OBJECT: + default: + throw new UnsupportedOperationException(); + } + } + //load scripts + for(String scriptPath : file.getScriptPaths()){ + Globals.scriptEngine.loadScript(scriptPath); + } + Globals.scriptEngine.runScript(file.getInitScriptPath()); + } + +} diff --git a/src/main/java/electrosphere/game/collision/CollisionEngine.java b/src/main/java/electrosphere/game/collision/CollisionEngine.java index 94800985..7b784a4d 100644 --- a/src/main/java/electrosphere/game/collision/CollisionEngine.java +++ b/src/main/java/electrosphere/game/collision/CollisionEngine.java @@ -124,6 +124,16 @@ public class CollisionEngine { //https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=11399 } + + public CollisionEngine(InternalTickCallback callback){ + broadphase = new DbvtBroadphase(); + collisionConfiguration = new DefaultCollisionConfiguration(); + dispatcher = new CollisionDispatcher(collisionConfiguration); + solver = new SequentialImpulseConstraintSolver(); + world = new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); + this.callback = callback; + world.setInternalTickCallback(callback, world); + } public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3d normal, Vector3d localPosition, Vector3d worldPos, float magnitude){ switch(receiver.getType()){ diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index ea21caf9..dde2013b 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -56,6 +56,7 @@ import electrosphere.renderer.ui.ElementManager; import electrosphere.renderer.ui.elements.ImagePanel; import electrosphere.renderer.ui.font.FontUtils; import electrosphere.renderer.ui.font.RawFontMap; +import electrosphere.script.ScriptEngine; import electrosphere.util.FileUtils; /** @@ -244,6 +245,8 @@ public class Globals { //micro simulation public static MicroSimulation microSimulation; + //script engine + public static ScriptEngine scriptEngine; //manages hitboxes public static HitboxManager hitboxManager; @@ -336,6 +339,9 @@ public class Globals { assetManager = new AssetManager(); //load widget manager elementManager = new ElementManager(); + //script engine + scriptEngine = new ScriptEngine(); + scriptEngine.init(); //hitbox manager hitboxManager = new HitboxManager(); //ai manager diff --git a/src/main/java/electrosphere/script/ScriptEngine.java b/src/main/java/electrosphere/script/ScriptEngine.java index 09c67a6e..e07c608d 100644 --- a/src/main/java/electrosphere/script/ScriptEngine.java +++ b/src/main/java/electrosphere/script/ScriptEngine.java @@ -9,6 +9,7 @@ import org.graalvm.polyglot.Context; import org.graalvm.polyglot.Source; import org.graalvm.polyglot.Value; +import electrosphere.logger.LoggerInterface; import electrosphere.util.FileUtils; public class ScriptEngine { @@ -25,15 +26,15 @@ public class ScriptEngine { //read scripts into source map readScriptsDirectory("/Scripts", FileUtils.getAssetFile("/Scripts")); //create bindings - try { - String content = FileUtils.getAssetFileAsString("/Scripts/test.js"); - Source source = Source.create("js", content); - context.eval(source); - System.out.println("Evaluated"); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + // try { + // String content = FileUtils.getAssetFileAsString("/Scripts/test.js"); + // Source source = Source.create("js", content); + // context.eval(source); + // System.out.println("Evaluated"); + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } } void createBindings(){ @@ -56,5 +57,24 @@ public class ScriptEngine { } } } + + public void loadScript(String path){ + String content; + try { + content = FileUtils.getAssetFileAsString(path); + sourceMap.put(path,Source.create("js",content)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + LoggerInterface.loggerGameLogic.ERROR("FAILED TO LOAD SCRIPT", e); + } + } + + public void runScript(String path){ + Source source = sourceMap.get(path); + if(source != null){ + context.eval(source); + } + } } diff --git a/src/main/java/electrosphere/util/FileUtils.java b/src/main/java/electrosphere/util/FileUtils.java index 3dd988a2..5822bd8b 100644 --- a/src/main/java/electrosphere/util/FileUtils.java +++ b/src/main/java/electrosphere/util/FileUtils.java @@ -258,11 +258,13 @@ public class FileUtils { recursivelyDelete(child.getAbsolutePath()); } } - try { - Files.delete(file.toPath()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if(file.exists()){ + try { + Files.delete(file.toPath()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } }