using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

class WeaponMelee : Weapon
{
    public float Range = 1.0f;
    public float SwingTime = 1.0f;
    public bool Debug = false;
    public LayerMask AttackLayermask; 

    private Vector3 p1;
    private Vector3 p2; 

    public void OnDrawGizmos()
    {
        if(Debug)
        {
            Gizmos.DrawCube((p1 + p2) / 2, p2 - p1); 
        }
    }

    public override void Use(Vector2 aimPos)
    {
        base.Use(aimPos); 
        
        Player.Instance.Animator.SetTrigger("Swing"); 

        Vector2 pos1 = Player.Instance.transform.position;
        pos1.y += 1.0f;
        Vector2 pos2 = Player.Instance.transform.position;
        pos2.y -= 1.0f;
        pos2.x += Player.Instance.Controller.collisions.faceDir * Range;

        p1 = pos1;
        p2 = pos2; 

        Collider2D[] colliders = Physics2D.OverlapAreaAll(pos1, pos2); 
        foreach(Collider2D collider in colliders)
        {
            if(Util.IsInLayerMask(collider.gameObject.layer, AttackLayermask))
            {
                Entity e = collider.gameObject.GetComponent<Entity>(); 
                if(e != null && e != this.Owner)
                {
                    e.Harm(this.Damage, this.Owner, this.Owner.transform.position); 
                }
            }
        }
    }
}
