using System.Collections.Generic;
using Core.Health;
using Core.Utilities;
using UnityEngine;
namespace TowerDefense.Agents
{
///
/// This effect will get attached to an agent that is within range of the SlowAffector radius
///
public class AgentSlower : AgentEffect
{
protected GameObject m_SlowFx;
///
/// 列表的原因是这个效果是可以叠加的,多个效果叠加在一起必须得使用列表的形式来处理。
///
protected List m_CurrentEffects = new List();
///
/// Initializes the slower with the parameters configured in the SlowAffector
///
/// Normalized float that represents the % slowdown applied to the agent
/// The instantiated object to visualize the slow effect
///
///
public void Initialize(float slowFactor, GameObject slowfxPrefab = null,
Vector3 position = default(Vector3),
float scale = 1)
{
LazyLoad();
m_CurrentEffects.Add(slowFactor);
// find greatest slow effect
float min = slowFactor;
foreach (float item in m_CurrentEffects)
{
min = Mathf.Min(min, item);
}
float originalSpeed = m_Agent.originalMovementSpeed;
float newSpeed = originalSpeed * min;
m_Agent.navMeshNavMeshAgent.speed = newSpeed;
if (m_SlowFx == null && slowfxPrefab != null)
{
m_SlowFx = Poolable.TryGetPoolable(slowfxPrefab);
m_SlowFx.transform.parent = transform;
m_SlowFx.transform.localPosition = position;
m_SlowFx.transform.localScale *= scale;
}
m_Agent.removed += OnRemoved;
}
///
/// Resets the agent's speed
///
public void RemoveSlow(float slowFactor)
{
m_Agent.removed -= OnRemoved;
m_CurrentEffects.Remove(slowFactor);
if (m_CurrentEffects.Count != 0)
{
return;
}
// No more slow effects
ResetAgent();
}
///
/// Agent has died, remove affect
///
void OnRemoved(DamageableBehaviour targetable)
{
m_Agent.removed -= OnRemoved;
ResetAgent();
}
void ResetAgent()
{
if (m_Agent != null)
{
m_Agent.navMeshNavMeshAgent.speed = m_Agent.originalMovementSpeed;
}
if (m_SlowFx != null)
{
Poolable.TryPool(m_SlowFx);
m_SlowFx.transform.localScale = Vector3.one;
m_SlowFx = null;
}
Destroy(this);
}
}
}