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

public class HpManagerAndRespawner : MonoBehaviour
{
    [SerializeField] GameObject player;
    [SerializeField] GameObject platform;
    [SerializeField] int hp = 3;
    [SerializeField] GameObject Hud;
    [SerializeField] GameObject heart;
    GameObject currentPlayer;
    GameObject[] hearts;
    // Start is called before the first frame update
    void Start()
    {
        hearts = new GameObject[hp];
        SpawnPlayerAndSetHP();
        SpawnHearts();
    }

    void SpawnHearts()
    {
        float offsetX = 0;
        //add hud stuff later
        for (int i = 0; i < hp; i++)
        {
            GameObject newHeart = Instantiate(heart, Hud.transform);
            newHeart.transform.position += new Vector3(offsetX, 0);
            offsetX += 50;
            hearts[i] = newHeart;
        }
    }
    void SpawnPlayerAndSetHP()
    {
        Vector2 spawnLoc = new Vector2(0, 0);
        
        
        Instantiate(platform, new Vector2(0, -0), Quaternion.identity);
        currentPlayer = Instantiate(player, new Vector2(0, -0), Quaternion.identity);
    }

    void SpawnNewPlayerAndCheckHP()
    {
        hp -= 1;
        Destroy(hearts[hp]);
        if (hp > 0) {
            SpawnPlayerAndSetHP();
            
        }
        else
        {
            SceneManager.LoadScene("GameOver");
        }
    }

    

    // Update is called once per frame
    void Update()
    {
        if (currentPlayer == null && hp > 0) SpawnNewPlayerAndCheckHP();
    }
}
