Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
3.1 kB
1
Indexable
Never
using System;
using System.Collections.Generic;
using Gameplay.Attack;
using Gameplay.AttackEffect;
using GameplayModules.Stats;
using Id;
using UnityEngine;

public class BaseAttackInstance : MonoBehaviour
{
    [SerializeField] private float _outDuration;
    [SerializeField] private GameObject[] _activateOnOut;
    [SerializeField] private GameObject[] _deactivateOnOut;
    [SerializeField] protected float HitRadius;
    [SerializeField] protected GameObject DebugHitZone;
    [SerializeField] private BaseAttackEffect[] _baseAttackEffects;

    private SubAttackContainer _subAttackContainer;
    private GameObject _attackEnemyHitVfx;
    private GameObject _attackObstacleHitVfx;
    protected List<uint> EnemyIds = new List<uint>();
    private Dictionary<EStat, float> _statsLevel = new Dictionary<EStat, float>();
    protected bool IsUpdating => !_isDestroying && !_isPaused;

    private bool _isPaused;
    private bool _isDestroying;
    public Action<BaseAttackInstance> OnInstanceDestroyed;
    
    public bool IsPaused
    {
        set => _isPaused = value;
    }

    public Dictionary<EStat, float> StatsLevel => _statsLevel;
    public GameObject AttackEnemyHitVfx => _attackEnemyHitVfx;
    public SubAttackContainer SubAttackContainer => _subAttackContainer;

    public virtual void Initialize(Dictionary<EStat, float> statsLevel, GameObject attackEnemyHitVfx, GameObject obstacleHitVfx, SubAttackContainer subAttackContainer)
    {
        _statsLevel = statsLevel;
        _attackEnemyHitVfx = attackEnemyHitVfx;
        _attackObstacleHitVfx = obstacleHitVfx;
        _subAttackContainer = subAttackContainer;
    }

    protected void ApplyEffect(uint[] enemyId)
    {
        foreach (var baseAttackEffect in _baseAttackEffects)
        {
            baseAttackEffect.ApplyEffect(enemyId, this);
        }
    }
   
    protected void SpawnObstacleHitVfx(Vector3 obstaclePosition)
    {
        if (_attackObstacleHitVfx == null) return;
        
        Transform attackTransform = transform;
        Instantiate(_attackObstacleHitVfx, obstaclePosition, attackTransform.rotation);
    }

    public void AddEnemyHit(uint enemyId)
    {
        EnemyIds.Add(enemyId);
    }

    protected virtual void DetectHit()
    {
        
    }

    public virtual void DestroyInstance()
    {
        _isDestroying = true;
        OnInstanceDestroyed?.Invoke(this);
        if (_outDuration > 0f)
        {
            foreach (var go in _activateOnOut)
            {
                go.SetActive(true);
            }
            
            foreach (var go in _deactivateOnOut)
            {
                go.SetActive(false);
            }
            
            Invoke(nameof(DestroyObject), _outDuration);
        }
        else
        {
            DestroyObject();
        }
    }

    private void DestroyObject()
    {
        if (gameObject != null)
        {
            Destroy(gameObject); //gameobject is already destroyed when you stop the game (sometimes)    
        }
    }
}