Renderer/src/main/java/electrosphere/entity/EntityUtils.java
2024-03-09 20:05:47 -05:00

177 lines
7.3 KiB
Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package electrosphere.entity;
import electrosphere.engine.Globals;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.collision.CollisionObjUtils;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.renderer.actor.Actor;
import electrosphere.renderer.actor.ActorUtils;
import electrosphere.renderer.model.Model;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.ServerDataCell;
import electrosphere.server.datacell.utils.DataCellSearchUtils;
import electrosphere.server.datacell.utils.EntityLookupUtils;
import electrosphere.server.poseactor.PoseActor;
import org.joml.Quaterniond;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class EntityUtils {
public static Vector3d getPosition(Entity e){
return (Vector3d)e.getData(EntityDataStrings.DATA_STRING_POSITION);
}
public static Quaterniond getRotation(Entity e){
return (Quaterniond)e.getData(EntityDataStrings.DATA_STRING_ROTATION);
}
public static Vector3f getScale(Entity e){
return (Vector3f)e.getData(EntityDataStrings.DATA_STRING_SCALE);
}
public static String getModelPath(Entity e){
return (String)e.getData(EntityDataStrings.DATA_STRING_MODEL_PATH);
}
/**
* Creates an entity with an associated actor
* @param modelPath The model path of the model for the actor
* @return The entity
*/
protected static Entity spawnDrawableEntity(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
// rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaterniond().identity());
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true);
rVal.putData(EntityDataStrings.DRAW_SOLID_PASS, true);
Globals.clientScene.registerEntity(rVal);
Globals.clientScene.registerEntityToTag(rVal, EntityTags.DRAWABLE);
return rVal;
}
/**
* Creates an entity with an actor based on a model that already exists or is actively being loaded, not sure
* @param modelPath The path to the model in asset manager
* @return The entity
*/
protected static Entity spawnDrawableEntityWithPreexistingModel(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorOfLoadingModel(modelPath));
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaterniond().rotateAxis(0, new Vector3d(1,0,0)));
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true);
rVal.putData(EntityDataStrings.DRAW_SOLID_PASS, true);
Globals.clientScene.registerEntity(rVal);
Globals.clientScene.registerEntityToTag(rVal, EntityTags.DRAWABLE);
return rVal;
}
/**
* Spawns an entity with a backing pose actor (server side presumably)
* @param modelPath The model path to back the pose actor
* @return The entity
*/
protected static Entity spawnPoseableEntity(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.POSE_ACTOR, new PoseActor(modelPath));
// rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaterniond().identity());
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
rVal.putData(EntityDataStrings.DATA_STRING_DRAW, true);
rVal.putData(EntityDataStrings.DRAW_SOLID_PASS, true);
return rVal;
}
protected static Entity spawnUIEntity(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaterniond().rotateAxis(0, new Vector3d(1,0,0)));
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
rVal.putData(EntityDataStrings.DATA_STRING_UI_ELEMENT, true);
Globals.clientScene.registerEntity(rVal);
Globals.clientScene.registerEntityToTag(rVal, EntityTags.UI);
return rVal;
}
/**
* Spawns an entity that has a position in the world, but isn't necessarily drawable
* @return the entity
*/
protected static Entity spawnSpatialEntity(){
Entity rVal = new Entity();
// rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3d(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaterniond().identity());
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
EntityLookupUtils.registerServerEntity(rVal);
return rVal;
}
/**
* Cleans up the entity and deregisters it from all tracking datastructures
* @param e The entity to clean up
*/
public static void cleanUpEntity(Entity e){
//remove from client
Globals.clientSceneWrapper.getScene().deregisterEntity(e);
//remove from all server classes
if(Globals.realmManager != null){
Realm realm = Globals.realmManager.getEntityRealm(e);
if(realm != null){
//get data cell
ServerDataCell dataCell = realm.getEntityDataCellMapper().getEntityDataCell(e);
if(dataCell != null){
dataCell.getScene().deregisterEntity(e);
}
realm.getEntityDataCellMapper().ejectEntity(e);
}
Globals.realmManager.removeEntity(e);
}
EntityLookupUtils.removeEntity(e);
}
// public static void setEntityID(Entity e, int id){
// Globals.entityManager.mapIdToId(e.getId(), id);
// }
public static Actor getActor(Entity e){
return (Actor)e.getData(EntityDataStrings.DATA_STRING_ACTOR);
}
public static PoseActor getPoseActor(Entity e){
return (PoseActor)e.getData(EntityDataStrings.POSE_ACTOR);
}
public static void setVisible(Entity entity, boolean visible){
entity.putData(EntityDataStrings.DATA_STRING_DRAW, visible);
}
public static void setDraw(Entity entity, boolean draw){
entity.putData(EntityDataStrings.DATA_STRING_DRAW, draw);
}
public static boolean getDraw(Entity entity){
return (boolean)entity.getData(EntityDataStrings.DATA_STRING_DRAW);
}
}