5

  1. 实现地图的生成、游戏关卡的管理、以及一些简单的GUI的显示与关闭
  2. using UnityEngine;  
  3. using System; //为了使用其中的Serializable  
  4. using System.Collections.Generic;  
  5. using Random = UnityEngine.Random;  
  6.   
  7. public class BoardMannager : MonoBehaviour {  
  8.   
  9.     [Serializable]//序列化,<span style="font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; line-height: 25.2px;"><span style="font-size:10px;">使得变量可以被Inspector界面获得</span></span>  
  10.     public class Count //之后用于存储随机生成最小数量和最大数量的值  
  11.     {  
  12.         public int minimum;  
  13.         public int maximum;  
  14.         public Count(int min,int max)  
  15.         {  
  16.             minimum = min;  
  17.             maximum = max;  
  18.         }  
  19.     }  
  20.   
  21.     public int columns = 8;  
  22.     public int rows = 8;  
  23.     public Count wallCount = new Count(5, 9);  
  24.     public Count foodCount = new Count(1, 5);  
  25.     public GameObject exit;  
  26.     public GameObject[] floorTiles;  
  27.     public GameObject[] wallTiles;  
  28.     public GameObject[] foodTiles;  
  29.     public GameObject[] enemyTiles;  
  30.     public GameObject[] outerWallTiles;  
  31.   
  32.     private Transform boardHolder;//将所有通过代码生成的对象整合在boardHolder中  
  33.     private List<Vector3> gridPositions = new List<Vector3>();//存储网格位置信息  
  34.   
  35.     void InitialiseList()  
  36.     {  
  37.         gridPositions.Clear();  
  38.   
  39.         for (int x = 1; x < columns-1; x++)  
  40.         {  
  41.             for (int y = 1; y < rows-1; y++)  
  42.             {  
  43.                 gridPositions.Add(new Vector3(x, y, 0f));//通过二重循环获得整个网格的位置      数组.Add(添加的东西)
  44.             }  
  45.         }  
  46.     }  
  47.   
  48.     void BoardSetup()//生成外墙和场景内的地面  
  49.     {  
  50.         boardHolder = new GameObject("Board").transform;  
  51.         for (int x = -1; x < columns + 1; x++)  
  52.         {  
  53.             for (int y = -1; y < rows + 1; y++)  
  54.             {  
  55.                 GameObject toInstantiate = floorTiles[Random.Range(0,floorTiles.Length)];  //??????
  56.                 if (x==-1|| x==columns||y==-1||y==rows)//判断是否是外墙位置  
  57.                 {  
  58.                     toInstantiate=outerWallTiles[Random.Range(0,outerWallTiles.Length)];  
  59.                 }  
  60.                 GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;//prefab实例化  
  61.                 instance.transform.SetParent(boardHolder); //将实例化的对象放到board中  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     Vector3 RandomPosition() //随机获取一个地图内的位置  
  67.     {  
  68.         int randomIndex = Random.Range(0, gridPositions.Count);  
  69.         Vector3 randomPosition = gridPositions[randomIndex];  
  70.         gridPositions.RemoveAt(randomIndex);  
  71.         return randomPosition;  
  72.     }  
  73.   
  74.   
  75.     void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maxmum)  //将物品在地图中实例化  
  76.     {  
  77.         int objectCount = Random.Range(minimum,maxmum+1);  
  78.   
  79.         for (int i = 0; i < objectCount; i++)  
  80.         {  
  81.             Vector3 randomPosition = RandomPosition();  
  82.             GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];  
  83.             Instantiate(tileChoice,randomPosition,Quaternion.identity);  
  84.         }  
  85.     }  
  86.   
  87.     public void SetupScene(int level) //调用之前的函数,完成一张地图的生成  
  88.     {  
  89.         BoardSetup();  
  90.         InitialiseList();  
  91.         LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);  
  92.         LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum) ;  
  93.         int enemyCount = (int)Mathf.Log(level,2f);  
  94.         LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);  
  95.         Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);  
  96.           
  97.     }  
  98. }  
[csharp] view plain copy
 
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using UnityEngine.UI;  
  5.   
  6.   
  7. public class GameManager : MonoBehaviour {  
  8. //GameManager脚本主要用于管理整个的逻辑运行  
  9.     public float levelStartDelay = 2f;  
  10.     public float turnDelay = .1f;  
  11.     public static GameManager instance = null;//单例设计,保证在游戏运行中永远只有一个GameManager  
  12.     public BoardMannager boardScript;  
  13.     public int platerFoodPoints = 100;  
  14.     [HideInInspector]public bool playersTurn = true;  
  15.   
  16.     private Text levelText;  
  17.     private GameObject levelImage;  
  18.     private int level = 1;  
  19.     private List<Enemy> enemies;  
  20.     private bool enemiesMoving;  
  21.     private bool doingSetup;  
  22.   
  23.   
  24.     void Awake() {  
  25.         if (instance==null)  
  26.         {  
  27.             instance = this;  
  28.         }  
  29.         else if (instance!=null)  
  30.                 Destroy(gameObject);//单例设计部分  
  31.         DontDestroyOnLoad(gameObject);  
  32.         enemies=new List<Enemy>();  
  33.         boardScript=GetComponent<BoardMannager>();//获取boardMannager脚本  
  34.         InitGame();      
  35.     }  
  36.   
  37.     private void OnLevelWasLoaded(int index)  
  38.     {  
  39.         level++;  
  40.         InitGame();  
  41.     }  
  42.   
  43.     void InitGame() {  
  44.         doingSetup = true;  
  45.         levelImage = GameObject.Find("LevelImage");  
  46.         levelText = GameObject.Find("LevelText").GetComponent<Text>();  
  47.         levelText.text = "Day " + level;  
  48.         levelImage.SetActive(true);  
  49.         Invoke("HideLevelImage", levelStartDelay); //UI部分,显示第几日、显示黑色的背景等等  
  50.   
  51.         enemies.Clear();  
  52.         boardScript.SetupScene(level); //调用BoardMannager生成新地图  
  53.       
  54.     }  
  55.   
  56.     private void HideLevelImage()  
  57.     {  
  58.         levelImage.SetActive(false);  
  59.         doingSetup = false;  
  60.     }  
  61.     IEnumerator MoveEnemies( )  
  62.     {  
  63.         enemiesMoving = true;  
  64.         yield return new WaitForSeconds(turnDelay);  
  65.         if (enemies.Count==0)  
  66.         {  
  67.             yield return new WaitForSeconds(turnDelay);  
  68.         }  
  69.         for (int i = 0; i < enemies.Count; i++)  
  70.         {  
  71.             enemies[i].MoveEnemy();  
  72.             yield return new WaitForSeconds(enemies[i].moveTime);  
  73.         }  
  74.         playersTurn = true;  
  75.         enemiesMoving = false;  
  76.     }  
  77.   
  78.     public void AddEnemyToList(Enemy script)  
  79.     {  
  80.         enemies.Add(script);  
  81.     }  
  82.   
  83.   
  84.     public void GameOver()  
  85.     {  
  86.         levelText.text = "After " + level + " days,you starved.";  
  87.         levelImage.SetActive(true);  
  88.         enabled = false; //游戏结束时显示的UI  
  89.     }  
  90.       
  91.     void Update () {  
  92.         if (playersTurn || enemiesMoving||doingSetup)  
  93.             return;  
  94.         StartCoroutine(MoveEnemies());  
  95.       
  96.     }  
  97. }  
原文地址:https://www.cnblogs.com/wshyj/p/6096205.html