Project_WL/Assets/Script/Util/TurnOff_GO.cs

97 lines
2.4 KiB
C#
Raw Normal View History

2024-12-15 01:39:39 +00:00
using System;
2025-02-11 07:09:38 +00:00
using System.Collections.Generic;
2024-12-15 01:39:39 +00:00
using UnityEngine;
public class TurnOff_GO : MyCoroutine
{
public bool NoUseParticleTime = false;
public float OffTime = 5f;
public eEffectLocation e_EffectLocation;
public Action act_End;
2025-02-11 07:09:38 +00:00
private Actor m_target;
private ProjectileData m_ProjectileData;
private bool followTarget;
2024-12-15 01:39:39 +00:00
2025-02-11 07:09:38 +00:00
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)
2024-12-15 01:39:39 +00:00
{
followTarget = true;
m_target = target;
m_ProjectileData = pd;
NoUseParticleTime = true;
2025-02-11 07:09:38 +00:00
OffTime = lifeTime;
2024-12-15 01:39:39 +00:00
2025-02-11 07:09:38 +00:00
transform.position = pd.Get_EffectPos(target);
ToggleActive(true);
2024-12-15 01:39:39 +00:00
}
2025-02-11 07:09:38 +00:00
public void Set(Actor target, eEffectLocation loca, float lifeTime)
2024-12-15 01:39:39 +00:00
{
e_EffectLocation = loca;
2025-02-11 07:09:38 +00:00
OffTime = lifeTime;
m_target = target;
2024-12-15 01:39:39 +00:00
2025-02-11 07:09:38 +00:00
ToggleActive(true);
2024-12-15 01:39:39 +00:00
}
2025-02-11 07:09:38 +00:00
2024-12-15 01:39:39 +00:00
public void Set_PrefabInfo(Actor target)
{
2025-02-11 07:09:38 +00:00
m_target = target;
UpdateLocation();
ToggleActive(true);
2024-12-15 01:39:39 +00:00
}
2025-02-11 07:09:38 +00:00
private void ToggleActive(bool state)
{
gameObject.SetActive(false);
gameObject.SetActive(state);
}
private void OnEnable() => Set_Off();
private void Set_Off()
2024-12-15 01:39:39 +00:00
{
Set_Coroutine_withCancel(() =>
{
gameObject.SetActive(false);
act_End?.Invoke();
}, NoUseParticleTime ? OffTime : GetComponent<ParticleSystem>().main.duration);
}
2025-02-11 07:09:38 +00:00
private void Update()
2024-12-15 01:39:39 +00:00
{
if (followTarget)
{
2025-02-11 07:09:38 +00:00
if (m_target == null)
2024-12-15 01:39:39 +00:00
{
transform.position = Vector3.right * 1000000f;
gameObject.SetActive(false);
}
else
{
2025-02-11 07:09:38 +00:00
transform.position = m_ProjectileData.Get_EffectPos(m_target);
2024-12-15 01:39:39 +00:00
}
}
2025-02-11 07:09:38 +00:00
else
{
UpdateLocation();
}
2024-12-15 01:39:39 +00:00
}
2025-02-11 07:09:38 +00:00
private void UpdateLocation()
2024-12-15 01:39:39 +00:00
{
2025-02-11 07:09:38 +00:00
if (m_target != null && EffectLocationMap.TryGetValue(e_EffectLocation, out var positionFunc))
2024-12-15 01:39:39 +00:00
{
2025-02-11 07:09:38 +00:00
transform.position = positionFunc(m_target);
2024-12-15 01:39:39 +00:00
}
}
}