From 2f7295eaad3a304ed2bafa47b95674b2a7fa8c9c Mon Sep 17 00:00:00 2001 From: Regalis Date: Sat, 26 Mar 2016 23:20:02 +0200 Subject: [PATCH] Option to set a duration for a status effect --- Subsurface/Source/Characters/DelayedEffect.cs | 12 ++-- Subsurface/Source/Characters/StatusEffect.cs | 70 +++++++++---------- Subsurface/Source/CoroutineManager.cs | 12 ++-- Subsurface/Source/GameMain.cs | 9 ++- Subsurface/Source/Map/TransitionCinematic.cs | 6 +- Subsurface/Source/Networking/GameClient.cs | 4 +- Subsurface/Source/Networking/GameServer.cs | 2 +- Subsurface/Source/Screens/Screen.cs | 2 +- 8 files changed, 58 insertions(+), 59 deletions(-) diff --git a/Subsurface/Source/Characters/DelayedEffect.cs b/Subsurface/Source/Characters/DelayedEffect.cs index 195cab43c..6578aaab0 100644 --- a/Subsurface/Source/Characters/DelayedEffect.cs +++ b/Subsurface/Source/Characters/DelayedEffect.cs @@ -9,15 +9,15 @@ namespace Barotrauma private float delay; - private float timer; + private float startTimer; private Entity entity; private List targets; - public float Timer + public float StartTimer { - get { return timer; } + get { return startTimer; } } public DelayedEffect(XElement element) @@ -30,7 +30,7 @@ namespace Barotrauma { if (this.type != type) return; - timer = delay; + startTimer = delay; this.entity = entity; this.targets = targets; @@ -40,9 +40,9 @@ namespace Barotrauma public void Update(float deltaTime) { - timer -= deltaTime; + startTimer -= deltaTime; - if (timer > 0.0f) return; + if (startTimer > 0.0f) return; base.Apply(1.0f, entity, targets); List.Remove(this); diff --git a/Subsurface/Source/Characters/StatusEffect.cs b/Subsurface/Source/Characters/StatusEffect.cs index 5b710d33e..76c4cfda7 100644 --- a/Subsurface/Source/Characters/StatusEffect.cs +++ b/Subsurface/Source/Characters/StatusEffect.cs @@ -23,10 +23,12 @@ namespace Barotrauma private object[] propertyEffects; private bool setValue; - + private bool disableDeltaTime; private string[] onContainingNames; + + private readonly float duration; private readonly bool useItem; @@ -59,10 +61,8 @@ namespace Barotrauma { return new DelayedEffect(element); } - else - { - return new StatusEffect(element); - } + + return new StatusEffect(element); } protected StatusEffect(XElement element) @@ -121,6 +121,9 @@ namespace Barotrauma case "sound": sound = Sound.Load(attribute.Value.ToString()); break; + case "duration": + duration = ToolBox.GetAttributeFloat(attribute, 0.0f); + break; default: propertyAttributes.Add(attribute); break; @@ -215,11 +218,11 @@ namespace Barotrauma Apply(deltaTime, entity, targets); } - protected virtual void Apply(float deltaTime, Entity entity, List targets) + protected void Apply(float deltaTime, Entity entity, List targets) { + if (explosion != null) explosion.Explode(entity.WorldPosition); - - + if (FireSize > 0.0f) { var fire = new FireSource(entity.WorldPosition); @@ -246,47 +249,38 @@ namespace Barotrauma //if (targetNames!=null && !targetNames.Contains(target.Name)) continue; if (!target.ObjectProperties.TryGetValue(propertyNames[i], out property)) continue; - - ApplyToProperty(property, propertyEffects[i], deltaTime); - } + if (duration > 0.0f) + { + CoroutineManager.StartCoroutine(ApplyToPropertyOverDuration(duration, property, propertyEffects[i])); + } + else + { + ApplyToProperty(property, propertyEffects[i], deltaTime); + } + } } } - //protected virtual void Apply(float deltaTime, Character Character, Item item) - //{ - // if (explosion != null) explosion.Explode(item.SimPosition); + private IEnumerable ApplyToPropertyOverDuration(float duration, ObjectProperty property, object value) + { + float timer = duration; + while (timer > 0.0f) + { + ApplyToProperty(property, value, CoroutineManager.UnscaledDeltaTime); - // if (sound != null) sound.Play(1.0f, 1000.0f, item.body.FarseerBody); - - // for (int i = 0; i < propertyNames.Count(); i++) - // { - // ObjectProperty property; + timer -= CoroutineManager.DeltaTime; - // if (Character!=null && Character.properties.TryGetValue(propertyNames[i], out property)) - // { - // ApplyToProperty(property, propertyEffects[i], deltaTime); - // } + yield return CoroutineStatus.Running; + } - // if (item == null) continue; - - // if (item.properties.TryGetValue(propertyNames[i], out property)) - // { - // ApplyToProperty(property, propertyEffects[i], deltaTime); - // } - - // foreach (ItemComponent ic in item.components) - // { - // if (!ic.properties.TryGetValue(propertyNames[i], out property)) continue; - // ApplyToProperty(property, propertyEffects[i], deltaTime); - // } - // } - //} + yield return CoroutineStatus.Success; + } protected void ApplyToProperty(ObjectProperty property, object value, float deltaTime) { - if (disableDeltaTime) deltaTime = 1.0f; + if (disableDeltaTime || setValue) deltaTime = 1.0f; Type type = value.GetType(); if (type == typeof(float) || diff --git a/Subsurface/Source/CoroutineManager.cs b/Subsurface/Source/CoroutineManager.cs index c57fa7f75..dd2847a00 100644 --- a/Subsurface/Source/CoroutineManager.cs +++ b/Subsurface/Source/CoroutineManager.cs @@ -14,7 +14,7 @@ namespace Barotrauma { static readonly List> Coroutines = new List>(); - public static float DeltaTime; + public static float UnscaledDeltaTime, DeltaTime; // Starting a coroutine just means adding an enumerator to the list. // You might also want to be able to stop coroutines or delete them, @@ -40,19 +40,19 @@ namespace Barotrauma } // Updating just means stepping through all the coroutines - public static void Update(float deltaTime) + public static void Update(float unscaledDeltaTime, float deltaTime) { + UnscaledDeltaTime = unscaledDeltaTime; DeltaTime = deltaTime; for (int i = Coroutines.Count-1; i>=0; i--) { if (Coroutines[i].Current != null) { - if (Coroutines[i].Current is WaitForSeconds) + WaitForSeconds wfs = Coroutines[i].Current as WaitForSeconds; + if (wfs != null) { - WaitForSeconds wfs = (WaitForSeconds)Coroutines[i].Current; - if (!wfs.CheckFinished(deltaTime)) continue; - + if (!wfs.CheckFinished(unscaledDeltaTime)) continue; } else { diff --git a/Subsurface/Source/GameMain.cs b/Subsurface/Source/GameMain.cs index f131838bb..8c9f48c62 100644 --- a/Subsurface/Source/GameMain.cs +++ b/Subsurface/Source/GameMain.cs @@ -284,6 +284,8 @@ namespace Barotrauma double deltaTime = gameTime.ElapsedGameTime.TotalSeconds; PlayerInput.Update(deltaTime); + bool paused = false; + if (hasLoaded && !titleScreenOpen) { SoundPlayer.Update(); @@ -292,7 +294,10 @@ namespace Barotrauma DebugConsole.Update(this, (float)deltaTime); - if ((!DebugConsole.IsOpen && !GUI.PauseMenuOpen) || (NetworkMember != null && NetworkMember.GameStarted)) Screen.Selected.Update(deltaTime); + paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen) && + (NetworkMember == null || !NetworkMember.GameStarted); + + if (!paused) Screen.Selected.Update(deltaTime); GUI.Update((float)deltaTime); @@ -306,7 +311,7 @@ namespace Barotrauma } } - CoroutineManager.Update((float)deltaTime); + CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime); } diff --git a/Subsurface/Source/Map/TransitionCinematic.cs b/Subsurface/Source/Map/TransitionCinematic.cs index c161ce65b..ce2fa7f5c 100644 --- a/Subsurface/Source/Map/TransitionCinematic.cs +++ b/Subsurface/Source/Map/TransitionCinematic.cs @@ -64,21 +64,21 @@ namespace Barotrauma yield return CoroutineStatus.Success; } - cam.Zoom = Math.Max(0.2f, cam.Zoom - CoroutineManager.DeltaTime * 0.1f); + cam.Zoom = Math.Max(0.2f, cam.Zoom - CoroutineManager.UnscaledDeltaTime * 0.1f); Vector2 cameraPos = sub.Position + Submarine.HiddenSubPosition; cameraPos.Y = Math.Min(cameraPos.Y, ConvertUnits.ToDisplayUnits(Level.Loaded.ShaftBodies[0].Position.Y) - cam.WorldView.Height/2.0f); GUI.ScreenOverlayColor = Color.Lerp(Color.TransparentBlack, Color.Black, timer/duration); - cam.Translate((cameraPos - cam.Position) * CoroutineManager.DeltaTime*10.0f); + cam.Translate((cameraPos - cam.Position) * CoroutineManager.UnscaledDeltaTime*10.0f); if (diff != Vector2.Zero) { sub.ApplyForce((Vector2.Normalize(diff) * targetSpeed - sub.Velocity) * 500.0f); } - timer += CoroutineManager.DeltaTime; + timer += CoroutineManager.UnscaledDeltaTime; yield return CoroutineStatus.Running; } diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 191a74f80..29bf04126 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -146,7 +146,7 @@ namespace Barotrauma.Networking updateInterval = new TimeSpan(0, 0, 0, 0, 150); // Set timer to tick every 50ms - //update = new System.Timers.Timer(50); + //update = new System.Timers.startTimer(50); // When time has elapsed ( 50ms in this case ), call "update_Elapsed" funtion //update.Elapsed += new System.Timers.ElapsedEventHandler(Update); @@ -690,7 +690,7 @@ namespace Barotrauma.Networking do { - secondsLeft -= CoroutineManager.DeltaTime; + secondsLeft -= CoroutineManager.UnscaledDeltaTime; //float camAngle = (float)((DateTime.Now - endTime).TotalSeconds / endPreviewLength) * MathHelper.TwoPi; //Vector2 offset = (new Vector2( diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index c03cfac7c..33b222346 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -1042,7 +1042,7 @@ namespace Barotrauma.Networking do { - secondsLeft -= CoroutineManager.DeltaTime; + secondsLeft -= CoroutineManager.UnscaledDeltaTime; yield return CoroutineStatus.Running; } while (secondsLeft > 0.0f); diff --git a/Subsurface/Source/Screens/Screen.cs b/Subsurface/Source/Screens/Screen.cs index d0b70ef8d..cf45ddc39 100644 --- a/Subsurface/Source/Screens/Screen.cs +++ b/Subsurface/Source/Screens/Screen.cs @@ -56,7 +56,7 @@ namespace Barotrauma { GUI.ScreenOverlayColor = Color.Lerp(from, to, Math.Min(timer / duration, 1.0f)); - timer += CoroutineManager.DeltaTime; + timer += CoroutineManager.UnscaledDeltaTime; yield return CoroutineStatus.Running; }