2dRogueLike源码分析GameManager(2)
2018-02-11 20:26
281 查看
1.调用unity包using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Allows us to use Lists.
using UnityEngine.UI; //Allows us to use UI.
2.levelStartDelay:开始level之前的延迟
turnDelay:每回合的delay
playerFoodPoints:初始化玩家的food数
instance:单例
playersTurn:是否是player的回合
levelText:UI的text
levelImage:切换level的图片
boardScipt:地图的脚本
level:当前level
enemies:敌人list
enemiesMoving:enemy是否在移动
doingSetup:防止player在setting时移动 public class GameManager : MonoBehaviour
{
public float levelStartDelay = 2f; //Time to wait before starting level, in seconds.
public float turnDelay = 0.1f; //Delay between each Player turn.
public int playerFoodPoints = 100; //Starting value for Player food points.
public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script.
[HideInInspector] public bool playersTurn = true; //Boolean to check if it's players turn, hidden in inspector but public.
private Text levelText; //Text to display current level number.
private GameObject levelImage; //Image to block out level as levels are being set up, background for levelText.
private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level.
private int level = 1; //Current level number, expressed in game as "Day 1".
private List<Enemy> enemies; //List of all Enemy units, used to issue them move commands.
private bool enemiesMoving; //Boolean to check if enemies are moving.
private bool doingSetup = true; //Boolean to check if we're setting up board, prevent Player from moving during setup.
3.awake与之前相同 //Awake is always called before any Start functions
void Awake()
{
//Check if instance already exists
if (instance == null)
//if not, set instance to this
instance = this;
//If instance already exists and it's not this:
else if (instance != this)
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
//Assign enemies to a new List of Enemy objects.
enemies = new List<Enemy>();
//Get a component reference to the attached BoardManager script
boardScript = GetComponent<BoardManager>();
//Call the InitGame function to initialize the first level
InitGame();
}4.每次level加载时调用 //This is called each time a scene is loaded.
void OnLevelWasLoaded(int index)
{
//Add one to our level number.
level++;
//Call InitGame to initialize our level.
InitGame();
}5.找到levelimage显示,修改levelText,
延迟后隐藏levelImage
清空enemies list
重新设置地图 //Initializes the game for each level.
void InitGame()
{
//While doingSetup is true the player can't move, prevent player from moving while title card is up.
doingSetup = true;
//Get a reference to our image LevelImage by finding it by name.
levelImage = GameObject.Find("LevelImage");
//Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
levelText = GameObject.Find("LevelText").GetComponent<Text>();
//Set the text of levelText to the string "Day" and append the current level number.
levelText.text = "Day " + level;
//Set levelImage to active blocking player's view of the game board during setup.
levelImage.SetActive(true);
//Call the HideLevelImage function with a delay in seconds of levelStartDelay.
Invoke("HideLevelImage", levelStartDelay);
//Clear any Enemy objects in our List to prepare for next level.
enemies.Clear();
//Call the SetupScene function of the BoardManager script, pass it current level number.
boardScript.SetupScene(level);
}6.隐藏levelImage //Hides black image used between levels
void HideLevelImage()
{
//Disable the levelImage gameObject.
levelImage.SetActive(false);
//Set doingSetup to false allowing player to move again.
doingSetup = false;
}7.MoveEnemies(player回合,敌人在移动或在设置中则什么都不做) //Update is called every frame.
void Update()
{
//Check that playersTurn or enemiesMoving or doingSetup are not currently true.
if(playersTurn || enemiesMoving || doingSetup)
//If any of these are true, return and do not start MoveEnemies.
return;
//Start moving enemies.
StartCoroutine (MoveEnemies ());
}8.加入enemy //Call this to add the passed in Enemy to the List of Enemy objects.
public void AddEnemyToList(Enemy script)
{
//Add Enemy to List enemies.
enemies.Add(script);
}9.设置GameOver时的UI背景,禁用当前gamemanager //GameOver is called when the player reaches 0 food points
public void GameOver()
{
//Set levelText to display number of levels passed and game over message
levelText.text = "After " + level + " days, you starved.";
//Enable black background image gameObject.
levelImage.SetActive(true);
//Disable this GameManager.
enabled = false;
}10.协程enemy移动函数
先等一个turndelay,
如果enemy数量是0则再等一个turnDelay
不然,等所有的enemy move完
设置playerturn=True
设置enemyMoving=False //Coroutine to move enemies in sequence.
IEnumerator MoveEnemies()
{
//While enemiesMoving is true player is unable to move.
enemiesMoving = true;
//Wait for turnDelay seconds, defaults to .1 (100 ms).
yield return new WaitForSeconds(turnDelay);
//If there are no enemies spawned (IE in first level):
if (enemies.Count == 0)
{
//Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.
yield return new WaitForSeconds(turnDelay);
}
//Loop through List of Enemy objects.
for (int i = 0; i < enemies.Count; i++)
{
//Call the MoveEnemy function of Enemy at index i in the enemies List.
enemies[i].MoveEnemy ();
//Wait for Enemy's moveTime before moving next Enemy,
yield return new WaitForSeconds(enemies[i].moveTime);
}
//Once Enemies are done moving, set playersTurn to true so player can move.
playersTurn = true;
//Enemies are done moving, set enemiesMoving to false.
enemiesMoving = false;
}
}
using System.Collections;
using System.Collections.Generic; //Allows us to use Lists.
using UnityEngine.UI; //Allows us to use UI.
2.levelStartDelay:开始level之前的延迟
turnDelay:每回合的delay
playerFoodPoints:初始化玩家的food数
instance:单例
playersTurn:是否是player的回合
levelText:UI的text
levelImage:切换level的图片
boardScipt:地图的脚本
level:当前level
enemies:敌人list
enemiesMoving:enemy是否在移动
doingSetup:防止player在setting时移动 public class GameManager : MonoBehaviour
{
public float levelStartDelay = 2f; //Time to wait before starting level, in seconds.
public float turnDelay = 0.1f; //Delay between each Player turn.
public int playerFoodPoints = 100; //Starting value for Player food points.
public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script.
[HideInInspector] public bool playersTurn = true; //Boolean to check if it's players turn, hidden in inspector but public.
private Text levelText; //Text to display current level number.
private GameObject levelImage; //Image to block out level as levels are being set up, background for levelText.
private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level.
private int level = 1; //Current level number, expressed in game as "Day 1".
private List<Enemy> enemies; //List of all Enemy units, used to issue them move commands.
private bool enemiesMoving; //Boolean to check if enemies are moving.
private bool doingSetup = true; //Boolean to check if we're setting up board, prevent Player from moving during setup.
3.awake与之前相同 //Awake is always called before any Start functions
void Awake()
{
//Check if instance already exists
if (instance == null)
//if not, set instance to this
instance = this;
//If instance already exists and it's not this:
else if (instance != this)
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
//Assign enemies to a new List of Enemy objects.
enemies = new List<Enemy>();
//Get a component reference to the attached BoardManager script
boardScript = GetComponent<BoardManager>();
//Call the InitGame function to initialize the first level
InitGame();
}4.每次level加载时调用 //This is called each time a scene is loaded.
void OnLevelWasLoaded(int index)
{
//Add one to our level number.
level++;
//Call InitGame to initialize our level.
InitGame();
}5.找到levelimage显示,修改levelText,
延迟后隐藏levelImage
清空enemies list
重新设置地图 //Initializes the game for each level.
void InitGame()
{
//While doingSetup is true the player can't move, prevent player from moving while title card is up.
doingSetup = true;
//Get a reference to our image LevelImage by finding it by name.
levelImage = GameObject.Find("LevelImage");
//Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
levelText = GameObject.Find("LevelText").GetComponent<Text>();
//Set the text of levelText to the string "Day" and append the current level number.
levelText.text = "Day " + level;
//Set levelImage to active blocking player's view of the game board during setup.
levelImage.SetActive(true);
//Call the HideLevelImage function with a delay in seconds of levelStartDelay.
Invoke("HideLevelImage", levelStartDelay);
//Clear any Enemy objects in our List to prepare for next level.
enemies.Clear();
//Call the SetupScene function of the BoardManager script, pass it current level number.
boardScript.SetupScene(level);
}6.隐藏levelImage //Hides black image used between levels
void HideLevelImage()
{
//Disable the levelImage gameObject.
levelImage.SetActive(false);
//Set doingSetup to false allowing player to move again.
doingSetup = false;
}7.MoveEnemies(player回合,敌人在移动或在设置中则什么都不做) //Update is called every frame.
void Update()
{
//Check that playersTurn or enemiesMoving or doingSetup are not currently true.
if(playersTurn || enemiesMoving || doingSetup)
//If any of these are true, return and do not start MoveEnemies.
return;
//Start moving enemies.
StartCoroutine (MoveEnemies ());
}8.加入enemy //Call this to add the passed in Enemy to the List of Enemy objects.
public void AddEnemyToList(Enemy script)
{
//Add Enemy to List enemies.
enemies.Add(script);
}9.设置GameOver时的UI背景,禁用当前gamemanager //GameOver is called when the player reaches 0 food points
public void GameOver()
{
//Set levelText to display number of levels passed and game over message
levelText.text = "After " + level + " days, you starved.";
//Enable black background image gameObject.
levelImage.SetActive(true);
//Disable this GameManager.
enabled = false;
}10.协程enemy移动函数
先等一个turndelay,
如果enemy数量是0则再等一个turnDelay
不然,等所有的enemy move完
设置playerturn=True
设置enemyMoving=False //Coroutine to move enemies in sequence.
IEnumerator MoveEnemies()
{
//While enemiesMoving is true player is unable to move.
enemiesMoving = true;
//Wait for turnDelay seconds, defaults to .1 (100 ms).
yield return new WaitForSeconds(turnDelay);
//If there are no enemies spawned (IE in first level):
if (enemies.Count == 0)
{
//Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.
yield return new WaitForSeconds(turnDelay);
}
//Loop through List of Enemy objects.
for (int i = 0; i < enemies.Count; i++)
{
//Call the MoveEnemy function of Enemy at index i in the enemies List.
enemies[i].MoveEnemy ();
//Wait for Enemy's moveTime before moving next Enemy,
yield return new WaitForSeconds(enemies[i].moveTime);
}
//Once Enemies are done moving, set playersTurn to true so player can move.
playersTurn = true;
//Enemies are done moving, set enemiesMoving to false.
enemiesMoving = false;
}
}
相关文章推荐
- 2dRogueLike源码分析GameManager
- 2dRogueLike源码分析MovingObject
- 2dRogueLike源码分析Wall
- 2dRogueLike源码分析BoardManager
- 2dRogueLike源码分析SoundManager
- 【Cocos2d-x】源码分析之 2d/ui/UILayout
- ROS源码分析--Costmap_2D分析
- 【Cocos2d-x】源码分析之 2d/ui/Widget
- 【Cocos2d-x】源码分析之 2d/ui/Widget
- 源码分析使用Cocos2d-x实现2D光线效果
- [Cocos2d-x]源码分析之 2d/ui/UILayout
- java.lang.String源码分析(2)
- Redis Jedis 之 JedisSentinelPool 源码分析
- ActiveSupport eager_autoload源码分析
- HDFS源码分析之数据块Block、副本Replica
- Linux4.4 TCP源码分析
- 深入学习Django源码基础16 - django中信号的学习分析
- DHCP源码分析-系统概述
- 仿爱奇艺视频,腾讯视频,搜狐视频首页推荐位轮播图(二)之SuperIndicator源码分析
- Spring源码分析----IOC容器的实现(IoC容器的初始化过程(定位、载入解析、注册))