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

View File

@@ -9,15 +9,15 @@ namespace Barotrauma
private float delay;
private float timer;
private float startTimer;
private Entity entity;
private List<IPropertyObject> 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);

View File

@@ -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<IPropertyObject> targets)
protected void Apply(float deltaTime, Entity entity, List<IPropertyObject> 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<object> 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) ||

View File

@@ -14,7 +14,7 @@ namespace Barotrauma
{
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.
// 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
{

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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(

View File

@@ -1042,7 +1042,7 @@ namespace Barotrauma.Networking
do
{
secondsLeft -= CoroutineManager.DeltaTime;
secondsLeft -= CoroutineManager.UnscaledDeltaTime;
yield return CoroutineStatus.Running;
} while (secondsLeft > 0.0f);

View File

@@ -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;
}