Fixed timestep for some GUI & input code

Fixed timestep only applied to the physics simulation, but input and GUI updates were not being handled properly, so here's some progress into fixing them.
This commit is contained in:
juanjp600
2016-09-18 11:03:52 -03:00
parent 179c0424d7
commit 33641ead3e
14 changed files with 153 additions and 50 deletions

View File

@@ -450,13 +450,11 @@ namespace Barotrauma
if (pauseMenuOpen)
{
pauseMenu.Update(0.016f);
pauseMenu.Draw(spriteBatch);
}
if (settingsMenuOpen)
{
GameMain.Config.SettingsFrame.Update(0.016f);
GameMain.Config.SettingsFrame.Draw(spriteBatch);
}
@@ -469,6 +467,17 @@ namespace Barotrauma
public static void Update(float deltaTime)
{
if (pauseMenuOpen)
{
pauseMenu.Update(0.016f);
}
if (settingsMenuOpen)
{
GameMain.Config.SettingsFrame.Update(0.016f);
}
if (GUIMessageBox.MessageBoxes.Count > 0)
{
var messageBox = GUIMessageBox.MessageBoxes.Peek();

View File

@@ -161,6 +161,27 @@ namespace Barotrauma
public override void Draw(SpriteBatch spriteBatch)
{
if (!Visible) return;
//Color currColor = color;
//if (state == ComponentState.Hover) currColor = hoverColor;
//if (state == ComponentState.Selected) currColor = selectedColor;
//GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
////spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
//GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
DrawChildren(spriteBatch);
//if (!Enabled) GUI.DrawRectangle(spriteBatch, rect, Color.Gray*0.5f, true);
}
public override void Update(float deltaTime)
{
if (!Visible) return;
base.Update(deltaTime);
if (rect.Contains(PlayerInput.MousePosition) && CanBeSelected && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn)))
{
@@ -183,7 +204,7 @@ namespace Barotrauma
else
{
Selected = !Selected;
// state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected;
// state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected;
}
}
}
@@ -193,20 +214,6 @@ namespace Barotrauma
}
frame.State = state;
//Color currColor = color;
//if (state == ComponentState.Hover) currColor = hoverColor;
//if (state == ComponentState.Selected) currColor = selectedColor;
//GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
////spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
//GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
DrawChildren(spriteBatch);
//if (!Enabled) GUI.DrawRectangle(spriteBatch, rect, Color.Gray*0.5f, true);
}
}
}

View File

@@ -71,6 +71,9 @@ namespace Barotrauma
private bool hasLoaded;
private GameTime fixedTime;
private double updatesToMake;
//public static Random localRandom;
//public static Random random;
@@ -141,6 +144,9 @@ namespace Barotrauma
IsFixedTimeStep = false;
//TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55);
updatesToMake = 0.0;
fixedTime = new GameTime();
World = new World(new Vector2(0, -9.82f));
FarseerPhysics.Settings.AllowSleep = true;
FarseerPhysics.Settings.ContinuousPhysics = false;
@@ -286,40 +292,52 @@ namespace Barotrauma
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
double realDeltaTime = gameTime.ElapsedGameTime.TotalSeconds;
double deltaTime = 0.016;
updatesToMake += realDeltaTime;
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;
PlayerInput.Update(deltaTime);
bool paused = false;
if (hasLoaded && !titleScreenOpen)
while (updatesToMake > 0.0)
{
SoundPlayer.Update();
fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly;
TimeSpan addTime = new TimeSpan(0,0,0,0,16);
fixedTime.ElapsedGameTime = addTime;
fixedTime.TotalGameTime.Add(addTime);
base.Update(fixedTime);
if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu();
PlayerInput.Update(deltaTime);
DebugConsole.Update(this, (float)deltaTime);
bool paused = false;
paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen || GUI.SettingsMenuOpen) &&
(NetworkMember == null || !NetworkMember.GameStarted);
if (!paused) Screen.Selected.Update(deltaTime);
if (NetworkMember != null)
if (hasLoaded && !titleScreenOpen)
{
NetworkMember.Update((float)deltaTime);
}
else
{
SoundPlayer.Update();
if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu();
DebugConsole.Update(this, (float)deltaTime);
paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen || GUI.SettingsMenuOpen) &&
(NetworkMember == null || !NetworkMember.GameStarted);
if (!paused || Screen.Selected is MainMenuScreen) Screen.Selected.Update(deltaTime);
if (NetworkMember != null)
{
NetworkMember.Update((float)deltaTime);
}
else
{
}
GUI.Update((float)deltaTime);
}
GUI.Update((float)deltaTime);
CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime);
updatesToMake -= deltaTime;
}
CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime);
}

View File

@@ -469,6 +469,8 @@ namespace Barotrauma.Items.Components
//}
public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { }
public virtual void UpdateHUD(Character character) { }
/// <returns>true if the operation was completed</returns>
public virtual bool AIOperate(float deltaTime, Character character, AIObjectiveOperateItem objective)

View File

@@ -82,10 +82,14 @@ namespace Barotrauma.Items.Components
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{
GuiFrame.Update((float)Physics.step);
GuiFrame.Draw(spriteBatch);
}
public override void UpdateHUD(Character character)
{
GuiFrame.Update((float)Physics.step);
}
private bool ToggleActive(GUIButton button, object obj)
{
SetActive(!IsActive);

View File

@@ -338,6 +338,11 @@ namespace Barotrauma.Items.Components
}
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{
GuiFrame.Draw(spriteBatch);
}
public override void UpdateHUD(Character character)
{
FabricableItem targetItem = itemList.SelectedData as FabricableItem;
if (targetItem != null)
@@ -346,7 +351,6 @@ namespace Barotrauma.Items.Components
}
GuiFrame.Update((float)Physics.step);
GuiFrame.Draw(spriteBatch);
}
private bool CanBeFabricated(FabricableItem fabricableItem, Character user)

View File

@@ -156,13 +156,17 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch);
spriteBatch.DrawString(GUI.Font, "Pumping speed: " + (int)flowPercentage + " %", new Vector2(x + 40, y + 85), Color.White);
}
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
{
base.ReceiveSignal(stepsTaken, signal, connection, sender, power);

View File

@@ -102,8 +102,6 @@ namespace Barotrauma.Items.Components
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch);
if (voltage < minVoltage && powerConsumption > 0.0f) return;
@@ -112,6 +110,11 @@ namespace Barotrauma.Items.Components
DrawRadar(spriteBatch, new Rectangle((int)GuiFrame.Center.X - radius, (int)GuiFrame.Center.Y - radius, radius * 2, radius * 2));
}
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private void DrawRadar(SpriteBatch spriteBatch, Rectangle rect)
{
Vector2 center = new Vector2(rect.X + rect.Width*0.5f, rect.Center.Y);

View File

@@ -438,7 +438,6 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch);
float xOffset = (graphTimer / (float)updateGraphInterval);
@@ -481,6 +480,11 @@ namespace Barotrauma.Items.Components
//y = y - 260;
}
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private bool ToggleAutoTemp(GUITickBox tickBox)
{
unsentChanges = true;

View File

@@ -161,7 +161,6 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch);
if (voltage < minVoltage && powerConsumption > 0.0f) return;
@@ -210,6 +209,11 @@ namespace Barotrauma.Items.Components
}
}
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private void UpdateAutoPilot(float deltaTime)
{
if (posToMaintain != null)

View File

@@ -184,18 +184,23 @@ namespace Barotrauma
}
public static void DrawHud(SpriteBatch spriteBatch, Item item, Character character)
{
if (frame == null) return;
frame.Draw(spriteBatch);
}
public static void UpdateHud(Item item,Character character)
{
if (frame == null || frame.UserData != item)
{
CreateGUIFrame(item);
}
UpdateGUIFrame(item, character);
if (frame == null) return;
frame.Update((float)Physics.step);
frame.Draw(spriteBatch);
}
}
}

View File

@@ -1057,6 +1057,20 @@ namespace Barotrauma
}
}
public virtual void UpdateHUD(Character character)
{
if (condition <= 0.0f)
{
FixRequirement.UpdateHud(this, character);
return;
}
foreach (ItemComponent ic in components)
{
ic.UpdateHUD(character);
}
}
public List<T> GetConnectedComponents<T>(bool recursive = false)
{
List<T> connectedComponents = new List<T>();

View File

@@ -108,6 +108,14 @@ namespace Barotrauma
Character.UpdateAll(cam, (float)deltaTime);
if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null)
{
if (Character.Controlled.SelectedConstruction == Character.Controlled.ClosestItem)
{
Character.Controlled.SelectedConstruction.UpdateHUD(Character.Controlled);
}
}
BackgroundCreatureManager.Update(cam, (float)deltaTime);
GameMain.ParticleManager.Update((float)deltaTime);

View File

@@ -12,6 +12,8 @@ namespace Barotrauma
{
public enum Tab { NewGame = 1, LoadGame = 2, HostServer = 3, Settings = 4 }
private GUIButton[] menuButtons;
GUIFrame buttonsTab;
private GUIFrame[] menuTabs;
@@ -43,42 +45,52 @@ namespace Barotrauma
290, y,
500, 360);
menuButtons = new GUIButton[8];
GUIButton button = new GUIButton(new Rectangle(50, y, 200, 30), "Tutorial", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.OnClicked = TutorialButtonClicked;
menuButtons[0] = button;
button = new GUIButton(new Rectangle(50, y + 60, 200, 30), "New Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.UserData = Tab.NewGame;
button.OnClicked = SelectTab;
menuButtons[1] = button;
button = new GUIButton(new Rectangle(50, y + 100, 200, 30), "Load Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.UserData = Tab.LoadGame;
button.OnClicked = SelectTab;
menuButtons[2] = button;
button = new GUIButton(new Rectangle(50, y + 160, 200, 30), "Join Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
//button.UserData = (int)Tabs.JoinServer;
button.OnClicked = JoinServerClicked;
menuButtons[3] = button;
button = new GUIButton(new Rectangle(50, y + 200, 200, 30), "Host Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.UserData = Tab.HostServer;
button.OnClicked = SelectTab;
menuButtons[4] = button;
button = new GUIButton(new Rectangle(50, y + 260, 200, 30), "Submarine Editor", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.OnClicked = (GUIButton btn, object userdata) => { GameMain.EditMapScreen.Select(); return true; };
menuButtons[5] = button;
button = new GUIButton(new Rectangle(50, y + 320, 200, 30), "Settings", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.UserData = Tab.Settings;
button.OnClicked = SelectTab;
menuButtons[6] = button;
button = new GUIButton(new Rectangle(0, 0, 150, 30), "Quit", Alignment.BottomRight, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f;
button.OnClicked = QuitClicked;
menuButtons[7] = button;
panelRect.Y += 10;
@@ -478,6 +490,11 @@ namespace Barotrauma
public override void Update(double deltaTime)
{
foreach (GUIButton button in menuButtons)
{
button.Update((float)deltaTime);
}
buttonsTab.Update((float)deltaTime);
if (selectedTab>0) menuTabs[selectedTab].Update((float)deltaTime);