using Photon.Pun; 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 PhotonView photonView; public PhotonView owner; public Taggable ownerTag; public ProjectileAbility ability; public float speed; public float lifeSpan; private PhotonView possibleTarget; private Taggable target; private bool waitingForDestroy = false; public UnityEvent onTargetHit = new UnityEvent(); private List hitSpawnedVFXs = new List(); private GameObject hitSpawnedVFX; private Vector3 hitPositionCorrected; private float currentspeed; private void Awake() { photonView = GetComponent(); onTargetHit.AddListener(SpawnHitParticleVFX); } public void Init() { waitingForDestroy = false; if (photonView.IsMine) { photonView.RPC(nameof(RPC_RemoteInit), RpcTarget.Others, AbilityIndexer.Instance.Abilities.IndexOf(ability)); currentspeed = speed / 10f; //StartCoroutine(SelfDestruct()); } } private void OnTriggerEnter(Collider other) { if (waitingForDestroy) return; possibleTarget = other.GetComponentInParent(); if (possibleTarget != null) { if (possibleTarget != owner) return; target = other.GetComponentInParent(); if (target == null) return; hitPositionCorrected = target.transform.position; hitPositionCorrected.y = this.transform.position.y; onTargetHit.Invoke(hitPositionCorrected); if (!photonView.IsMine) return; foreach (BaseEffect effect in ability.abilityEffects) { effect.ApplyEffect(ownerTag, new List { target }); } waitingForDestroy = true; StartCoroutine(DelayedDestroy()); } } private void Update() { if (!photonView.IsMine) return; if (waitingForDestroy) return; currentspeed += Time.deltaTime*2f; this.transform.position = Vector3.Lerp(this.transform.position, owner.transform.position, currentspeed * Time.deltaTime); } [PunRPC] private void RPC_RemoteInit(int abilityIndex) { ability = (ProjectileAbility)AbilityIndexer.Instance.Abilities[abilityIndex]; } [PunRPC] private void RPC_DisableVisuals() { visuals.SetActive(false); } 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); photonView.RPC(nameof(RPC_DisableVisuals), RpcTarget.Others); 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); PhotonNetwork.Destroy(photonView); } }