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

public class WeaponScript : MonoBehaviour
{

    public GameObject spawnpoint;
    public GameObject bullet;

    public DataBullet dataBullet;

    private int ammo;
    private bool isReloading = false;
    private float time = 0;

    public float reloadTime;

    public void reload(){
        if(ammo == 0){
            isReloading = true;
        }
        if(isReloading){
            time += Time.deltaTime;
            if(time > reloadTime){
                ammo = dataBullet.ammo;
                time = 0;
                isReloading = false;
            }
        }
    }
    public void shoot(){
        if(ammo != 0){
        if(Input.GetKeyDown(KeyCode.Mouse0)){
            Instantiate(bullet,spawnpoint.transform.position,transform.rotation);
            ammo--;
        } 
        }else{
            reload();
        }

        
    }
    void Start()
    {
        dataBullet = GetComponentInChildren<DataBullet>();
        ammo = dataBullet.ammo;
    }

    // Update is called once per frame
    void Update()
    {
        
        shoot();
    }
}
