GameManager

mail@pastecode.io avatar
unknown
csharp
3 years ago
8.1 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Cinemachine;

public class GameManager : MonoBehaviour
{
    //Singleton
    public static GameManager instance;

    [Header("Player Variables")]
    public GameObject player;
    public int playerHealth;
    public int temp;
    public bool DoWakeUpAnim = true;
    
    [HideInInspector] public bool playerRunningRight;
    [HideInInspector] public bool playerRunningLeft;
    [HideInInspector] public bool playerGoingUp;
    [HideInInspector] public bool restartInCheckpoint = false;

    [Header("Unlocked Skills")]
     public bool dash;
     public bool iai;
     public bool shadow;
    public bool smokeBomb;
    public bool grapplingHook;

    [Header("SceneChange Variables")]
    public float timeOfMovingAloneWhenArrivedScene;
    public float forceToGetShootOutOfAHole;
    [SerializeField] public GameObject changeSceneIcon;
    [HideInInspector] public bool enteringScene;
    [HideInInspector] public bool exitingDoor;
    [HideInInspector] public int entryNum;
    [HideInInspector] public int checkPointNum;
    [HideInInspector] public int currentScene;
    [HideInInspector] public GameObject startPosition;
    [HideInInspector] public GameObject[] entryPoint;
    [HideInInspector] public GameObject[] checkPoints;
    AsyncOperation oper;


    
    bool changeSolicited;
    public bool screenBlack = false;
    int sceneIndexToChangeAt;


    private void Awake()
    {
        //Set Skills
        //dash = ES3.Load("SkillBookDash", false);
        //iai = ES3.Load("SkillBookIai", false);
        //shadow = ES3.Load("SkillBookShadow", false);
        //smokeBomb = ES3.Load("SkillBookSmokeBomb", false);
        //grapplingHook = ES3.Load("SkillsBookGrapplingGun", false);

        //Cursor isn't visible
        Cursor.visible = false;

        //Creates an array of points.
        entryPoint = new GameObject[20];
        checkPoints = new GameObject[20];

        //Dont destroy the game object between scenes.
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(instance);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        playerHealth = scrPlayerDamageManager.instance.heartAmount;
        temp = playerHealth;

        if (DoWakeUpAnim)
        {
            //Play wakeup animation for the player.
            scrPlayerController2D.instance.hasBeenWakenUp = false;
            scrPlayerController2D.instance.cantMove = true;
            scrPlayerDash.instance.cantDash = true;
            scrPlayerAnimationHandler.instance.ChangeAnimation(scrPlayerAnimationHandler.PLAYER_WAKEUP);
            StartCoroutine(scrPlayerController2D.instance.CantMoveTimer(/*Animation duration, handmade cause i don't want to program*/4.3f));
        }
    }

    public void ResetScene()
    {
        //Reset the scene to the last checkpoint.
        restartInCheckpoint = true;
        //Sets life to 3 (the life counter).
        playerHealth = scrPlayerDamageManager.instance.heartAmount;
        //Resets with the actual function.
        StartCoroutine(LoadSceneAsyncronously(SceneManager.GetActiveScene().buildIndex));
    }

    //Change Scene.
    public void ChangeScene(int sceneIndex)
    {
        scrPlayerScreenBlackFX.instance.changingScene = true;
        changeSolicited = true;
        sceneIndexToChangeAt = sceneIndex;
    }

    private void Update()
    {
        if(temp != playerHealth) { print("playerHealthChanged"); temp = playerHealth; }

        IconWhileLoading();

        if (currentScene != SceneManager.GetActiveScene().buildIndex) { currentScene = SceneManager.GetActiveScene().buildIndex; }

        if(player == null) { player = GameObject.Find("PlayerCharacter"); }

        if (playerHealth < 0) { playerHealth = 0; }
        if (playerHealth > scrPlayerDamageManager.instance.heartAmount) { playerHealth = scrPlayerDamageManager.instance.heartAmount; }

        if (!scrPlayerScreenBlackFX.instance.changingScene && changeSolicited)
        {
            StartCoroutine(LoadSceneAsyncronously(sceneIndexToChangeAt));
            screenBlack = true;
            changeSolicited = false;
        }

        if (oper != null)
        {
            if (oper.isDone)
            {
                screenBlack = false;

                //Spawn at the entrance or the checkPoint.
                if (entryPoint[entryNum] != null && !restartInCheckpoint)
                {
                    startPosition.transform.position = entryPoint[entryNum].transform.position;
                }
                else if (restartInCheckpoint && checkPoints[checkPointNum] != null)
                {
                    startPosition.transform.position = checkPoints[checkPointNum].transform.position;
                    restartInCheckpoint = false;
                }

                //Actually set the player position.
                player.transform.position = startPosition.transform.position;

                //If it has to, do whatever it should (player running etc...).
                if (playerRunningLeft)
                {
                    PlayerRunLeft();
                }
                else if (playerRunningRight)
                {
                    PlayerRunRight();
                }
                else
                {
                    NoMovement();
                }

                if (playerGoingUp)
                {
                    PlayerGoingUp();
                    playerGoingUp = false;
                }

                oper = null;
            }
        }
    }

    void IconWhileLoading()
    {
        if (screenBlack) { changeSceneIcon.SetActive(true); }
        else { changeSceneIcon.SetActive(false); }
    }


    void PlayerRunLeft()
    {
        enteringScene = true;
        scrPlayerController2D.instance.cantMove = true;
        scrPlayerController2D.instance.canMoveHorizontally = true;
        scrPlayerController2D.instance.horizontalMovement = -1;
        scrPlayerDash.instance.cantDash = true;
        StartCoroutine(WaitForSceneArrival());
        playerRunningLeft = playerRunningRight = false;
    }

    void PlayerRunRight()
    {
        enteringScene = true;
        scrPlayerController2D.instance.cantMove = true;
        scrPlayerController2D.instance.canMoveHorizontally = true;
        scrPlayerController2D.instance.horizontalMovement = 1;
        scrPlayerDash.instance.cantDash = true;
        StartCoroutine(WaitForSceneArrival());
        playerRunningLeft = playerRunningRight = false;
    }

    void NoMovement()
    {
        enteringScene = true;
        scrPlayerController2D.instance.cantMove = true;
        scrPlayerController2D.instance.canMoveHorizontally = true;
        scrPlayerDash.instance.cantDash = true;
        StartCoroutine(WaitForSceneArrival());
    }

    void PlayerGoingUp()
    {
        Vector2 Angle = new Vector2(playerRunningRight ? 3 : -3, 1);
        Angle.Normalize();
        scrPlayerController2D.instance.rb.AddForce(Angle * forceToGetShootOutOfAHole * Time.fixedDeltaTime, ForceMode2D.Impulse);
    }

    IEnumerator WaitForSceneArrival()
    {
        yield return new WaitForSeconds(timeOfMovingAloneWhenArrivedScene);
        enteringScene = false;
        scrPlayerController2D.instance.horizontalMovement = 0;
        scrPlayerController2D.instance.cantMove = false;
        scrPlayerController2D.instance.canMoveHorizontally = false;
        scrPlayerDash.instance.cantDash = false;

    }

    IEnumerator LoadSceneAsyncronously(int sceneIndex)
    {
        oper = SceneManager.LoadSceneAsync(sceneIndex);

        while (!oper.isDone)
        {
            yield return null;
        }
    }
}