using UnityEngine;
using System.Collections;

public class ShootMagic : MonoBehaviour {

    public Magic magic;

    public float AttackRate;
    //private float nextAttackTime = 0f;
    private float lastFireTime = -1f;

    ForceMovement Move;

    void Start() 
    {
        Move = GetComponent<ForceMovement>();
    }

    void Update()
    {
        if (Input.GetButtonDown("Trigger1"))
        {
            StartCoroutine(FireMagic());
        }
    }

    IEnumerator FireMagic()
    {
        if (Time.time > lastFireTime + 1 / AttackRate)
        {
            ConstantForce2D magicFroce = ((GameObject)Instantiate(magic.gameObject,
                                                                     magic.getSpawnPoint(this.transform) + Vector2.right * ((Move.facingRight) ? 1 : -1),
                                                                     magic.transform.rotation)).GetComponent<ConstantForce2D>();
            magicFroce.force = new Vector2((Move.facingRight) ? 10 : -10,0 );
            lastFireTime = Time.time;
        }
           
        yield return null;
    }

}
