// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Collections.Generic;
using System.Linq;
namespace CUE.NET.Effects
{
///
/// Represents an generic effect-target.
///
///
public abstract class AbstractEffectTarget : IEffectTarget
where T : IEffectTarget
{
#region Properties & Fields
private IList _effectTimes = new List();
///
/// Gets all attached to this target.
///
protected IList> Effects => _effectTimes.Select(x => x.Effect).Cast>().ToList();
///
/// Gets the strongly-typed target used for the effect.
///
protected abstract T EffectTarget { get; }
#endregion
#region Methods
///
/// Updates all effects added to this target.
///
public void UpdateEffects()
{
lock (Effects)
{
for (int i = _effectTimes.Count - 1; i >= 0; i--)
{
EffectTimeContainer effectTime = _effectTimes[i];
long currentTicks = DateTime.Now.Ticks;
float deltaTime;
if (effectTime.TicksAtLastUpdate < 0)
{
effectTime.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
}
else
deltaTime = (currentTicks - effectTime.TicksAtLastUpdate) / 10000000f;
effectTime.TicksAtLastUpdate = currentTicks;
effectTime.Effect.Update(deltaTime);
if (effectTime.Effect.IsDone)
_effectTimes.RemoveAt(i);
}
}
}
///
/// Adds an affect.
///
/// The effect to add.
public void AddEffect(IEffect effect)
{
if (_effectTimes.Any(x => x.Effect == effect)) return;
effect.OnAttach(EffectTarget);
_effectTimes.Add(new EffectTimeContainer(effect, -1));
}
///
/// Removes an effect
///
/// The effect to remove.
public void RemoveEffect(IEffect effect)
{
EffectTimeContainer effectTimeToRemove = _effectTimes.FirstOrDefault(x => x.Effect == effect);
if (effectTimeToRemove == null) return;
effect.OnDetach(EffectTarget);
_effectTimes.Remove(effectTimeToRemove);
}
#endregion
}
}