Advertisement
gundambison

gameManager (rogue)

Feb 22nd, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6.  
  7. public class GameManager : MonoBehaviour
  8. {
  9.     public float levelStartDelay = 2.2f;
  10.     public float turnDelay = .1f;
  11.    
  12.     public static GameManager instance = null;
  13.     public BoardManager boardScript;
  14.    
  15.     public int playerFoodPoints = 100;
  16.     [HideInInspector] public bool playersTurn = true;
  17.    
  18.     private int level = 1  ;
  19.     private List<Enemy> enemies;
  20.     private bool        enemiesMoving;
  21.     private Text levelText;
  22.     private GameObject levelImage;
  23.  
  24.     private bool doingSetup;
  25.    
  26.  
  27.     // Start is called before the first frame update
  28.     void Awake()
  29.     {
  30.         Debug.Log("GM: Awake"  );
  31.         if ( instance == null ){
  32.             instance = this;
  33.         }else if( instance != this){
  34.             Destroy(gameObject);
  35.         }
  36.        
  37.         DontDestroyOnLoad(gameObject);
  38.        
  39.         enemies = new List<Enemy>();
  40.         boardScript = GetComponent<BoardManager>();
  41.         InitGame();
  42.        
  43.     }
  44.  
  45.     private void OnLevelWasLoaded( int index)
  46.     {
  47.         level++;
  48.         Debug.Log("GM: OnLevelWasLoaded" + level);
  49.         InitGame();
  50.  
  51.     }
  52.     void InitGame()
  53.     {
  54.         doingSetup = true;
  55.  
  56.         levelImage = GameObject.Find("LevelImage");
  57.         levelText = GameObject.Find("LevelText").GetComponent<Text>();
  58.         levelText.text = "Day " + level;
  59.         levelImage.SetActive(true);
  60.         Invoke("HideLevelImage", levelStartDelay);
  61.         enemies.Clear();
  62.         boardScript.SetupScene(level);
  63.     }
  64.  
  65.     private void HideLevelImage()
  66.     {
  67.         levelImage.SetActive(false);
  68.         doingSetup = false;
  69.     }
  70.    
  71.     public void GameOver()
  72.     {
  73.         levelText.text = "After " + level + " days, you starved.";
  74.         levelImage.SetActive(true);
  75.  
  76.         enabled = false;
  77.     }
  78.    
  79.     IEnumerator MoveEnemies()
  80.     {
  81.         enemiesMoving= true;
  82.         yield return new WaitForSeconds(turnDelay);
  83.        
  84.         if( enemies.Count == 0){
  85.             yield return new WaitForSeconds(turnDelay);
  86.         }
  87.        
  88.         for(int i=0;i< enemies.Count ; i++){
  89.             enemies[i].MoveEnemy();
  90.             yield return new WaitForSeconds(enemies[i].moveTime);
  91.         }
  92.        
  93.         playersTurn = true;
  94.         enemiesMoving = false;
  95.     }
  96.    
  97.     // Update is called once per frame
  98.     void Update()
  99.     {
  100.         if(playersTurn || enemiesMoving || doingSetup)
  101.             return ;
  102.             //https://docs.unity3d.com/530/Documentation/ScriptReference/Application.Quit.html
  103.             if (Input.GetKey("escape")){
  104.                 Application.Quit();
  105.             }
  106.  
  107.  
  108.         StartCoroutine(MoveEnemies());
  109.     }
  110.    
  111.     public void AddEnemyToList(Enemy script){
  112.         enemies.Add(script);
  113.        
  114.     }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement