﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Arugula.Games
{
    public abstract class Projectile : Expirable
    {
        [Header("Projectile")]
        public DamageInfo damageInfo;
        public LayerMask impactMask;
        public LayerMask damageMask;
        public bool destroyOnUsed;
        public bool useOnImpact;
        public event Action<Projectile> OnImpact;
        public event Action<Projectile> OnDamage;
        protected bool used;

        public override void Start()
        {
            base.Start();
            OnExpired += (x) => used = true;
        }

        protected virtual void Use()
        {
            used = true;
            if(destroyOnUsed)
                Destroy(gameObject);
        }

        protected bool IsImpactable(int layer) => impactMask.Contains(layer);
        protected bool IsImpactable(GameObject go) => IsImpactable(go.layer);
        protected bool IsImpactable(Component c) => IsImpactable(c.gameObject);

        protected bool IsDamageable(int layer) => damageMask.Contains(layer);
        protected bool IsDamageable(GameObject go) => IsDamageable(go.layer);
        protected bool IsDamageable(Component c) => IsDamageable(c.gameObject);

        protected void _OnImpact() { if (OnImpact != null) OnImpact(this); }
        protected void _OnDamage() { if (OnDamage != null) OnDamage(this); }
    }
}
