Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / Systems / RandomGeneration / ProgressController.cs
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ProgressController : MonoBehaviour
{
    // Start is called before the first frame update
    [Header("Progress and enemies")]
    [HideInInspector]
    public List<IUpdatable> updatables = new List<IUpdatable>();
    public int totalProgress = 0;
    public int currentProgress = 0;
    [Tooltip("If this variable is true, all the updatables of this script will be added to the update manager when the game starts. Only true if it's a start room.")]
    [SerializeField] private bool startRoom;
    private UpdateManager updateManager;
    private bool roomEnded = false;
    private bool roomStarted = false;

    private RoomInformation roomInfo;
    private RoomSpawner roomSpawner;

    public void StartMe()
    {
        roomSpawner = GameObject.Find("LevelManager").GetComponent<RoomSpawner>();
        updateManager = GameObject.Find("UpdateManager").GetComponent<UpdateManager>();
        roomInfo = GetComponent<IRoomCreationHandler>().GetRoomInfo();
        if (startRoom)
        {
            StartRoom();
        }
    }
    public void UpdateProgress()
    {
        currentProgress++;
    }
    
    public void StartRoom()
    {
        roomStarted= true;
        updateManager.updatables = updatables;
        foreach (ExitScript e in roomInfo.exits)
        {
            e.ToggleExit();
        }
    }
    public void AddToUpdatables(GameObject go)
    {
        IUpdatable[] upd = go.GetComponents<IUpdatable>();
        foreach(IUpdatable u in upd)
        {
            updatables.Add(u);
        }
    }
    private void Update()
    {
        if (roomStarted)
        {
            if (currentProgress >= totalProgress && !roomEnded)
            {
                List<string> list = roomSpawner.WhereCanIOpen(roomInfo.coords);
                foreach (ExitScript e in roomInfo.exits)
                {
                    foreach (string s in list)
                    {
                        if (e.gameObject.transform.parent.name == s)
                        Destroy(e.gameObject);
                    }
                    roomEnded = true;
                }
            }
        }
    }
}