Project_WL/Assets/Script/Util/TurnOff_GO.cs

97 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class TurnOff_GO : MyCoroutine
{
public bool NoUseParticleTime = false;
public float OffTime = 5f;
public eEffectLocation e_EffectLocation;
public Action act_End;
private Actor m_target;
private ProjectileData m_ProjectileData;
private bool followTarget;
private static readonly Dictionary<eEffectLocation, Func<Actor, Vector3>> EffectLocationMap = new()
{
{ eEffectLocation.Top, target => target.Get_Top_postion() },
{ eEffectLocation.Mid, target => target.Get_Center_position() },
{ eEffectLocation.Bottom, target => target.Get_position() }
};
public void Set() => ToggleActive(true);
public void Set(Actor target, ProjectileData pd, float lifeTime)
{
followTarget = true;
m_target = target;
m_ProjectileData = pd;
NoUseParticleTime = true;
OffTime = lifeTime;
transform.position = pd.Get_EffectPos(target);
ToggleActive(true);
}
public void Set(Actor target, eEffectLocation loca, float lifeTime)
{
e_EffectLocation = loca;
OffTime = lifeTime;
m_target = target;
ToggleActive(true);
}
public void Set_PrefabInfo(Actor target)
{
m_target = target;
UpdateLocation();
ToggleActive(true);
}
private void ToggleActive(bool state)
{
gameObject.SetActive(false);
gameObject.SetActive(state);
}
private void OnEnable() => Set_Off();
private void Set_Off()
{
Set_Coroutine_withCancel(() =>
{
gameObject.SetActive(false);
act_End?.Invoke();
}, NoUseParticleTime ? OffTime : GetComponent<ParticleSystem>().main.duration);
}
private void Update()
{
if (followTarget)
{
if (m_target == null)
{
transform.position = Vector3.right * 1000000f;
gameObject.SetActive(false);
}
else
{
transform.position = m_ProjectileData.Get_EffectPos(m_target);
}
}
else
{
UpdateLocation();
}
}
private void UpdateLocation()
{
if (m_target != null && EffectLocationMap.TryGetValue(e_EffectLocation, out var positionFunc))
{
transform.position = positionFunc(m_target);
}
}
}