55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Rigidbody), typeof(Collider), typeof(Damager))]
|
|
public abstract class AbstractProjectile : MonoBehaviour
|
|
{
|
|
public static event Action<AbstractProjectile> OnSpawned;
|
|
|
|
[SerializeField] protected ParticleSystem lifetimeVFX;
|
|
[SerializeField] protected GameObject explosionSoundPrefab;
|
|
[SerializeField] protected GameObject explosionVfxPrefab;
|
|
|
|
protected Rigidbody rb;
|
|
private Damager damager;
|
|
public GameObject Owner { get; protected set; }
|
|
protected virtual void Launch(GameObject owner, float damage)
|
|
{
|
|
damager = GetComponent<Damager>();
|
|
rb = GetComponent<Rigidbody>();
|
|
Owner = owner;
|
|
damager.Initialize(owner, damage);
|
|
damager.OnHit += ExplodeProjectile;
|
|
|
|
OnSpawned?.Invoke(this);
|
|
}
|
|
|
|
public virtual void ExplodeProjectile()
|
|
{
|
|
if (explosionSoundPrefab != null)
|
|
Instantiate(explosionSoundPrefab, transform.position, Quaternion.identity);
|
|
if (explosionVfxPrefab != null)
|
|
Instantiate(explosionVfxPrefab, transform.position, Quaternion.identity);
|
|
|
|
DestroyProjectile();
|
|
}
|
|
|
|
public virtual void DestroyProjectile()
|
|
{
|
|
damager.OnHit -= DestroyProjectile;
|
|
|
|
if (lifetimeVFX != null)
|
|
{
|
|
lifetimeVFX.transform.parent = null;
|
|
lifetimeVFX.Stop();
|
|
Destroy(lifetimeVFX.gameObject, 2);
|
|
}
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
DestroyProjectile();
|
|
}
|
|
} |