RiftMayhem/Assets/Scripts/Networking/NetworkedProjectile.cs
2025-02-21 18:35:51 +00:00

149 lines
3.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class NetworkedProjectile : 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;
public bool canPierce;
public bool canHitSelf;
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;
protected List<Taggable> processedTargets = new List<Taggable>();
NetworkedAntiProjectile possibleBlock;
private void Awake()
{
onTargetHit.AddListener(SpawnHitParticleVFX);
}
public void Init()
{
waitingForDestroy = false;
StartCoroutine(SelfDestruct());
}
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);
}
private void Update()
{
if (waitingForDestroy) return;
this.transform.position += this.transform.forward * speed * Time.deltaTime;
}
private void OnTriggerEnter(Collider other)
{
if (waitingForDestroy) return;
if(other.GetComponent<IBreakable>() != null)
{
other.GetComponent<IBreakable>().Hit();
onTargetHit.Invoke(this.transform.position);
waitingForDestroy = true;
StartCoroutine(DelayedDestroy());
}
target = other.GetComponentInParent<Taggable>();
if (target == null) return;
if (target == ownerTag && !canHitSelf) return;
if (!target.IsValidTarget(ability.targettingTags)) return;
if (processedTargets.Contains(target)) return;
processedTargets.Add(target);
hitPositionCorrected = target.transform.position;
hitPositionCorrected.y = this.transform.position.y;
onTargetHit.Invoke(hitPositionCorrected);
possibleBlock = target.GetComponentInParent<NetworkedAntiProjectile>();
if (possibleBlock != null)
{
waitingForDestroy = true;
possibleBlock.SendBlockNotice();
StartCoroutine(DelayedDestroy());
return;
}
foreach (BaseEffect effect in ability.abilityEffects)
{
effect.ApplyEffect(ownerTag, new List<Taggable> { target });
}
if (!canPierce)
{
waitingForDestroy = true;
StartCoroutine(DelayedDestroy());
}
}
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);
}
}