diff --git a/Subsurface/Barotrauma.csproj b/Subsurface/Barotrauma.csproj index 0728f2e10..b3a31c6df 100644 --- a/Subsurface/Barotrauma.csproj +++ b/Subsurface/Barotrauma.csproj @@ -168,6 +168,7 @@ + diff --git a/Subsurface/Source/Camera.cs b/Subsurface/Source/Camera.cs index ba04ff939..bd0e83628 100644 --- a/Subsurface/Source/Camera.cs +++ b/Subsurface/Source/Camera.cs @@ -127,9 +127,9 @@ namespace Barotrauma public void UpdateTransform(bool interpolate = true, bool clampPos = false) { - Vector2 interpolatedPosition = interpolate ? Physics.Interpolate(prevPosition, position) : position; + Vector2 interpolatedPosition = interpolate ? Timing.Interpolate(prevPosition, position) : position; - float interpolatedZoom = interpolate ? Physics.Interpolate(prevZoom, zoom) : zoom; + float interpolatedZoom = interpolate ? Timing.Interpolate(prevZoom, zoom) : zoom; worldView.X = (int)(interpolatedPosition.X - worldView.Width / 2.0); worldView.Y = (int)(interpolatedPosition.Y + worldView.Height / 2.0); diff --git a/Subsurface/Source/GUI/LoadingScreen.cs b/Subsurface/Source/GUI/LoadingScreen.cs index cc42b4e91..77ac5faec 100644 --- a/Subsurface/Source/GUI/LoadingScreen.cs +++ b/Subsurface/Source/GUI/LoadingScreen.cs @@ -127,9 +127,13 @@ namespace Barotrauma } } - spriteBatch.DrawString(GUI.LargeFont, loadText, - new Vector2(GameMain.GraphicsWidth/2.0f - GUI.LargeFont.MeasureString(loadText).X/2.0f, GameMain.GraphicsHeight*0.8f), - Color.White); + if (GUI.LargeFont!=null) + { + spriteBatch.DrawString(GUI.LargeFont, loadText, + new Vector2(GameMain.GraphicsWidth/2.0f - GUI.LargeFont.MeasureString(loadText).X/2.0f, GameMain.GraphicsHeight*0.8f), + Color.White); + } + } spriteBatch.End(); diff --git a/Subsurface/Source/GameMain.cs b/Subsurface/Source/GameMain.cs index 28b995303..d11b0f9a2 100644 --- a/Subsurface/Source/GameMain.cs +++ b/Subsurface/Source/GameMain.cs @@ -72,7 +72,6 @@ namespace Barotrauma private bool hasLoaded; private GameTime fixedTime; - private double updatesToMake; //public static Random localRandom; //public static Random random; @@ -142,7 +141,7 @@ namespace Barotrauma IsFixedTimeStep = false; //TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55); - updatesToMake = 0.0; + Timing.Accumulator = 0.0f; fixedTime = new GameTime(); World = new World(new Vector2(0, -9.82f)); @@ -291,51 +290,59 @@ namespace Barotrauma /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { - double realDeltaTime = gameTime.ElapsedGameTime.TotalSeconds; - double deltaTime = 0.016; - updatesToMake += realDeltaTime; + Timing.Accumulator += gameTime.ElapsedGameTime.TotalSeconds; - while (updatesToMake > 0.0) + while (Timing.Accumulator >= Timing.Step) { fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly; TimeSpan addTime = new TimeSpan(0, 0, 0, 0, 16); fixedTime.ElapsedGameTime = addTime; fixedTime.TotalGameTime.Add(addTime); base.Update(fixedTime); - - PlayerInput.Update(deltaTime); + + PlayerInput.Update(Timing.Step); bool paused = false; - if (hasLoaded && !titleScreenOpen) + if (titleScreenOpen) + { + if (TitleScreen.LoadState >= 100.0f && + (!waitForKeyHit || PlayerInput.GetKeyboardState.GetPressedKeys().Length>0 || PlayerInput.LeftButtonClicked())) + { + titleScreenOpen = false; + } + } + else if (hasLoaded) { SoundPlayer.Update(); if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu(); - DebugConsole.Update(this, (float)deltaTime); + DebugConsole.Update(this, (float)Timing.Step); paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen || GUI.SettingsMenuOpen) && (NetworkMember == null || !NetworkMember.GameStarted); - - if (!paused) Screen.Selected.Update(deltaTime); + + if (!paused) + { + Screen.Selected.Update(Timing.Step); + } if (NetworkMember != null) { - NetworkMember.Update((float)deltaTime); - } - else - { - NetworkEvent.Events.Clear(); + NetworkMember.Update((float)Timing.Step); } - GUI.Update((float)deltaTime); + GUI.Update((float)Timing.Step); } + - CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime); + CoroutineManager.Update((float)Timing.Step, paused ? 0.0f : (float)Timing.Step); - updatesToMake -= deltaTime; + Timing.Accumulator -= Timing.Step; } + + Timing.Alpha = Timing.Accumulator / Timing.Step; } @@ -344,8 +351,6 @@ namespace Barotrauma /// protected override void Draw(GameTime gameTime) { - //renderTimer.Restart(); - double deltaTime = gameTime.ElapsedGameTime.TotalSeconds; FrameCounter.Update(deltaTime); @@ -353,22 +358,11 @@ namespace Barotrauma if (titleScreenOpen) { TitleScreen.Draw(spriteBatch, GraphicsDevice, (float)deltaTime); - if (TitleScreen.LoadState>=100.0f && - (!waitForKeyHit || PlayerInput.GetKeyboardState.GetPressedKeys().Length>0 || PlayerInput.LeftButtonClicked())) - { - titleScreenOpen = false; - } } else if (hasLoaded) { Screen.Selected.Draw(deltaTime, GraphicsDevice, spriteBatch); } - - //double elapsed = sw.Elapsed.TotalSeconds; - //if (elapsed < Physics.step) - //{ - // System.Threading.Thread.Sleep((int)((Physics.step - elapsed) * 1000.0)); - //} } static bool waitForKeyHit = true; diff --git a/Subsurface/Source/Items/Components/Machines/Deconstructor.cs b/Subsurface/Source/Items/Components/Machines/Deconstructor.cs index c8dc22eab..4a139dfd0 100644 --- a/Subsurface/Source/Items/Components/Machines/Deconstructor.cs +++ b/Subsurface/Source/Items/Components/Machines/Deconstructor.cs @@ -87,7 +87,7 @@ namespace Barotrauma.Items.Components public override void UpdateHUD(Character character) { - GuiFrame.Update((float)Physics.step); + GuiFrame.Update((float)Timing.Step); } private bool ToggleActive(GUIButton button, object obj) diff --git a/Subsurface/Source/Items/Components/Machines/Fabricator.cs b/Subsurface/Source/Items/Components/Machines/Fabricator.cs index 1f690c678..d10a6c833 100644 --- a/Subsurface/Source/Items/Components/Machines/Fabricator.cs +++ b/Subsurface/Source/Items/Components/Machines/Fabricator.cs @@ -350,7 +350,7 @@ namespace Barotrauma.Items.Components activateButton.Enabled = CanBeFabricated(targetItem, character); } - GuiFrame.Update((float)Physics.step); + GuiFrame.Update((float)Timing.Step); } private bool CanBeFabricated(FabricableItem fabricableItem, Character user) diff --git a/Subsurface/Source/Items/Components/Turret.cs b/Subsurface/Source/Items/Components/Turret.cs index e017c9b99..d0c77b0cf 100644 --- a/Subsurface/Source/Items/Components/Turret.cs +++ b/Subsurface/Source/Items/Components/Turret.cs @@ -366,7 +366,7 @@ namespace Barotrauma.Items.Components break; case "trigger_in": - item.Use((float)Physics.step, null); + item.Use((float)Timing.Step, null); break; } } diff --git a/Subsurface/Source/Items/FixRequirement.cs b/Subsurface/Source/Items/FixRequirement.cs index 70d5644f6..804da105d 100644 --- a/Subsurface/Source/Items/FixRequirement.cs +++ b/Subsurface/Source/Items/FixRequirement.cs @@ -202,7 +202,7 @@ namespace Barotrauma if (frame == null) return; - frame.Update((float)Physics.step); + frame.Update((float)Timing.Step); } } } diff --git a/Subsurface/Source/Items/Item.cs b/Subsurface/Source/Items/Item.cs index 440d69459..b8a89263b 100644 --- a/Subsurface/Source/Items/Item.cs +++ b/Subsurface/Source/Items/Item.cs @@ -923,7 +923,7 @@ namespace Barotrauma } editingHUD.Draw(spriteBatch); - editingHUD.Update((float)Physics.step); + editingHUD.Update((float)Timing.Step); if (!prefab.IsLinkable) return; @@ -951,7 +951,7 @@ namespace Barotrauma if (editingHUD.Rect.Height > 60) { - editingHUD.Update((float)Physics.step); + editingHUD.Update((float)Timing.Step); editingHUD.Draw(spriteBatch); } } diff --git a/Subsurface/Source/Map/LinkedSubmarine.cs b/Subsurface/Source/Map/LinkedSubmarine.cs index aef2d6ccf..ba292d6fe 100644 --- a/Subsurface/Source/Map/LinkedSubmarine.cs +++ b/Subsurface/Source/Map/LinkedSubmarine.cs @@ -156,8 +156,8 @@ namespace Barotrauma editingHUD = CreateEditingHUD(); } + editingHUD.Update(0.016f); editingHUD.Draw(spriteBatch); - editingHUD.Update((float)Physics.step); if (!PlayerInput.LeftButtonClicked() || !PlayerInput.KeyDown(Keys.Space)) return; diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index 343bd3f3d..42ed37efd 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -323,7 +323,7 @@ namespace Barotrauma public void UpdateTransform() { - DrawPosition = Physics.Interpolate(prevPosition, Position); + DrawPosition = Timing.Interpolate(prevPosition, Position); } //math/physics stuff ---------------------------------------------------- diff --git a/Subsurface/Source/Map/WayPoint.cs b/Subsurface/Source/Map/WayPoint.cs index 22e4c29cf..05c14b5a2 100644 --- a/Subsurface/Source/Map/WayPoint.cs +++ b/Subsurface/Source/Map/WayPoint.cs @@ -178,7 +178,7 @@ namespace Barotrauma editingHUD = CreateEditingHUD(); } - editingHUD.Update((float)Physics.step); + editingHUD.Update((float)Timing.Step); editingHUD.Draw(spriteBatch); if (!PlayerInput.LeftButtonClicked()) return; diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 80cd77a4d..bcb6ba06f 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -304,8 +304,6 @@ namespace Barotrauma.Networking if (gameStarted) { - //inGameHUD.Update((float)Physics.step); - if (respawnManager != null) respawnManager.Update(deltaTime); bool isCrewDead = diff --git a/Subsurface/Source/Particles/Particle.cs b/Subsurface/Source/Particles/Particle.cs index 3de83ae90..d2c65e0f8 100644 --- a/Subsurface/Source/Particles/Particle.cs +++ b/Subsurface/Source/Particles/Particle.cs @@ -301,8 +301,8 @@ namespace Barotrauma.Particles public void UpdateDrawPos() { - drawPosition = Physics.Interpolate(prevPosition, position); - drawRotation = Physics.Interpolate(prevRotation, rotation); + drawPosition = Timing.Interpolate(prevPosition, position); + drawRotation = Timing.Interpolate(prevRotation, rotation); prevPosition = position; prevRotation = rotation; diff --git a/Subsurface/Source/Physics/Physics.cs b/Subsurface/Source/Physics/Physics.cs index 0ef6d42db..69fa6f870 100644 --- a/Subsurface/Source/Physics/Physics.cs +++ b/Subsurface/Source/Physics/Physics.cs @@ -20,33 +20,6 @@ namespace Barotrauma public static float DisplayToRealWorldRatio = 1.0f / 80.0f; - public static double accumulator; - public static double step = 1.0/60.0; - - public const float DisplayToSimRation = 100.0f; - - public static double Alpha - { - get { return alpha; } - set { alpha = Math.Min(Math.Max(value, 0.0), 1.0); } - } - - public static double Interpolate(double previous, double current) - { - return current * alpha + previous * (1.0 - alpha); - } - - public static float Interpolate(float previous, float current) - { - return current * (float)alpha + previous * (1.0f - (float)alpha); - } - - public static Vector2 Interpolate(Vector2 previous, Vector2 current) - { - return new Vector2( - Interpolate(previous.X, current.X), - Interpolate(previous.Y, current.Y)); - } + public const float DisplayToSimRation = 100.0f; } - } diff --git a/Subsurface/Source/Physics/PhysicsBody.cs b/Subsurface/Source/Physics/PhysicsBody.cs index 9769648e7..0029c26e6 100644 --- a/Subsurface/Source/Physics/PhysicsBody.cs +++ b/Subsurface/Source/Physics/PhysicsBody.cs @@ -330,10 +330,10 @@ namespace Barotrauma public void UpdateDrawPosition() { - drawPosition = Physics.Interpolate(prevPosition, body.Position) - offsetFromTargetPos; + drawPosition = Timing.Interpolate(prevPosition, body.Position) - offsetFromTargetPos; drawPosition = ConvertUnits.ToDisplayUnits(drawPosition); - drawRotation = Physics.Interpolate(prevRotation, body.Rotation); + drawRotation = Timing.Interpolate(prevRotation, body.Rotation); if (offsetFromTargetPos == Vector2.Zero) return; @@ -370,7 +370,7 @@ namespace Barotrauma /// public void SmoothRotate(float targetRotation, float force = 10.0f) { - float nextAngle = body.Rotation + body.AngularVelocity * (float)Physics.step; + float nextAngle = body.Rotation + body.AngularVelocity * (float)Timing.Step; float angle = MathUtils.GetShortestAngle(nextAngle, targetRotation); diff --git a/Subsurface/Source/Screens/EditCharacterScreen.cs b/Subsurface/Source/Screens/EditCharacterScreen.cs index a5af06f7b..706faa041 100644 --- a/Subsurface/Source/Screens/EditCharacterScreen.cs +++ b/Subsurface/Source/Screens/EditCharacterScreen.cs @@ -98,26 +98,15 @@ namespace Barotrauma /// Provides a snapshot of timing values. public override void Update(double deltaTime) { - Physics.accumulator += deltaTime; - //cam.Zoom += Math.Sign(PlayerInput.GetMouseState.ScrollWheelValue - PlayerInput.GetOldMouseState.ScrollWheelValue)*0.1f; - cam.MoveCamera((float)deltaTime); if (physicsEnabled) { - Physics.accumulator = Math.Min(Physics.accumulator, Physics.step * 4); - while (Physics.accumulator >= Physics.step) - { - Character.UpdateAnimAll((float)Physics.step * 1000.0f); + Character.UpdateAnimAll((float)deltaTime); - Ragdoll.UpdateAll(cam, (float)Physics.step); + Ragdoll.UpdateAll(cam, (float)deltaTime); - GameMain.World.Step((float)Physics.step); - - Physics.accumulator -= Physics.step; - } - - Physics.Alpha = Physics.accumulator / Physics.step; + GameMain.World.Step((float)deltaTime); } } diff --git a/Subsurface/Source/Screens/GameScreen.cs b/Subsurface/Source/Screens/GameScreen.cs index 577988788..59d8a257a 100644 --- a/Subsurface/Source/Screens/GameScreen.cs +++ b/Subsurface/Source/Screens/GameScreen.cs @@ -91,9 +91,6 @@ namespace Barotrauma /// Provides a snapshot of timing values. public override void Update(double deltaTime) { - //the accumulator code is based on this article: - //http://gafferongames.com/game-physics/fix-your-timestep/ - Physics.accumulator += deltaTime; #if DEBUG if (GameMain.GameSession != null && GameMain.GameSession.Level != null && GameMain.GameSession.Submarine != null) @@ -112,8 +109,6 @@ namespace Barotrauma if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime); - Character.UpdateAll(cam, (float)deltaTime); - if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null) { if (Character.Controlled.SelectedConstruction == Character.Controlled.ClosestItem) @@ -121,55 +116,42 @@ namespace Barotrauma Character.Controlled.SelectedConstruction.UpdateHUD(Character.Controlled); } } + Character.UpdateAll(cam, (float)deltaTime); + + BackgroundCreatureManager.Update(cam, (float)deltaTime); - Physics.accumulator = Math.Min(Physics.accumulator, Physics.step * 6); - //Physics.accumulator = Physics.step; - while (Physics.accumulator >= Physics.step) + GameMain.ParticleManager.Update((float)deltaTime); + + StatusEffect.UpdateAll((float)deltaTime); + + if (Character.Controlled != null && Lights.LightManager.ViewTarget != null) { - BackgroundCreatureManager.Update(cam, (float)Physics.step); - - GameMain.ParticleManager.Update((float)Physics.step); - - StatusEffect.UpdateAll((float)Physics.step); - - if (Character.Controlled != null && Lights.LightManager.ViewTarget != null) - { - cam.TargetPos = Lights.LightManager.ViewTarget.WorldPosition; - //Lights.LightManager.ViewPos = Character.Controlled.WorldPosition; - } - cam.MoveCamera((float)Physics.step); + cam.TargetPos = Lights.LightManager.ViewTarget.WorldPosition; + } + cam.MoveCamera((float)deltaTime); - foreach (Submarine sub in Submarine.Loaded) - { - sub.SetPrevTransform(sub.Position); - } - - foreach (PhysicsBody pb in PhysicsBody.list) - { - pb.SetPrevTransform(pb.SimPosition, pb.Rotation); - } - - MapEntity.UpdateAll(cam, (float)Physics.step); - - Character.UpdateAnimAll((float)Physics.step); - - Ragdoll.UpdateAll(cam, (float)Physics.step); - - foreach (Submarine sub in Submarine.Loaded) - { - sub.Update((float)Physics.step); - } - - GameMain.World.Step((float)Physics.step); - - //Level.AfterWorldStep(); - - Physics.accumulator -= Physics.step; + foreach (Submarine sub in Submarine.Loaded) + { + sub.SetPrevTransform(sub.Position); } + foreach (PhysicsBody pb in PhysicsBody.list) + { + pb.SetPrevTransform(pb.SimPosition, pb.Rotation); + } - Physics.Alpha = Physics.accumulator / Physics.step; + MapEntity.UpdateAll(cam, (float)deltaTime); + Character.UpdateAnimAll((float)deltaTime); + + Ragdoll.UpdateAll(cam, (float)deltaTime); + + foreach (Submarine sub in Submarine.Loaded) + { + sub.Update((float)deltaTime); + } + + GameMain.World.Step((float)deltaTime); } public override void Draw(double deltaTime, GraphicsDevice graphics, SpriteBatch spriteBatch) diff --git a/Subsurface/Source/Timing.cs b/Subsurface/Source/Timing.cs new file mode 100644 index 000000000..19a8aa9bd --- /dev/null +++ b/Subsurface/Source/Timing.cs @@ -0,0 +1,40 @@ +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Barotrauma +{ + static class Timing + { + private static double alpha; + + public static double Accumulator; + public static double Step = 1.0 / 60.0; + + public static double Alpha + { + get { return alpha; } + set { alpha = Math.Min(Math.Max(value, 0.0), 1.0); } + } + + public static double Interpolate(double previous, double current) + { + return current * alpha + previous * (1.0 - alpha); + } + + public static float Interpolate(float previous, float current) + { + return current * (float)alpha + previous * (1.0f - (float)alpha); + } + + public static Vector2 Interpolate(Vector2 previous, Vector2 current) + { + return new Vector2( + Interpolate(previous.X, current.X), + Interpolate(previous.Y, current.Y)); + } + } +}