RiftMayhem/Assets/Scripts/Networking/NetworkedDrainProjectile.cs
2025-02-21 23:51:46 +00:00

127 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class NetworkedDrainProjectile : MonoBehaviour
{
[Header("Visuals")]
[SerializeField] private GameObject hitParticlesPrefab;
[SerializeField] private GameObject visuals;
[Header("Set by code")]
public Taggable ownerTag;
public ProjectileAbility ability;
public float speed;
public float lifeSpan;
private Taggable target;
private bool waitingForDestroy = false;
public UnityEvent<Vector3> onTargetHit = new UnityEvent<Vector3>();
private List<GameObject> hitSpawnedVFXs = new List<GameObject>();
private GameObject hitSpawnedVFX;
private Vector3 hitPositionCorrected;
private float currentspeed;
private void Awake()
{
onTargetHit.AddListener(SpawnHitParticleVFX);
}
public void Init()
{
waitingForDestroy = false;
currentspeed = speed / 10f;
//StartCoroutine(SelfDestruct());
}
private void OnTriggerEnter(Collider other)
{
if (waitingForDestroy) return;
target = other.GetComponentInParent<Taggable>();
if (target == null) return;
if (target != ownerTag) return;
hitPositionCorrected = target.transform.position;
hitPositionCorrected.y = this.transform.position.y;
onTargetHit.Invoke(hitPositionCorrected);
foreach (BaseEffect effect in ability.abilityEffects)
{
effect.ApplyEffect(ownerTag, new List<Taggable> { target });
}
waitingForDestroy = true;
StartCoroutine(DelayedDestroy());
}
private void Update()
{
if (waitingForDestroy) return;
if (ownerTag == null)
{
waitingForDestroy = true;
StartCoroutine(DelayedDestroy());
return;
}
currentspeed += Time.deltaTime * 2f;
this.transform.position = Vector3.Lerp(this.transform.position, ownerTag.transform.position, currentspeed * Time.deltaTime);
}
private void SpawnHitParticleVFX(Vector3 position)
{
if (hitParticlesPrefab == null) return;
hitSpawnedVFX = Instantiate(hitParticlesPrefab, position, this.transform.rotation);
//hitSpawnedVFX.transform.localScale = visuals.transform.localScale;
hitSpawnedVFXs.Add(hitSpawnedVFX);
}
IEnumerator SelfDestruct()
{
yield return new WaitForSeconds(lifeSpan);
waitingForDestroy = true;
StartCoroutine(DelayedDestroy());
}
IEnumerator DelayedDestroy()
{
visuals.SetActive(false);
yield return new WaitForSeconds(1.5f);
for (int i = hitSpawnedVFXs.Count - 1; i >= 0; i--)
{
if (hitSpawnedVFXs[i] != null)
{
Destroy(hitSpawnedVFXs[i]);
}
}
yield return new WaitForSeconds(1f);
Destroy(this.gameObject);
}
}