Basic scene loading to server

This commit is contained in:
austin 2022-12-08 22:53:40 -05:00
parent bde6002695
commit 9e2b0d396e
13 changed files with 245 additions and 14 deletions

View File

@ -0,0 +1,16 @@
{
"terrainObjects" : [
{
"objectId" : "terrain1",
"modelPath" : "Models/startingarea1.fbx",
"tokens" : [
],
"collidable": null
}
],
"files" : [
"Data/objects/floatingisland.json"
]
}

Binary file not shown.

View File

@ -0,0 +1 @@
console.log("Loaded testscene1");

View File

@ -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"
}

View File

@ -16,6 +16,7 @@ import electrosphere.controls.ControlHandler;
import electrosphere.entity.Entity; import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils; import electrosphere.entity.EntityUtils;
import electrosphere.entity.scene.SceneLoader;
import electrosphere.entity.state.movement.ApplyRotationTree; import electrosphere.entity.state.movement.ApplyRotationTree;
import electrosphere.entity.types.camera.CameraEntityUtils; import electrosphere.entity.types.camera.CameraEntityUtils;
import electrosphere.entity.types.creature.CreatureTemplate; import electrosphere.entity.types.creature.CreatureTemplate;
@ -766,6 +767,9 @@ public class LoadingThread extends Thread {
myCube.putData(EntityDataStrings.DRAW_TRANSPARENT_PASS, true); myCube.putData(EntityDataStrings.DRAW_TRANSPARENT_PASS, true);
EntityUtils.getPosition(myCube).set(3,1,3); EntityUtils.getPosition(myCube).set(3,1,3);
SceneLoader loader = new SceneLoader();
loader.serverInstantiateSceneFile("Scenes/testscene1/testscene1.json");
// Globals.entityManager.registerBehaviorTree(new BehaviorTree() { // Globals.entityManager.registerBehaviorTree(new BehaviorTree() {
// int i = 0; // int i = 0;
// public void simulate(){ // public void simulate(){

View File

@ -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;
}
}

View File

@ -0,0 +1,5 @@
package electrosphere.entity.scene;
public class Scene {
}

View File

@ -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<EntityDescriptor> entities;
//the paths relative to the assets folder of each script to be loaded when the scene is loaded
List<String> scriptPaths;
//the initial script to run when the scene is loaded into the engine
String initScriptPath;
public List<String> getScriptPaths(){
return scriptPaths;
}
public List<EntityDescriptor> getEntities(){
return entities;
}
public String getInitScriptPath(){
return initScriptPath;
}
}

View File

@ -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());
}
}

View File

@ -124,6 +124,16 @@ public class CollisionEngine {
//https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=11399 //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){ public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3d normal, Vector3d localPosition, Vector3d worldPos, float magnitude){
switch(receiver.getType()){ switch(receiver.getType()){

View File

@ -56,6 +56,7 @@ import electrosphere.renderer.ui.ElementManager;
import electrosphere.renderer.ui.elements.ImagePanel; import electrosphere.renderer.ui.elements.ImagePanel;
import electrosphere.renderer.ui.font.FontUtils; import electrosphere.renderer.ui.font.FontUtils;
import electrosphere.renderer.ui.font.RawFontMap; import electrosphere.renderer.ui.font.RawFontMap;
import electrosphere.script.ScriptEngine;
import electrosphere.util.FileUtils; import electrosphere.util.FileUtils;
/** /**
@ -244,6 +245,8 @@ public class Globals {
//micro simulation //micro simulation
public static MicroSimulation microSimulation; public static MicroSimulation microSimulation;
//script engine
public static ScriptEngine scriptEngine;
//manages hitboxes //manages hitboxes
public static HitboxManager hitboxManager; public static HitboxManager hitboxManager;
@ -336,6 +339,9 @@ public class Globals {
assetManager = new AssetManager(); assetManager = new AssetManager();
//load widget manager //load widget manager
elementManager = new ElementManager(); elementManager = new ElementManager();
//script engine
scriptEngine = new ScriptEngine();
scriptEngine.init();
//hitbox manager //hitbox manager
hitboxManager = new HitboxManager(); hitboxManager = new HitboxManager();
//ai manager //ai manager

View File

@ -9,6 +9,7 @@ import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source; import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value; import org.graalvm.polyglot.Value;
import electrosphere.logger.LoggerInterface;
import electrosphere.util.FileUtils; import electrosphere.util.FileUtils;
public class ScriptEngine { public class ScriptEngine {
@ -25,15 +26,15 @@ public class ScriptEngine {
//read scripts into source map //read scripts into source map
readScriptsDirectory("/Scripts", FileUtils.getAssetFile("/Scripts")); readScriptsDirectory("/Scripts", FileUtils.getAssetFile("/Scripts"));
//create bindings //create bindings
try { // try {
String content = FileUtils.getAssetFileAsString("/Scripts/test.js"); // String content = FileUtils.getAssetFileAsString("/Scripts/test.js");
Source source = Source.create("js", content); // Source source = Source.create("js", content);
context.eval(source); // context.eval(source);
System.out.println("Evaluated"); // System.out.println("Evaluated");
} catch (IOException e) { // } catch (IOException e) {
// TODO Auto-generated catch block // // TODO Auto-generated catch block
e.printStackTrace(); // e.printStackTrace();
} // }
} }
void createBindings(){ 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);
}
}
} }

View File

@ -258,11 +258,13 @@ public class FileUtils {
recursivelyDelete(child.getAbsolutePath()); recursivelyDelete(child.getAbsolutePath());
} }
} }
try { if(file.exists()){
Files.delete(file.toPath()); try {
} catch (IOException e) { Files.delete(file.toPath());
// TODO Auto-generated catch block } catch (IOException e) {
e.printStackTrace(); // TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }