Renderer/src/main/java/electrosphere/entity/scene/SceneLoader.java
2024-07-13 21:11:46 -04:00

116 lines
5.1 KiB
Java

package electrosphere.entity.scene;
import org.joml.Vector3d;
import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.entity.types.object.ObjectUtils;
import electrosphere.game.server.world.ServerWorldData;
import electrosphere.server.content.ServerContentManager;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.ServerDataCell;
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 name of the save to look for a scene file within
*/
public static void serverInstantiateSaveSceneFile(String saveName, ServerWorldData serverWorldData){
//load scene file
SceneFile file = FileUtils.loadObjectFromSavePath(saveName, "/scene.json", SceneFile.class);
//instantiate scene data
serverInstantiateSceneFile(file,serverWorldData);
}
/**
* Loads a scene file on the server
* @param path The name of the scene in the assets/scenes folder
*/
public static void serverInstantiateAssetSceneFile(String sceneName, ServerWorldData serverWorldData){
//load scene file
String sanitizedPath = FileUtils.sanitizeFilePath("/Scenes/" + sceneName + "/scene.json");
SceneFile file = FileUtils.loadObjectFromAssetPath(sanitizedPath, SceneFile.class);
//instantiate scene data
serverInstantiateSceneFile(file,serverWorldData);
}
/**
* Loads a scene file on the server
* @param path The path in the assets directory to a scene file
* @param isSave if true, will try to load scene from save file instead of asset file
*/
private static void serverInstantiateSceneFile(SceneFile file, ServerWorldData serverWorldData){
//
//Content manager
//
ServerContentManager serverContentManager = null;
if(file.realmDescriptor.getType() == RealmDescriptor.REALM_DESCRIPTOR_PROCEDURAL){
serverContentManager = ServerContentManager.createServerContentManager(true);
} else {
serverContentManager = ServerContentManager.createServerContentManager(false);
}
//
//Init the realm
//
Realm realm = null;
switch(file.realmDescriptor.getType()){
case RealmDescriptor.REALM_DESCRIPTOR_GRIDDED: {
realm = Globals.realmManager.createGriddedRealm(serverWorldData,serverContentManager);
} break;
case RealmDescriptor.REALM_DESCRIPTOR_PROCEDURAL: {
realm = Globals.realmManager.createRealm();
} break;
}
ServerDataCell rVal = realm.createNewCell();
//spawn initial entities
for(EntityDescriptor descriptor : file.getEntities()){
//spawn entity somehow
switch(descriptor.getType()){
case EntityDescriptor.TYPE_CREATURE: {
Vector3d position = new Vector3d(descriptor.posX,descriptor.posY,descriptor.posZ);
Entity newEntity = CreatureUtils.serverSpawnBasicCreature(realm, position, descriptor.subtype, null);
EntityUtils.getRotation(newEntity).set((float)descriptor.rotX, (float)descriptor.rotY, (float)descriptor.rotZ, (float)descriptor.rotW);
rVal.initializeEntityForNewPlayers(newEntity, rVal);
} break;
case EntityDescriptor.TYPE_ITEM: {
Vector3d position = new Vector3d(descriptor.posX,descriptor.posY,descriptor.posZ);
Entity newEntity = ItemUtils.serverSpawnBasicItem(realm, position, descriptor.subtype);
EntityUtils.getRotation(newEntity).set((float)descriptor.rotX, (float)descriptor.rotY, (float)descriptor.rotZ, (float)descriptor.rotW);
rVal.initializeEntityForNewPlayers(newEntity, rVal);
} break;
case EntityDescriptor.TYPE_OBJECT: {
Vector3d position = new Vector3d(descriptor.posX,descriptor.posY,descriptor.posZ);
Entity newEntity = ObjectUtils.serverSpawnBasicObject(realm, position, 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);
rVal.initializeEntityForNewPlayers(newEntity, rVal);
} break;
default:
throw new UnsupportedOperationException();
}
}
//load scripts
//TODO: integrate scripts for client side of scenes
// for(String scriptPath : file.getScriptPaths()){
// Globals.scriptEngine.loadScript(scriptPath);
// }
// Globals.scriptEngine.runScript(file.getInitScriptPath());
//TODO: instruct client to load the scene
}
}