package electrosphere.entity.state.lod; import org.joml.Vector3d; import electrosphere.collision.PhysicsEntityUtils; import electrosphere.collision.PhysicsUtils; import electrosphere.data.entity.common.CommonEntityType; import electrosphere.engine.Globals; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.net.synchronization.enums.FieldIdEnums; import electrosphere.server.datacell.Realm; import electrosphere.server.datacell.utils.DataCellSearchUtils; import electrosphere.net.parser.net.message.SynchronizationMessage; import electrosphere.entity.Entity; import electrosphere.net.synchronization.enums.BehaviorTreeIdEnums; import electrosphere.server.datacell.utils.ServerBehaviorTreeUtils; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.types.common.CommonEntityUtils; import electrosphere.net.synchronization.annotation.SyncedField; import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; /** * Creates a server LOD component */ @SynchronizedBehaviorTree(name = "serverLODTree", isServer = true, correspondingTree="clientLODTree") public class ServerLODComponent implements BehaviorTree { /** * Radius after which we reduce LOD */ public static final int LOD_RADIUS = 32; /** * Full resolution LOD */ public static final int FULL_RES = 1; /** * Low resolution */ public static final int LOW_RES = 0; /** * The current LOD level */ @SyncedField private int lodLevel; /** * The parent entity */ private Entity parent; @Override public void simulate(float deltaTime) { Vector3d parentLoc = EntityUtils.getPosition(this.parent); boolean fullRes = Globals.serverState.lodEmitterService.isFullLod(parentLoc); if(fullRes){ if(this.lodLevel != FULL_RES){ //make full res this.setLodLevel(FULL_RES); Realm realm = Globals.serverState.realmManager.getEntityRealm(this.parent); CommonEntityType type = CommonEntityUtils.getCommonData(this.parent); if(type.getCollidable() != null){ PhysicsEntityUtils.serverAttachCollidableTemplate(realm, this.parent, type.getCollidable()); } } } else { if(this.lodLevel != LOW_RES){ //make low res this.setLodLevel(LOW_RES); Realm realm = Globals.serverState.realmManager.getEntityRealm(this.parent); if(PhysicsEntityUtils.containsDBody(this.parent)){ PhysicsUtils.destroyPhysicsPair( realm.getCollisionEngine(), PhysicsEntityUtils.getDBody(this.parent), PhysicsEntityUtils.getCollidable(this.parent) ); } } } } /** *
(initially) Automatically generated
** Attaches this tree to the entity. *
* @param entity The entity to attach to * @param tree The behavior tree to attach * @param params Optional parameters that will be provided to the constructor */ public static ServerLODComponent attachTree(Entity parent, Object ... params){ ServerLODComponent rVal = new ServerLODComponent(parent,params); //!!WARNING!! from here below should not be touched //This was generated automatically to properly alert various systems that the btree exists and should be tracked ServerBehaviorTreeUtils.attachBTreeToEntity(parent, rVal); parent.putData(EntityDataStrings.TREE_SERVERLODTREE, rVal); Globals.serverState.entityValueTrackingService.attachTreeToEntity(parent, BehaviorTreeIdEnums.BTREE_SERVERLODTREE_ID); return rVal; } /** *Automatically generated
** Detatches this tree from the entity. *
* @param entity The entity to detach to * @param tree The behavior tree to detach */ public static void detachTree(Entity entity, BehaviorTree tree){ Globals.serverState.entityValueTrackingService.detatchTreeFromEntity(entity, BehaviorTreeIdEnums.BTREE_SERVERLODTREE_ID); } /** *(initially) Automatically generated
*Private constructor to enforce using the attach methods
** Constructor *
* @param parent The parent entity of this tree * @param params Optional parameters that can be provided when attaching the tree. All custom data required for creating this tree should be passed in this varargs. */ private ServerLODComponent(Entity parent, Object ... params){ this.parent = parent; this.lodLevel = ServerLODComponent.FULL_RES; } /** ** Gets the ServerLODComponent of the entity *
* @param entity the entity * @return The ServerLODComponent */ public static ServerLODComponent getServerLODComponent(Entity entity){ return (ServerLODComponent)entity.getData(EntityDataStrings.TREE_SERVERLODTREE); } /** ** Checks if the entity has a ServerLODComponent component *
* @param entity the entity * @return true if the entity contains the component, false otherwise */ public static boolean hasServerLODComponent(Entity entity){ return entity.containsKey(EntityDataStrings.TREE_SERVERLODTREE); } /** *Automatically generated
** Sets lodLevel and handles the synchronization logic for it. *
* @param lodLevel The value to set lodLevel to. */ public void setLodLevel(int lodLevel){ this.lodLevel = lodLevel; if(DataCellSearchUtils.getEntityDataCell(parent) != null){ DataCellSearchUtils.getEntityDataCell(parent).broadcastNetworkMessage(SynchronizationMessage.constructUpdateClientIntStateMessage(parent.getId(), BehaviorTreeIdEnums.BTREE_SERVERLODTREE_ID, FieldIdEnums.TREE_SERVERLODTREE_SYNCEDFIELD_LODLEVEL_ID, lodLevel)); } } /** *Automatically generated
** Gets lodLevel. *
*/ public int getLodLevel(){ return lodLevel; } }