Option to set a duration for a status effect

This commit is contained in:
Regalis
2016-03-26 23:20:02 +02:00
parent ca492bf0d4
commit 2f7295eaad
8 changed files with 58 additions and 59 deletions
@@ -9,15 +9,15 @@ namespace Barotrauma
private float delay; private float delay;
private float timer; private float startTimer;
private Entity entity; private Entity entity;
private List<IPropertyObject> targets; private List<IPropertyObject> targets;
public float Timer public float StartTimer
{ {
get { return timer; } get { return startTimer; }
} }
public DelayedEffect(XElement element) public DelayedEffect(XElement element)
@@ -30,7 +30,7 @@ namespace Barotrauma
{ {
if (this.type != type) return; if (this.type != type) return;
timer = delay; startTimer = delay;
this.entity = entity; this.entity = entity;
this.targets = targets; this.targets = targets;
@@ -40,9 +40,9 @@ namespace Barotrauma
public void Update(float deltaTime) 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); base.Apply(1.0f, entity, targets);
List.Remove(this); List.Remove(this);
+29 -35
View File
@@ -28,6 +28,8 @@ namespace Barotrauma
private string[] onContainingNames; private string[] onContainingNames;
private readonly float duration;
private readonly bool useItem; private readonly bool useItem;
public readonly ActionType type; public readonly ActionType type;
@@ -59,10 +61,8 @@ namespace Barotrauma
{ {
return new DelayedEffect(element); return new DelayedEffect(element);
} }
else
{ return new StatusEffect(element);
return new StatusEffect(element);
}
} }
protected StatusEffect(XElement element) protected StatusEffect(XElement element)
@@ -121,6 +121,9 @@ namespace Barotrauma
case "sound": case "sound":
sound = Sound.Load(attribute.Value.ToString()); sound = Sound.Load(attribute.Value.ToString());
break; break;
case "duration":
duration = ToolBox.GetAttributeFloat(attribute, 0.0f);
break;
default: default:
propertyAttributes.Add(attribute); propertyAttributes.Add(attribute);
break; break;
@@ -215,10 +218,10 @@ namespace Barotrauma
Apply(deltaTime, entity, targets); Apply(deltaTime, entity, targets);
} }
protected virtual void Apply(float deltaTime, Entity entity, List<IPropertyObject> targets) protected void Apply(float deltaTime, Entity entity, List<IPropertyObject> targets)
{ {
if (explosion != null) explosion.Explode(entity.WorldPosition);
if (explosion != null) explosion.Explode(entity.WorldPosition);
if (FireSize > 0.0f) if (FireSize > 0.0f)
{ {
@@ -247,46 +250,37 @@ namespace Barotrauma
if (!target.ObjectProperties.TryGetValue(propertyNames[i], out property)) 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) private IEnumerable<object> ApplyToPropertyOverDuration(float duration, ObjectProperty property, object value)
//{ {
// if (explosion != null) explosion.Explode(item.SimPosition); float timer = duration;
while (timer > 0.0f)
{
ApplyToProperty(property, value, CoroutineManager.UnscaledDeltaTime);
// if (sound != null) sound.Play(1.0f, 1000.0f, item.body.FarseerBody); timer -= CoroutineManager.DeltaTime;
// for (int i = 0; i < propertyNames.Count(); i++) yield return CoroutineStatus.Running;
// { }
// ObjectProperty property;
// if (Character!=null && Character.properties.TryGetValue(propertyNames[i], out property)) yield return CoroutineStatus.Success;
// { }
// ApplyToProperty(property, propertyEffects[i], deltaTime);
// }
// 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);
// }
// }
//}
protected void ApplyToProperty(ObjectProperty property, object value, float deltaTime) protected void ApplyToProperty(ObjectProperty property, object value, float deltaTime)
{ {
if (disableDeltaTime) deltaTime = 1.0f; if (disableDeltaTime || setValue) deltaTime = 1.0f;
Type type = value.GetType(); Type type = value.GetType();
if (type == typeof(float) || if (type == typeof(float) ||
+6 -6
View File
@@ -14,7 +14,7 @@ namespace Barotrauma
{ {
static readonly List<IEnumerator<object>> Coroutines = new List<IEnumerator<object>>(); static readonly List<IEnumerator<object>> Coroutines = new List<IEnumerator<object>>();
public static float DeltaTime; public static float UnscaledDeltaTime, DeltaTime;
// Starting a coroutine just means adding an enumerator to the list. // Starting a coroutine just means adding an enumerator to the list.
// You might also want to be able to stop coroutines or delete them, // 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 // 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; DeltaTime = deltaTime;
for (int i = Coroutines.Count-1; i>=0; i--) for (int i = Coroutines.Count-1; i>=0; i--)
{ {
if (Coroutines[i].Current != null) 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(unscaledDeltaTime)) continue;
if (!wfs.CheckFinished(deltaTime)) continue;
} }
else else
{ {
+7 -2
View File
@@ -284,6 +284,8 @@ namespace Barotrauma
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds; double deltaTime = gameTime.ElapsedGameTime.TotalSeconds;
PlayerInput.Update(deltaTime); PlayerInput.Update(deltaTime);
bool paused = false;
if (hasLoaded && !titleScreenOpen) if (hasLoaded && !titleScreenOpen)
{ {
SoundPlayer.Update(); SoundPlayer.Update();
@@ -292,7 +294,10 @@ namespace Barotrauma
DebugConsole.Update(this, (float)deltaTime); 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); GUI.Update((float)deltaTime);
@@ -306,7 +311,7 @@ namespace Barotrauma
} }
} }
CoroutineManager.Update((float)deltaTime); CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime);
} }
+3 -3
View File
@@ -64,21 +64,21 @@ namespace Barotrauma
yield return CoroutineStatus.Success; 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; 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); 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); 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) if (diff != Vector2.Zero)
{ {
sub.ApplyForce((Vector2.Normalize(diff) * targetSpeed - sub.Velocity) * 500.0f); sub.ApplyForce((Vector2.Normalize(diff) * targetSpeed - sub.Velocity) * 500.0f);
} }
timer += CoroutineManager.DeltaTime; timer += CoroutineManager.UnscaledDeltaTime;
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
} }
+2 -2
View File
@@ -146,7 +146,7 @@ namespace Barotrauma.Networking
updateInterval = new TimeSpan(0, 0, 0, 0, 150); updateInterval = new TimeSpan(0, 0, 0, 0, 150);
// Set timer to tick every 50ms // 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 // When time has elapsed ( 50ms in this case ), call "update_Elapsed" funtion
//update.Elapsed += new System.Timers.ElapsedEventHandler(Update); //update.Elapsed += new System.Timers.ElapsedEventHandler(Update);
@@ -690,7 +690,7 @@ namespace Barotrauma.Networking
do do
{ {
secondsLeft -= CoroutineManager.DeltaTime; secondsLeft -= CoroutineManager.UnscaledDeltaTime;
//float camAngle = (float)((DateTime.Now - endTime).TotalSeconds / endPreviewLength) * MathHelper.TwoPi; //float camAngle = (float)((DateTime.Now - endTime).TotalSeconds / endPreviewLength) * MathHelper.TwoPi;
//Vector2 offset = (new Vector2( //Vector2 offset = (new Vector2(
+1 -1
View File
@@ -1042,7 +1042,7 @@ namespace Barotrauma.Networking
do do
{ {
secondsLeft -= CoroutineManager.DeltaTime; secondsLeft -= CoroutineManager.UnscaledDeltaTime;
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
} while (secondsLeft > 0.0f); } while (secondsLeft > 0.0f);
+1 -1
View File
@@ -56,7 +56,7 @@ namespace Barotrauma
{ {
GUI.ScreenOverlayColor = Color.Lerp(from, to, Math.Min(timer / duration, 1.0f)); GUI.ScreenOverlayColor = Color.Lerp(from, to, Math.Min(timer / duration, 1.0f));
timer += CoroutineManager.DeltaTime; timer += CoroutineManager.UnscaledDeltaTime;
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
} }