52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Events;
|
|
using System;
|
|
|
|
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
|
|
public class Damager : MonoBehaviour
|
|
{
|
|
[Header("Эффекты")]
|
|
[SerializeField] private List<GameObject> ImpactEffects;
|
|
|
|
//[Header("События")]
|
|
public event Action OnHit;
|
|
|
|
// Динамически установленные значения
|
|
private GameObject owner;
|
|
private float damage;
|
|
|
|
public GameObject Owner => owner;
|
|
|
|
public void Initialize(GameObject owner, float damage)
|
|
{
|
|
this.owner = owner;
|
|
this.damage = damage;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (owner != null && other.gameObject == owner) return;
|
|
|
|
if (other.TryGetComponent<Hitbox>(out var hitbox))
|
|
{
|
|
if (hitbox.Owner == owner) return;
|
|
|
|
hitbox.TakeDamage(damage, transform.position);
|
|
//OnHit?.Invoke(hitbox.gameObject);
|
|
OnHit?.Invoke();
|
|
|
|
SpawnImpactEffects();
|
|
}
|
|
}
|
|
|
|
private void SpawnImpactEffects()
|
|
{
|
|
if (ImpactEffects == null || ImpactEffects.Count == 0) return;
|
|
|
|
foreach (var effect in ImpactEffects)
|
|
{
|
|
Instantiate(effect, transform.position, transform.rotation);
|
|
}
|
|
}
|
|
} |