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

public class SpawnScanner : MonoBehaviour
{
    // Prefabs
    [SerializeField] private GameObject platform;
    [SerializeField] private GameObject enemy;
    public bool isAllowedToSpawn = true;

    // Platform Spawner
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("Ground") && isAllowedToSpawn)
        {
            Spawn();
        }
    }

    private void Spawn()
    {
        float height = Random.Range(-2f, -1f);
        Vector3 positionPLat = new Vector3(12f, height, 0f);
        GameObject newPlatform = Instantiate(platform, positionPLat, Quaternion.identity);
        int würfel = Random.Range(1, 4);
        if (würfel == 3)
        {
            SpawnEnemy(newPlatform);
        }
    }

    // Enemy spawns on platform
    private void SpawnEnemy(GameObject platformAdress)
    {
        Vector3 enemyPosition = platformAdress.transform.position + new Vector3(0f, 1f, 0f);
        GameObject newEnemy = Instantiate(enemy, enemyPosition, Quaternion.identity);

        // Enemy moves along the platform
        newEnemy.transform.parent = platformAdress.transform;
    }

    public void stopSpawn()
    {
        isAllowedToSpawn = false;
    }
}
