Renderer/src/main/java/electrosphere/entity/scene/SceneLoader.java
2023-01-28 22:23:01 -05:00

63 lines
2.8 KiB
Java

package electrosphere.entity.scene;
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.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 path in the assets directory to a scene file
*/
public ServerDataCell serverInstantiateSceneFile(String path){
ServerDataCell rVal = Globals.dataCellManager.createNewCell();
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);
rVal.initializeEntityForNewPlayers(newEntity, rVal);
} 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);
rVal.initializeEntityForNewPlayers(newEntity, rVal);
} break;
case EntityDescriptor.TYPE_OBJECT: {
Entity newEntity = ObjectUtils.spawnBasicObject(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
for(String scriptPath : file.getScriptPaths()){
Globals.scriptEngine.loadScript(scriptPath);
}
Globals.scriptEngine.runScript(file.getInitScriptPath());
return rVal;
}
}