package electrosphere.server.datacell; import electrosphere.collision.CollisionEngine; import electrosphere.engine.Globals; import electrosphere.engine.Main; import electrosphere.entity.Entity; import electrosphere.entity.Scene; import electrosphere.entity.types.hitbox.HitboxManager; import electrosphere.menu.debug.ImGuiWindowMacros; import electrosphere.net.parser.net.message.NetworkMessage; import electrosphere.server.datacell.interfaces.DataCellManager; import java.util.HashSet; import java.util.Set; /** * Manages data cells on the server side */ public class Realm { //The set containing all data cells loaded into this realm Set loadedDataCells = new HashSet(); //this is the cell that all players loading into the game (via connection startup, death, etc) reside in ServerDataCell loadingCell = new ServerDataCell(new Scene()); //resolver for entity -> data cell within this realm EntityDataCellMapper entityDataCellMapper; //provides functions for relating data cells to physical locations (eg creating cells, deleting cells, etc) DataCellManager dataCellManager; //Main entity physics collision checking engine CollisionEngine collisionEngine; //Hitbox manager for the realm HitboxManager hitboxManager; /** * Realm constructor * @param collisionEngine The collision engine for the realm * @param hitboxManager The hitbox manager for the realm */ protected Realm(CollisionEngine collisionEngine, HitboxManager hitboxManager){ this.collisionEngine = collisionEngine; this.hitboxManager = hitboxManager; } /** * Creates a new data cell * @return The new data cell */ public ServerDataCell createNewCell(){ ServerDataCell newCell = new ServerDataCell(new Scene()); loadedDataCells.add(newCell); return newCell; } /** * Removes a data cell from tracking in this data cell manager * @param cell The data cell to no longer keep track of */ public void deregisterCell(ServerDataCell cell){ loadedDataCells.remove(cell); } /** * Gets the default loading data cell * @return The default loading data cell */ public ServerDataCell getLoadingDataCell(){ return loadingCell; } /** * Broadcasts a message to all players in a certain serverdatacell * @param message The message to send * @param cell The serverdatacell */ public void sendNetworkMessageToChunk(NetworkMessage message, Entity e){ //solve for what data cell the entitiy is in ServerDataCell cell = Globals.entityDataCellMapper.getEntityDataCell(e); cell.broadcastNetworkMessage(message); } /** * If we're spawning an entity for the first time, call this method with the cell you want it to start in. * It adds the entity to the given cell and initializes it for all players in said cell * @param entity The entity we are initializing * @param cell The cell we are wanting to initialize the entity in */ public void initializeServerSideEntity(Entity entity, ServerDataCell cell){ //register entity to this realm Globals.realmManager.mapEntityToRealm(entity, this); //add the entity to the cell cell.getScene().registerEntity(entity); //send the entity to all players cell.initializeEntityForNewPlayers(entity, null); //register to entity data cell mapper Globals.entityDataCellMapper.registerEntity(entity, cell); } /** * Gets the entity data cell mapper for this realm * @return The entity data cell mapper for this realm */ public EntityDataCellMapper getEntityDataCellMapper(){ return this.entityDataCellMapper; } /** * Gets the data cell manager for this realm * @return The data cell manager for this realm */ public DataCellManager getDataCellManager(){ return this.dataCellManager; } /** * Sets the entity data cell mapper for this realm * @param entityDataCellMapper The entity data cell mapper for this realm */ protected void setEntityDataCellMapper(EntityDataCellMapper entityDataCellMapper){ this.entityDataCellMapper = entityDataCellMapper; } /** * Sets the data cell manager for this realm * @param dataCellManager The data cell manager for this realm */ protected void setDataCellManager(DataCellManager dataCellManager){ this.dataCellManager = dataCellManager; } /** * Gets the collision engine for physics collision checking in this realm * @return The collision engine */ public CollisionEngine getCollisionEngine(){ return this.collisionEngine; } /** * Gets the hitbox manager backing this realm * @return The hitbox manager */ public HitboxManager getHitboxManager(){ return this.hitboxManager; } /** * Tells the data cell manager to simulate all loaded cells */ protected void simulate(){ //simulate bullet physics engine step collisionEngine.simulatePhysics((float)Globals.timekeeper.getSimFrameTime()); collisionEngine.updateDynamicObjectTransforms(); //main simulation dataCellManager.simulate(); //data cell manager update misc variables (player positions, unload not-in-use cells) if(dataCellManager != null){ dataCellManager.unloadPlayerlessChunks(); } //clear collidable impulse lists collisionEngine.clearCollidableImpulseLists(); } }