iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 25
0
自我挑戰組

我要和天一樣高!!!(Unity 2D手機小遊戲開發日誌)系列 第 25

Day 25: 隨高度增加調整難度

(本篇文章網誌版:http://shineright.blogspot.tw/2016/12/day-25.html)

現在遊戲的敵人只有幾種,非常單調,於是,今天要讓敵人的重生分三個階段。階段一,重生以下敵人

http://ithelp.ithome.com.tw/upload/images/20161225/20103149HK8AmKDyX0.png

過了一定的時間,進入階段二,重生以下敵人

http://ithelp.ithome.com.tw/upload/images/20161225/20103149SFPdqaPGwX.png

再過一段時間,進入階段三,重生以下敵人

http://ithelp.ithome.com.tw/upload/images/20161225/20103149LlYnCtV2su.png

另外,我認為先前設計以每秒一定機率重生的方式不好,所以想改成像重生掉落武器一樣,以隨機區間(定義最大區間和最小區間)的方式來重生敵人。

打開EnemySpawnPoint.cs,全部刪掉重新寫過:

public class EnemySpawnPoint : MonoBehaviour 
{
	//重生隨機區間
    public float minSpawnInterval;
    public float maxSpawnInterval;
    
	//讓重生隨機區間隨時間縮短的值
    public float minSpawnIntervalDecreaseRate;
    public float maxSpawnIntervalDecreaseRate;
    public float decreaseTime; //過decreaseTime秒後,縮短隨機區間
    
	//隨機區間的最小值
    public float minSpawnIntervalCap;
    public float maxSpawnIntervalCap;
    
	//第一階段的敵人Prefab
    public GameObject[] tier1EnemyPrefabs;

	//第二階段的敵人Prefab
    public GameObject[] tier2EnemyPrefabs;

	//第三階段的敵人Prefab
    public GameObject[] tier3EnemyPrefabs;
    
	//過secToTier2秒後,從第一階段敵人替換成第二階段敵人
    public float secToTier2;

	//過secToTier3秒後,從第二階段敵人替換成第三階段敵人
    public float secToTier3;
    
	//現階段的敵人Prefab
    private GameObject[] currEnemyPrefabs;
    
    void Start()
    {
		//開始以下三個Coroutine
        StartCoroutine (ChangeEnemyTierCoroutine ());
        StartCoroutine (SpawnCoroutine ());
        StartCoroutine (DecreaseSpawnIntervalCoroutine ());
    }
    
	//替換不同階段的敵人Prefab
    IEnumerator ChangeEnemyTierCoroutine()
    {
        currEnemyPrefabs = tier1EnemyPrefabs;
        yield return new WaitForSeconds (secToTier2);
        currEnemyPrefabs = tier2EnemyPrefabs;
        yield return new WaitForSeconds (secToTier3);
        currEnemyPrefabs = tier3EnemyPrefabs;
    }
    
	//每時間區間,重生一個隨機敵人
    IEnumerator SpawnCoroutine()
    {
        while (true) {
            yield return new WaitForSeconds (Random.Range (minSpawnInterval, maxSpawnInterval));
            Instantiate (currEnemyPrefabs [Random.Range (0, currEnemyPrefabs.Length)], transform.position, Quaternion.identity);
        }
    }
    
	//每過decreaseTime的時間,降低重生區間
    IEnumerator DecreaseSpawnIntervalCoroutine()
    {
        while (true) {
            yield return new WaitForSeconds (decreaseTime);
            minSpawnInterval = Mathf.Max (minSpawnIntervalCap, minSpawnInterval - minSpawnIntervalDecreaseRate);
            maxSpawnInterval = Mathf.Max (maxSpawnIntervalCap, maxSpawnInterval - maxSpawnIntervalDecreaseRate);
        }
    }
}

這段程式碼的三個Coroutine分別達成了:一、每隔一段時間替換敵人Prefab,二、每隔一段時間,重人一個敵人,三、透過增加敵人的重生時間緩緩增加遊戲難度。

把數值和Prefab拉到對應的欄位後,遊戲就不再那麼單調啦!

待續。


上一篇
Day 24: 音效與背景音樂
下一篇
Day 26: 觸控、Camera Aspect Ratio
系列文
我要和天一樣高!!!(Unity 2D手機小遊戲開發日誌)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言