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

public class RepairableGravity : MonoBehaviour
{


    public static float Integrity = 100;
    public int IntIntegrity = Mathf.RoundToInt(Integrity);
    public GameObject Player;
    bool Fixing = false;

    public GameObject TextDisplay;


    // Start is called before the first frame update
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.F))
        {
            Fixing = true;
        }
        if (Input.GetKeyUp(KeyCode.F))
        {
            Fixing = false;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        bool IsCloseEnough = CheckCloseToTag("Player", 7);

        if (IsCloseEnough)
        {
            if (Fixing)
            {
                if (IntIntegrity < 100)
                {
                    
                    Integrity += 10 * Time.deltaTime;
                    IntIntegrity = Mathf.RoundToInt(Integrity);
                }
            }
        }
        if (Integrity > 0)
        {
            Integrity -= Time.deltaTime/2;
            IntIntegrity = Mathf.RoundToInt(Integrity);
        }

        TextDisplay.GetComponent<TextMesh>().text = IntIntegrity + "%";
    }

    bool CheckCloseToTag(string tag, float minimumDistance)
    {
        GameObject[] goWithTag = GameObject.FindGameObjectsWithTag(tag);

        for (int i = 0; i < goWithTag.Length; ++i)
        {
            if (Vector3.Distance(transform.position, goWithTag[i].transform.position) <= minimumDistance)
                return true;
        }

        return false;

    }
}
