Removed fixed timestep logic from GameScreen (redundant now because the logic is handled in GameMain)
This commit is contained in:
@@ -164,6 +164,7 @@
|
||||
<Compile Include="Source\Screens\BlurEffect.cs" />
|
||||
<Compile Include="Source\Screens\NetLobbyVoting.cs" />
|
||||
<Compile Include="Source\Screens\ServerListScreen.cs" />
|
||||
<Compile Include="Source\Timing.cs" />
|
||||
<Compile Include="Source\Utils\MTRandom.cs" />
|
||||
<Compile Include="Source\Utils\Rand.cs" />
|
||||
<Compile Include="Source\Events\PropertyTask.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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -72,7 +72,6 @@ namespace Barotrauma
|
||||
private bool hasLoaded;
|
||||
|
||||
private GameTime fixedTime;
|
||||
private double updatesToMake;
|
||||
|
||||
//public static Random localRandom;
|
||||
//public static Random random;
|
||||
@@ -145,7 +144,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));
|
||||
@@ -292,12 +291,10 @@ namespace Barotrauma
|
||||
/// <param name="gameTime">Provides a snapshot of timing values.</param>
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
double realDeltaTime = gameTime.ElapsedGameTime.TotalSeconds;
|
||||
double deltaTime = 0.016;
|
||||
updatesToMake += realDeltaTime;
|
||||
Timing.Accumulator += gameTime.ElapsedGameTime.TotalSeconds;
|
||||
PlayerInput.UpdateVariable();
|
||||
|
||||
while (updatesToMake > 0.0)
|
||||
while (Timing.Accumulator >= Timing.Step)
|
||||
{
|
||||
fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly;
|
||||
TimeSpan addTime = new TimeSpan(0,0,0,0,16);
|
||||
@@ -305,7 +302,7 @@ namespace Barotrauma
|
||||
fixedTime.TotalGameTime.Add(addTime);
|
||||
base.Update(fixedTime);
|
||||
|
||||
PlayerInput.Update(deltaTime);
|
||||
PlayerInput.Update(Timing.Step);
|
||||
|
||||
bool paused = false;
|
||||
|
||||
@@ -319,32 +316,30 @@ namespace Barotrauma
|
||||
|
||||
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);
|
||||
Screen.Selected.Update(Timing.Step);
|
||||
}
|
||||
|
||||
if (NetworkMember != null)
|
||||
{
|
||||
NetworkMember.Update((float)deltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ namespace Barotrauma
|
||||
frame.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
public static void UpdateHud(Item item,Character character)
|
||||
public static void UpdateHud(Item item, Character character)
|
||||
{
|
||||
if (frame == null || frame.UserData != item)
|
||||
{
|
||||
@@ -200,7 +200,7 @@ namespace Barotrauma
|
||||
|
||||
if (frame == null) return;
|
||||
|
||||
frame.Update((float)Physics.step);
|
||||
frame.Update((float)Timing.Step);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -901,7 +901,7 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
editingHUD.Draw(spriteBatch);
|
||||
editingHUD.Update((float)Physics.step);
|
||||
editingHUD.Update((float)Timing.Step);
|
||||
|
||||
if (!prefab.IsLinkable) return;
|
||||
|
||||
@@ -929,7 +929,7 @@ namespace Barotrauma
|
||||
|
||||
if (editingHUD.Rect.Height > 60)
|
||||
{
|
||||
editingHUD.Update((float)Physics.step);
|
||||
editingHUD.Update((float)Timing.Step);
|
||||
editingHUD.Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,8 +148,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;
|
||||
|
||||
|
||||
@@ -325,7 +325,7 @@ namespace Barotrauma
|
||||
|
||||
public void UpdateTransform()
|
||||
{
|
||||
DrawPosition = Physics.Interpolate(prevPosition, Position);
|
||||
DrawPosition = Timing.Interpolate(prevPosition, Position);
|
||||
}
|
||||
|
||||
//math/physics stuff ----------------------------------------------------
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -303,7 +303,7 @@ namespace Barotrauma.Networking
|
||||
|
||||
if (gameStarted)
|
||||
{
|
||||
inGameHUD.Update((float)Physics.step);
|
||||
inGameHUD.Update((float)Timing.Step);
|
||||
|
||||
if (respawnManager != null) respawnManager.Update(deltaTime);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -339,10 +339,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;
|
||||
|
||||
@@ -379,7 +379,7 @@ namespace Barotrauma
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
|
||||
@@ -98,26 +98,15 @@ namespace Barotrauma
|
||||
/// <param name="gameTime">Provides a snapshot of timing values.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,9 +91,6 @@ namespace Barotrauma
|
||||
/// <param name="gameTime">Provides a snapshot of timing values.</param>
|
||||
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)
|
||||
@@ -109,15 +106,9 @@ namespace Barotrauma
|
||||
}
|
||||
#endif
|
||||
|
||||
Physics.accumulator = Math.Min(Physics.accumulator, Physics.step * 6);
|
||||
//Physics.accumulator = Physics.step;
|
||||
while (Physics.accumulator >= Physics.step)
|
||||
{
|
||||
if (GameMain.GameSession != null) GameMain.GameSession.Update((float)deltaTime);
|
||||
|
||||
if (GameMain.GameSession != null) GameMain.GameSession.Update((float)Physics.step);
|
||||
//EventManager.Update(gameTime);
|
||||
|
||||
if (Level.Loaded != null) Level.Loaded.Update((float)Physics.step);
|
||||
if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime);
|
||||
|
||||
if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null)
|
||||
{
|
||||
@@ -126,52 +117,43 @@ namespace Barotrauma
|
||||
Character.Controlled.SelectedConstruction.UpdateHUD(Character.Controlled);
|
||||
}
|
||||
}
|
||||
Character.UpdateAll(cam, (float)deltaTime);
|
||||
|
||||
|
||||
BackgroundCreatureManager.Update(cam, (float)deltaTime);
|
||||
|
||||
BackgroundCreatureManager.Update(cam, (float)Physics.step);
|
||||
GameMain.ParticleManager.Update((float)deltaTime);
|
||||
|
||||
GameMain.ParticleManager.Update((float)Physics.step);
|
||||
StatusEffect.UpdateAll((float)deltaTime);
|
||||
|
||||
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);
|
||||
if (Character.Controlled != null && Lights.LightManager.ViewTarget != null)
|
||||
{
|
||||
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)
|
||||
|
||||
40
Subsurface/Source/Timing.cs
Normal file
40
Subsurface/Source/Timing.cs
Normal file
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user