遊戲設計要求:
- 創建一個地圖和若干巡邏兵(使用動畫);
- 每個巡邏兵走一個3~5個邊的凸多邊型,位置數據是相對地址。即每次確定下一個目標位置,用自己當前位置爲原點計算;
- 巡邏兵碰撞到障礙物,則會自動選下一個點爲目標;
- 巡邏兵在設定範圍內感知到玩家,會自動追擊玩家;
- 失去玩家目標後,繼續巡邏;
- 計分:玩家每次甩掉一個巡邏兵計一分,與巡邏兵碰撞遊戲結束;
- 程序設計要求:
- 必須使用訂閱與發佈模式傳消息
- 工廠模式生產巡邏兵
實現的代碼地址爲:Github
視頻的地址爲:騰訊視頻
首先是找到巡邏兵的資源,我是在Asset Store上找到的資源,大家自己找自己喜歡的就好了。
然後進行具體的代碼實現,首先在實現好對各個物體的具體控制類之後,然後就是實現GameEventManager類,也是來進行訂閱者模式的代碼。
public class GameEventManager : MonoBehaviour
{
public delegate void EscapeEvent(GameObject patrol);
public static event EscapeEvent OnGoalLost;
public delegate void FollowEvent(GameObject patrol);
public static event FollowEvent OnFollowing;
public delegate void GameOverEvent();
public static event GameOverEvent GameOver;
public delegate void WinEvent();
public static event WinEvent Win;
public void PlayerEscape(GameObject patrol) {
if (OnGoalLost != null) {
OnGoalLost(patrol);
}
}
public void FollowPlayer(GameObject patrol) {
if (OnFollowing != null) {
OnFollowing(patrol);
}
}
public void OnPlayerCatched() {
if (GameOver != null) {
GameOver();
}
}
public void TimeIsUP() {
if (Win != null) {
Win();
}
}
}
然後這次也有一些比較需要實現的內容,鏡頭跟隨的內容,沒有這個的話,遊戲進行的體驗不是非常的好。
public class CameraFollowAction : MonoBehaviour
{
public GameObject player;
public float smothing = 5f;
Vector3 offset;
void Start() {
offset = new Vector3(0, 5, -5);
}
void FixedUpdate() {
Vector3 target = player.transform.position + offset;
transform.position = Vector3.Lerp(transform.position, target, smothing * Time.deltaTime);
}
}