using ActionGameFramework.Health;
using TowerDefense.Agents;
using UnityEngine;
namespace TowerDefense.Affectors
{
///
/// Uses a trigger to attach and remove components to agents
///
public class SlowAffector : PassiveAffector
{
///
/// A normalized value to slow agents by
///
[Range(0, 1)]
public float slowFactor;
///
/// The slow factor for displaying to the UI
///
public string slowFactorFormat = "Slow Factor: {0}";
///
/// The particle system that plays when an entity enters the sphere
///
public ParticleSystem enterParticleSystem;
public GameObject slowFxPrefab;
///
/// The audio source that plays when an entity enters the sphere
///
public AudioSource audioSource;
///
/// Subsribes to the relevant targetter events
///
protected void Awake()
{
towerTargetter.targetEntersRange += OnTargetEntersRange;
towerTargetter.targetExitsRange += OnTargetExitsRange;
}
///
/// Unsubsribes from the relevant targetter events
///
void OnDestroy()
{
towerTargetter.targetEntersRange -= OnTargetEntersRange;
towerTargetter.targetExitsRange -= OnTargetExitsRange;
}
///
/// Attaches a to the agent
///
/// The agent to attach the slower to
protected void AttachSlowComponent(Agent target)
{
var slower = target.GetComponent();
if (slower == null)
{
slower = target.gameObject.AddComponent();
}
slower.Initialize(slowFactor, slowFxPrefab, target.appliedEffectOffset, target.appliedEffectScale);
if (enterParticleSystem != null)
{
enterParticleSystem.Play();
}
if (audioSource != null)
{
audioSource.Play();
}
}
///
/// Removes the from the agent once it leaves the area
///
/// The agent to remove the slower from
protected void RemoveSlowComponent(Agent target)
{
if (target == null)
{
return;
}
var slowComponent = target.gameObject.GetComponent();
if (slowComponent != null)
{
slowComponent.RemoveSlow(slowFactor);
}
}
///
/// Fired when the targetter aquires a new targetable
///
protected void OnTargetEntersRange(Targetable other)
{
var agent = other as Agent;
if (agent == null)
{
return;
}
AttachSlowComponent(agent);
}
///
/// Fired when the targetter aquires loses a targetable
///
protected void OnTargetExitsRange(Targetable other)
{
var searchable = other as Agent;
if (searchable == null)
{
return;
}
RemoveSlowComponent(searchable);
}
}
}