Renderer/src/main/java/electrosphere/engine/loadingthreads/ClientLoading.java
austin 7ab6cf96d9
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good
Audio engine overhaul
2024-03-09 15:54:47 -05:00

312 lines
12 KiB
Java

package electrosphere.engine.loadingthreads;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.joml.Quaterniond;
import org.joml.Vector3d;
import org.joml.Vector3f;
import electrosphere.audio.AudioUtils;
import electrosphere.audio.VirtualAudioSource;
import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType;
import electrosphere.client.culling.ClientEntityCullingManager;
import electrosphere.client.foliagemanager.ClientFoliageManager;
import electrosphere.client.sim.ClientSimulation;
import electrosphere.client.targeting.crosshair.Crosshair;
import electrosphere.client.terrain.cells.DrawCellManager;
import electrosphere.collision.CollisionEngine;
import electrosphere.controls.ControlHandler;
import electrosphere.engine.Globals;
import electrosphere.entity.ClientEntityUtils;
import electrosphere.entity.DrawableUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityCreationUtils;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.BehaviorTree;
import electrosphere.entity.state.movement.ApplyRotationTree;
import electrosphere.entity.types.camera.CameraEntityUtils;
import electrosphere.entity.types.tree.ProceduralTree;
import electrosphere.logger.LoggerInterface;
import electrosphere.menu.MenuGenerators;
import electrosphere.menu.MenuGeneratorsMultiplayer;
import electrosphere.menu.WindowStrings;
import electrosphere.menu.WindowUtils;
import electrosphere.net.NetUtils;
import electrosphere.net.client.ClientNetworking;
import electrosphere.renderer.ui.Window;
import electrosphere.server.datacell.EntityDataCellMapper;
public class ClientLoading {
protected static void loadMainMenu(){
Window loadingWindow = (Window)Globals.elementManager.getWindow(WindowStrings.WINDOW_LOADING);
WindowUtils.recursiveSetVisible(loadingWindow,false);
WindowUtils.focusWindow(WindowStrings.WINDOW_MENU_MAIN);
WindowUtils.recursiveSetVisible(Globals.elementManager.getWindow(WindowStrings.WINDOW_MENU_MAIN), true);
}
protected static void loadCharacterServer(){
Window loadingWindow = (Window)Globals.elementManager.getWindow(WindowStrings.WINDOW_LOADING);
WindowUtils.recursiveSetVisible(Globals.elementManager.getWindow(WindowStrings.WINDOW_MENU_MAIN), false);
WindowUtils.replaceMainMenuContents(MenuGenerators.createEmptyMainMenu());
loadingWindow.setVisible(true);
//disable menu input
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.NO_INPUT);
//initialize the client thread (client)
initClientThread();
//while we don't know what races are playable, wait
while(Globals.gameConfigCurrent.getCreatureTypeLoader().getPlayableRaces().size() == 0){
try {
TimeUnit.MILLISECONDS.sleep(5);
} catch (InterruptedException ex) {}
}
//once we have them, bring up the character creation interface
//init character creation window
//eventually should replace with at ui to select an already created character or create a new one
WindowUtils.replaceMainMenuContents(MenuGeneratorsMultiplayer.createMultiplayerCharacterCreationWindow());
//make loading dialog disappear
loadingWindow.setVisible(false);
//make character creation window visible
WindowUtils.recursiveSetVisible(Globals.elementManager.getWindow(WindowStrings.WINDOW_MENU_MAIN), true);
//recapture window
Globals.controlHandler.setRecapture(true);
//log
LoggerInterface.loggerEngine.INFO("[Client]Finished loading character creation menu");
//set menu controls again
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.TITLE_MENU);
}
protected static void loadClientWorld(){
Window loadingWindow = (Window)Globals.elementManager.getWindow(WindowStrings.WINDOW_LOADING);
WindowUtils.recursiveSetVisible(Globals.elementManager.getWindow(WindowStrings.WINDOW_MENU_MAIN), false);
WindowUtils.replaceMainMenuContents(MenuGenerators.createEmptyMainMenu());
loadingWindow.setVisible(true);
//disable menu input
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.NO_INPUT);
//initialize the "real" objects simulation
initClientSimulation();
//init foliage manager
initFoliageManager();
//initialize the cell manager (client)
initDrawCellManager();
//initialize the basic graphical entities of the world (skybox, camera)
initWorldBaseGraphicalEntities();
//init arena specific stuff (ie different skybox colors)
initArenaGraphicalEntities();
//sets micro and macro sims to ready if they exist
setSimulationsToReady();
//init culling manager and other graphics-focused non-simulation items
initEntityCullingManager();
//hide cursor
Globals.controlHandler.hideMouse();
//make loading window disappear
loadingWindow.setVisible(false);
//recapture screen
Globals.controlHandler.setRecapture(true);
//set rendering flags to main game mode
Globals.RENDER_FLAG_RENDER_SHADOW_MAP = true;
Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT = true;
Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER = true;
Globals.RENDER_FLAG_RENDER_UI = true;
Globals.RENDER_FLAG_RENDER_BLACK_BACKGROUND = false;
Globals.RENDER_FLAG_RENDER_WHITE_BACKGROUND = false;
LoggerInterface.loggerEngine.INFO("[Client]Finished loading main game");
//set controls state
Globals.controlHandler.setHandlerState(ControlHandler.ControlsState.MAIN_GAME);
}
/**
* Inits the client networking thread and socket
*/
private static void initClientThread(){
//start client networking
Thread clientThread = null;
if(Globals.RUN_CLIENT){
Globals.clientConnection = new ClientNetworking(NetUtils.getAddress(),NetUtils.getPort());
clientThread = new Thread(Globals.clientConnection);
clientThread.start();
}
}
/**
* Creates client simulation object
*/
private static void initClientSimulation(){
if(Globals.clientSimulation == null){
Globals.clientSimulation = new ClientSimulation();
}
}
/**
* Sets client simulation object state to ready
*/
private static void setSimulationsToReady(){
Globals.clientSimulation.setReady(true);
}
private static void initWorldBaseGraphicalEntities(){
/*
Skybox
*/
// Model skyboxModel = Globals.assetManager.fetchModel(AssetDataStrings.ASSET_STRING_SKYBOX_BASIC);
// Globals.skybox = EntityUtils.spawnDrawableEntity(AssetDataStrings.ASSET_STRING_SKYBOX_BASIC);
/*
Player Camera
*/
Globals.playerCamera = CameraEntityUtils.spawnPlayerEntityTrackingCameraEntity(new Vector3f(1,0,1), new Vector3f(0,0,1));
// Globals.playerCamera = CameraEntityUtils.spawnPlayerEntityAirplaneTrackingCameraEntity(new Vector3f(1,0,1), new Vector3f(0,0,1));
/*
Targeting crosshair
*/
Crosshair.initCrossHairEntity();
}
static void initArenaGraphicalEntities(){
float skyR = 150;
float skyG = 200;
float skyB = 250;
float groundR = 20;
float groundG = 20;
float groundB = 20;
Globals.skyboxColors.add(new Vector3f(skyR,skyG,skyB));
Globals.skyboxColors.add(new Vector3f(skyR,skyG,skyB));
Globals.skyboxColors.add(new Vector3f(groundR,groundG,groundB));
Globals.skyboxColors.add(new Vector3f(groundR,groundG,groundB));
Globals.skyboxColors.add(new Vector3f(skyR,skyG,skyB));
Globals.skyboxColors.add(new Vector3f(skyR,skyG,skyB));
Globals.skyboxColors.add(new Vector3f(groundR,groundG,groundB));
Globals.skyboxColors.add(new Vector3f(groundR,groundG,groundB));
//starry sky true skybox
Entity skybox = EntityCreationUtils.createClientSpatialEntity();
EntityCreationUtils.makeEntityDrawable(skybox, "Models/skyboxSphere.fbx");
EntityUtils.getRotation(skybox).rotateX((float)(-Math.PI/2.0f));
EntityUtils.getScale(skybox).mul(2000.0f);
Globals.assetManager.queueOverrideMeshShader("Models/skyboxSphere.fbx", "Sphere", "Shaders/skysphere/skysphere.vs", "Shaders/skysphere/skysphere.fs");
//cloud ring pseudo skybox
Entity cloudRing = EntityCreationUtils.createClientSpatialEntity();
EntityCreationUtils.makeEntityDrawable(cloudRing, "Models/cloudRing.fbx");
EntityUtils.getRotation(cloudRing).rotateX((float)(-Math.PI/2.0f));
EntityUtils.getScale(cloudRing).mul(1000.0f);
Globals.clientScene.registerBehaviorTree(new ApplyRotationTree(cloudRing,new Quaterniond().rotationZ(0.0001)));
Globals.assetManager.queueOverrideMeshShader("Models/cloudRing.fbx", "Sphere", "Shaders/skysphere/skysphere.vs", "Shaders/skysphere/skysphere.fs");
Entity cursorTracker = EntityCreationUtils.createClientSpatialEntity();
EntityCreationUtils.makeEntityDrawable(cursorTracker, "Models/unitsphere_1.fbx");
EntityUtils.getScale(cursorTracker).set(0.3f);
Globals.clientSceneWrapper.getScene().registerBehaviorTree(new BehaviorTree() {
@Override
public void simulate(float deltaTime) {
CollisionEngine collisionEngine = Globals.clientSceneWrapper.getCollisionEngine();
Entity camera = Globals.playerCamera;
if(
collisionEngine != null &&
camera != null
){
Vector3d eyePos = new Vector3d(CameraEntityUtils.getCameraEye(camera));
Vector3d centerPos = new Vector3d(CameraEntityUtils.getCameraCenter(camera));
Vector3d cursorPos = collisionEngine.rayCastPosition(centerPos, new Vector3d(eyePos).mul(-1.0), 5.0);
if(cursorPos != null){
EntityUtils.getPosition(cursorTracker).set(cursorPos);
} else {
EntityUtils.getPosition(cursorTracker).set(new Vector3d(centerPos).add(new Vector3d(eyePos).normalize().mul(-5.0)));
}
}
}
});
}
static void initDrawCellManager(){
while(Globals.clientWorldData == null){
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException ex) {
}
}
//initialize draw cell manager
Globals.drawCellManager = new DrawCellManager(Globals.clientTerrainManager, 0, 0, 0);
//set our draw cell manager to actually generate drawable chunks
Globals.drawCellManager.setGenerateDrawables(true);
//Alerts the client simulation that it should start loading terrain
Globals.clientSimulation.setLoadingTerrain(true);
//wait for all the terrain data to arrive
while(Globals.drawCellManager.containsUnrequestedCell()){
// Globals.drawCellManager.updateInvalidCell();
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// System.out.println("invalid cell");
}
while(Globals.drawCellManager.containsUndrawableCell()){
// Globals.drawCellManager.makeCellDrawable();
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// System.out.println("undrawable");
}
// while(Globals.drawCellManager.containsPhysicsNeedingCell()){
// try {
// TimeUnit.MILLISECONDS.sleep(10);
// } catch (InterruptedException ex) {
// }
// }
// System.out.println("Draw Cell Manager ready");
}
/**
* Starts up the entity culling manager
*/
private static void initEntityCullingManager(){
Globals.clientEntityCullingManager = new ClientEntityCullingManager(Globals.clientScene);
}
/**
* Starts up the foliage manager
*/
private static void initFoliageManager(){
Globals.clientFoliageManager = new ClientFoliageManager();
Globals.clientFoliageManager.start();
}
}