public void Load(Main m, Ovr.Eye eyeType, Ovr.FovPort fov) { this.main = m; this.fov = fov; this.eye = eyeType; this.Reload(); }
public Main(int monitor) { #endif Factory<Main>.Initialize(); Voxel.States.Init(); Editor.SetupDefaultEditorComponents(); #if STEAMWORKS if (!SteamWorker.Init()) Log.d("Failed to initialize Steamworks."); #if VR if (SteamWorker.Initialized && Steamworks.SteamUtils.IsSteamRunningInVR()) this.VR = vr = true; #endif #endif #if VR if (this.VR) { if (!Ovr.Hmd.Initialize(new Ovr.InitParams())) throw new Exception("Failed to initialize Oculus runtime."); this.VRHmd = new Ovr.Hmd(0); if (this.VRHmd == null) { Log.d("Error: no Oculus found."); this.VR = false; this.oculusNotFound = true; } else { if (!this.VRHmd.ConfigureTracking( (uint)Ovr.TrackingCaps.Orientation | (uint)Ovr.TrackingCaps.MagYawCorrection | (uint)Ovr.TrackingCaps.Position, 0)) throw new Exception("Failed to configure head tracking."); this.vrHmdDesc = this.VRHmd.GetDesc(); this.vrLeftFov = this.vrHmdDesc.MaxEyeFov[0]; this.vrRightFov = this.vrHmdDesc.MaxEyeFov[1]; Ovr.FovPort maxFov = new Ovr.FovPort(); maxFov.UpTan = Math.Max(this.vrLeftFov.UpTan, this.vrRightFov.UpTan); maxFov.DownTan = Math.Max(this.vrLeftFov.DownTan, this.vrRightFov.DownTan); maxFov.LeftTan = Math.Max(this.vrLeftFov.LeftTan, this.vrRightFov.LeftTan); maxFov.RightTan = Math.Max(this.vrLeftFov.RightTan, this.vrRightFov.RightTan); float combinedTanHalfFovHorizontal = Math.Max(maxFov.LeftTan, maxFov.RightTan); float combinedTanHalfFovVertical = Math.Max(maxFov.UpTan, maxFov.DownTan); this.vrLeftEyeRenderDesc = this.VRHmd.GetRenderDesc(Ovr.Eye.Left, this.vrLeftFov); this.vrRightEyeRenderDesc = this.VRHmd.GetRenderDesc(Ovr.Eye.Right, this.vrRightFov); } } #endif this.Space = new Space(); this.Space.TimeStepSettings.TimeStepDuration = 1.0f / (float)maxPhysicsFramerate; this.ScreenSize.Value = new Point(this.Window.ClientBounds.Width, this.Window.ClientBounds.Height); // Give the space some threads to work with. // Just throw a thread at every processor. The thread scheduler will take care of where to put them. for (int i = 0; i < Environment.ProcessorCount - 1; i++) this.Space.ThreadManager.AddThread(); this.Space.ForceUpdater.Gravity = new Vector3(0, -18.0f, 0); this.IsFixedTimeStep = false; this.Window.AllowUserResizing = true; this.Window.ClientSizeChanged += new EventHandler<EventArgs>(delegate(object obj, EventArgs e) { if (!this.Settings.Fullscreen) { Rectangle bounds = this.Window.ClientBounds; this.ScreenSize.Value = new Point(bounds.Width, bounds.Height); this.resize = new Point(bounds.Width, bounds.Height); } }); this.Graphics = new GraphicsDeviceManager(this); this.Content = new ContentManager(this.Services); this.Content.RootDirectory = "Content"; this.Entities = new ListProperty<Entity>(); this.Camera = new Camera(); this.AddComponent(this.Camera); Lemma.Console.Console.AddConVar(new ConVar("player_speed", "Player speed.", s => { Entity playerData = PlayerDataFactory.Instance; if (playerData != null) playerData.Get<PlayerData>().MaxSpeed.Value = (float)Lemma.Console.Console.GetConVar("player_speed").GetCastedValue(); }, "10") { TypeConstraint = typeof(float), Validate = o => (float)o > 0 && (float)o < 200 }); Lemma.Console.Console.AddConCommand(new ConCommand("help", "List all commands or get info about a specific command.", delegate(ConCommand.ArgCollection args) { string cmd = (string)args.Get("command"); if (string.IsNullOrEmpty(cmd)) Lemma.Console.Console.Instance.ListAllConsoleStuff(); else Lemma.Console.Console.Instance.PrintConCommandDescription(cmd); }, new ConCommand.CommandArgument() { Name = "command", Optional = true })); Lemma.Console.Console.AddConVar(new ConVar("time_scale", "Time scale (percentage).", s => { float result; if (float.TryParse(s, out result)) this.BaseTimeMultiplier.Value = result / 100.0f; }, "100") { TypeConstraint = typeof(int), Validate = o => (int)o > 0 && (int)o <= 400 }); Lemma.Console.Console.AddConCommand(new ConCommand ( "load", "Load a map.", collection => { ConsoleUI.Showing.Value = false; this.Menu.Toggle(); IO.MapLoader.Transition(this, collection.ParsedArgs[0].StrValue); }, new ConCommand.CommandArgument { Name = "map", CommandType = typeof(string), Optional = false } )); Lemma.Console.Console.AddConVar(new ConVar("blocks", "Player block cheat (white, yellow, blue)", s => { Entity player = PlayerFactory.Instance; if (player != null && player.Active) { Voxel.t result = Voxel.t.Empty; switch (s.ToLower()) { case "white": result = Voxel.t.WhitePermanent; break; case "yellow": result = Voxel.t.GlowYellow; break; case "blue": result = Voxel.t.GlowBlue; break; default: break; } BlockCloud cloud = player.Get<BlockCloud>(); cloud.Blocks.Clear(); cloud.Type.Value = result; } }, "none")); Lemma.Console.Console.AddConCommand(new ConCommand("moves", "Enable all parkour moves.", delegate(ConCommand.ArgCollection args) { if (PlayerDataFactory.Instance != null) { PlayerData playerData = PlayerDataFactory.Instance.Get<PlayerData>(); playerData.EnableRoll.Value = true; playerData.EnableKick.Value = true; playerData.EnableWallRun.Value = true; playerData.EnableWallRunHorizontal.Value = true; playerData.EnableMoves.Value = true; playerData.EnableCrouch.Value = true; playerData.EnableSlowMotion.Value = true; } })); #if DEVELOPMENT Lemma.Console.Console.AddConCommand(new ConCommand("diavar", "Set a dialogue variable.", delegate(ConCommand.ArgCollection args) { if (args.ParsedArgs.Length == 2) { if (PlayerDataFactory.Instance != null) { Phone phone = PlayerDataFactory.Instance.Get<Phone>(); phone[args.ParsedArgs[0].StrValue] = args.ParsedArgs[1].StrValue; } } }, new ConCommand.CommandArgument { Name = "variable", CommandType = typeof(string), Optional = false, }, new ConCommand.CommandArgument { Name = "value", CommandType = typeof(string), Optional = false } )); #endif Lemma.Console.Console.AddConCommand(new ConCommand("specials", "Enable all special abilities.", delegate(ConCommand.ArgCollection args) { if (PlayerDataFactory.Instance != null) { PlayerData playerData = PlayerDataFactory.Instance.Get<PlayerData>(); playerData.EnableEnhancedWallRun.Value = true; } })); new SetBinding<float>(this.PauseAudioEffect, delegate(float value) { AkSoundEngine.SetRTPCValue(AK.GAME_PARAMETERS.PAUSE_PARAMETER, MathHelper.Clamp(value, 0.0f, 1.0f)); }); new CommandBinding(this.MapLoaded, delegate() { this.mapLoaded = true; }); #if DEVELOPMENT this.EditorEnabled.Value = true; #else this.EditorEnabled.Value = false; #endif if (!Directory.Exists(Main.DataDirectory)) Directory.CreateDirectory(Main.DataDirectory); this.settingsFile = Path.Combine(Main.DataDirectory, "settings.json"); this.SaveDirectory = Path.Combine(Main.DataDirectory, "saves"); if (!Directory.Exists(this.SaveDirectory)) Directory.CreateDirectory(this.SaveDirectory); this.analyticsDirectory = Path.Combine(Main.DataDirectory, "analytics"); if (!Directory.Exists(this.analyticsDirectory)) Directory.CreateDirectory(this.analyticsDirectory); this.CustomMapDirectory = Path.Combine(Main.DataDirectory, "maps"); if (!Directory.Exists(this.CustomMapDirectory)) Directory.CreateDirectory(this.CustomMapDirectory); this.MapDirectory = Path.Combine(this.Content.RootDirectory, IO.MapLoader.MapDirectory); this.timesFile = Path.Combine(Main.DataDirectory, "times.dat"); try { using (Stream fs = new FileStream(this.timesFile, FileMode.Open, FileAccess.Read, FileShare.None)) using (Stream stream = new GZipInputStream(fs)) using (StreamReader reader = new StreamReader(stream)) this.times = JsonConvert.DeserializeObject<Dictionary<string, float>>(reader.ReadToEnd()); } catch (Exception) { } if (this.times == null) this.times = new Dictionary<string, float>(); try { // Attempt to load previous window state this.Settings = JsonConvert.DeserializeObject<Config>(File.ReadAllText(this.settingsFile)); if (this.Settings.Version != Main.ConfigVersion) throw new Exception(); } catch (Exception) // File doesn't exist, there was a deserialization error, or we are on a new version. Use default window settings { this.Settings = new Config(); } if (string.IsNullOrEmpty(this.Settings.UUID)) this.Settings.UUID = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 32); this.Settings.MinimizeCameraMovementVR.Value |= this.Settings.MinimizeCameraMovement; this.MinimizeCameraMovement = this.VR ? this.Settings.MinimizeCameraMovementVR : this.Settings.MinimizeCameraMovement; this.Settings.GodModeProperty.Value = this.Settings.GodMode; this.Settings.LevelIndexProperty.Value = this.Settings.LevelIndex; new NotifyBinding(delegate() { this.Settings.GodMode = this.Settings.GodModeProperty; }, this.Settings.GodModeProperty); new NotifyBinding(delegate() { this.Settings.LevelIndex = this.Settings.LevelIndexProperty; }, this.Settings.LevelIndexProperty); TextElement.BindableProperties.Add("Forward", this.Settings.Forward); TextElement.BindableProperties.Add("Left", this.Settings.Left); TextElement.BindableProperties.Add("Backward", this.Settings.Backward); TextElement.BindableProperties.Add("Right", this.Settings.Right); TextElement.BindableProperties.Add("Jump", this.Settings.Jump); TextElement.BindableProperties.Add("Parkour", this.Settings.Parkour); TextElement.BindableProperties.Add("RollKick", this.Settings.RollKick); TextElement.BindableProperties.Add("TogglePhone", this.Settings.TogglePhone); TextElement.BindableProperties.Add("QuickSave", this.Settings.QuickSave); TextElement.BindableProperties.Add("ToggleFullscreen", this.Settings.ToggleFullscreen); TextElement.BindableProperties.Add("RecenterVRPose", this.Settings.RecenterVRPose); TextElement.BindableProperties.Add("ToggleConsole", this.Settings.ToggleConsole); new NotifyBinding ( this.updateTimesteps, this.BaseTimeMultiplier, this.TimeMultiplier, this.Settings.FPSLimit ); this.updateTimesteps(); if (this.Settings.FullscreenResolution.Value.X == 0) this.Settings.FullscreenResolution.Value = new Point(this.nativeDisplayMode.Width, this.nativeDisplayMode.Height); // Have to create the menu here so it can catch the PreparingDeviceSettings event // We call AddComponent(this.Menu) later on in LoadContent. this.Menu = new Menu(); this.Graphics.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs args) { args.GraphicsDeviceInformation.Adapter = GraphicsAdapter.Adapters[this.monitor]; args.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.Immediate; List<DisplayMode> supportedDisplayModes = args.GraphicsDeviceInformation.Adapter.SupportedDisplayModes.Where(x => x.Width <= 4096 && x.Height <= 4096).ToList(); int displayModeIndex = 0; foreach (DisplayMode mode in supportedDisplayModes) { if (mode.Format == SurfaceFormat.Color && mode.Width == this.Settings.FullscreenResolution.Value.X && mode.Height == this.Settings.FullscreenResolution.Value.Y) break; displayModeIndex++; } this.Menu.SetupDisplayModes(supportedDisplayModes, displayModeIndex); }; this.Screenshot = new Screenshot(); this.AddComponent(this.Screenshot); this.Graphics.SynchronizeWithVerticalRetrace = this.Settings.Vsync; new NotifyBinding(delegate() { this.Graphics.SynchronizeWithVerticalRetrace = this.Settings.Vsync; if (this.Settings.Fullscreen) this.Graphics.ApplyChanges(); }, this.Settings.Vsync); #if VR if (this.VR) this.ResizeViewport(this.nativeDisplayMode.Width, this.nativeDisplayMode.Height, true, false, false); else #endif if (this.Settings.Fullscreen) this.ResizeViewport(this.Settings.FullscreenResolution.Value.X, this.Settings.FullscreenResolution.Value.Y, true, this.Settings.Borderless, false); else this.ResizeViewport(this.Settings.Size.Value.X, this.Settings.Size.Value.Y, false, this.Settings.Borderless, false); }