Renderer/src/main/java/electrosphere/entity/EntityCreationUtils.java
2023-05-25 18:04:22 -04:00

107 lines
4.3 KiB
Java

package electrosphere.entity;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;
import electrosphere.engine.Globals;
import electrosphere.renderer.actor.ActorUtils;
import electrosphere.renderer.actor.instance.InstancedActor;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.ServerDataCell;
import electrosphere.server.datacell.utils.EntityLookupUtils;
import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils;
import electrosphere.server.poseactor.PoseActor;
public class EntityCreationUtils {
/**
* Creates a server entity in the given realm and position. This uses spatial entity as a server entity can't (currently) exist outside of a realm.
* @param realm The realm to attach the entity to
* @param position The position to place the entity at
* @return The entity
*/
public static Entity createServerEntity(Realm realm, Vector3d position){
Entity rVal = EntityUtils.spawnSpatialEntity();
//register to global entity id lookup table
EntityLookupUtils.registerServerEntity(rVal);
//register to data cell
ServerDataCell entityDataCell = realm.getDataCellManager().tryCreateCellAtPoint(position);
entityDataCell.getScene().registerEntity(rVal);
//maps this entity to its realm
Globals.realmManager.mapEntityToRealm(rVal, realm);
//enable behavior tree tracking
ServerBehaviorTreeUtils.registerEntity(rVal);
//register to entity data cell mapper
realm.getEntityDataCellMapper().registerEntity(rVal, entityDataCell);
return rVal;
}
/**
* Spawns an entity that is not attached to a realm (for instance an item in an inventory)
* @return The entity
*/
public static Entity createRealmlessServerEntity(){
Entity rVal = new Entity();
//register to global entity id lookup table
EntityLookupUtils.registerServerEntity(rVal);
//enable behavior tree tracking
ServerBehaviorTreeUtils.registerEntity(rVal);
return rVal;
}
/**
* Creates an entity for the client
* @return The entity
*/
public static Entity createClientSpatialEntity(){
Entity rVal = EntityUtils.spawnSpatialEntity();
Globals.clientSceneWrapper.getScene().registerEntity(rVal);
return rVal;
}
/**
* Creates a non-spatial entity for the client
* @return The entity
*/
public static Entity createClientNonSpatialEntity(){
Entity rVal = new Entity();
Globals.clientSceneWrapper.getScene().registerEntity(rVal);
return rVal;
}
/**
* Makes an already created entity a poseable entity by backing it with a PoseActor
* @param entity The entity
* @param modelPath The model path for the model to back the pose actor
*/
public static void makeEntityPoseable(Entity entity, String modelPath){
entity.putData(EntityDataStrings.POSE_ACTOR, new PoseActor(modelPath));
entity.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
entity.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().identity());
entity.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
entity.putData(EntityDataStrings.DATA_STRING_DRAW, true);
entity.putData(EntityDataStrings.DRAW_SOLID_PASS, true);
}
/**
* MAkes an already created entity a drawable entity (client only) by backing it with an Actor
* @param entity The entity
* @param modelPath The model path for the model to back the actor
*/
public static void makeEntityDrawable(Entity entity, String modelPath){
entity.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
entity.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
entity.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().identity());
entity.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
entity.putData(EntityDataStrings.DATA_STRING_DRAW, true);
entity.putData(EntityDataStrings.DRAW_SOLID_PASS, true);
Globals.clientScene.registerEntity(entity);
Globals.clientScene.registerEntityToTag(entity, EntityTags.DRAWABLE);
}
}