- 实现地图的生成、游戏关卡的管理、以及一些简单的GUI的显示与关闭
- using UnityEngine;
- using System; //为了使用其中的Serializable
- using System.Collections.Generic;
- using Random = UnityEngine.Random;
- public class BoardMannager : MonoBehaviour {
- [Serializable]//序列化,<span style="font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; line-height: 25.2px;"><span style="font-size:10px;">使得变量可以被Inspector界面获得</span></span>
- public class Count //之后用于存储随机生成最小数量和最大数量的值
- {
- public int minimum;
- public int maximum;
- public Count(int min,int max)
- {
- minimum = min;
- maximum = max;
- }
- }
- public int columns = 8;
- public int rows = 8;
- public Count wallCount = new Count(5, 9);
- public Count foodCount = new Count(1, 5);
- public GameObject exit;
- public GameObject[] floorTiles;
- public GameObject[] wallTiles;
- public GameObject[] foodTiles;
- public GameObject[] enemyTiles;
- public GameObject[] outerWallTiles;
- private Transform boardHolder;//将所有通过代码生成的对象整合在boardHolder中
- private List<Vector3> gridPositions = new List<Vector3>();//存储网格位置信息
- void InitialiseList()
- {
- gridPositions.Clear();
- for (int x = 1; x < columns-1; x++)
- {
- for (int y = 1; y < rows-1; y++)
- {
- gridPositions.Add(new Vector3(x, y, 0f));//通过二重循环获得整个网格的位置 数组.Add(添加的东西)
- }
- }
- }
- void BoardSetup()//生成外墙和场景内的地面
- {
- boardHolder = new GameObject("Board").transform;
- for (int x = -1; x < columns + 1; x++)
- {
- for (int y = -1; y < rows + 1; y++)
- {
- GameObject toInstantiate = floorTiles[Random.Range(0,floorTiles.Length)]; //??????
- if (x==-1|| x==columns||y==-1||y==rows)//判断是否是外墙位置
- {
- toInstantiate=outerWallTiles[Random.Range(0,outerWallTiles.Length)];
- }
- GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;//prefab实例化
- instance.transform.SetParent(boardHolder); //将实例化的对象放到board中
- }
- }
- }
- Vector3 RandomPosition() //随机获取一个地图内的位置
- {
- int randomIndex = Random.Range(0, gridPositions.Count);
- Vector3 randomPosition = gridPositions[randomIndex];
- gridPositions.RemoveAt(randomIndex);
- return randomPosition;
- }
- void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maxmum) //将物品在地图中实例化
- {
- int objectCount = Random.Range(minimum,maxmum+1);
- for (int i = 0; i < objectCount; i++)
- {
- Vector3 randomPosition = RandomPosition();
- GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
- Instantiate(tileChoice,randomPosition,Quaternion.identity);
- }
- }
- public void SetupScene(int level) //调用之前的函数,完成一张地图的生成
- {
- BoardSetup();
- InitialiseList();
- LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
- LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum) ;
- int enemyCount = (int)Mathf.Log(level,2f);
- LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
- Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);
- }
- }
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- public class GameManager : MonoBehaviour {
- //GameManager脚本主要用于管理整个的逻辑运行
- public float levelStartDelay = 2f;
- public float turnDelay = .1f;
- public static GameManager instance = null;//单例设计,保证在游戏运行中永远只有一个GameManager
- public BoardMannager boardScript;
- public int platerFoodPoints = 100;
- [HideInInspector]public bool playersTurn = true;
- private Text levelText;
- private GameObject levelImage;
- private int level = 1;
- private List<Enemy> enemies;
- private bool enemiesMoving;
- private bool doingSetup;
- void Awake() {
- if (instance==null)
- {
- instance = this;
- }
- else if (instance!=null)
- Destroy(gameObject);//单例设计部分
- DontDestroyOnLoad(gameObject);
- enemies=new List<Enemy>();
- boardScript=GetComponent<BoardMannager>();//获取boardMannager脚本
- InitGame();
- }
- private void OnLevelWasLoaded(int index)
- {
- level++;
- InitGame();
- }
- void InitGame() {
- doingSetup = true;
- levelImage = GameObject.Find("LevelImage");
- levelText = GameObject.Find("LevelText").GetComponent<Text>();
- levelText.text = "Day " + level;
- levelImage.SetActive(true);
- Invoke("HideLevelImage", levelStartDelay); //UI部分,显示第几日、显示黑色的背景等等
- enemies.Clear();
- boardScript.SetupScene(level); //调用BoardMannager生成新地图
- }
- private void HideLevelImage()
- {
- levelImage.SetActive(false);
- doingSetup = false;
- }
- IEnumerator MoveEnemies( )
- {
- enemiesMoving = true;
- yield return new WaitForSeconds(turnDelay);
- if (enemies.Count==0)
- {
- yield return new WaitForSeconds(turnDelay);
- }
- for (int i = 0; i < enemies.Count; i++)
- {
- enemies[i].MoveEnemy();
- yield return new WaitForSeconds(enemies[i].moveTime);
- }
- playersTurn = true;
- enemiesMoving = false;
- }
- public void AddEnemyToList(Enemy script)
- {
- enemies.Add(script);
- }
- public void GameOver()
- {
- levelText.text = "After " + level + " days,you starved.";
- levelImage.SetActive(true);
- enabled = false; //游戏结束时显示的UI
- }
- void Update () {
- if (playersTurn || enemiesMoving||doingSetup)
- return;
- StartCoroutine(MoveEnemies());
- }
- }