3D作業5--與遊戲世界交互

作業要求

編寫一個簡單的鼠標打飛碟(Hit UFO)遊戲

  • 遊戲內容要求:
  1. 遊戲有 n 個 round,每個 round 都包括10 次 trial;
  2. 每個 trial 的飛碟的色彩、大小、發射位置、速度、角度、同時出現的個數都可能不同。它們由該 round 的 ruler 控制;
  3. 每個 trial 的飛碟有隨機性,總體難度隨 round 上升;
  4. 鼠標點中得分,得分規則按色彩、大小、速度不同計算,規則可自由設定。
  • 遊戲的要求:
  1. 使用帶緩存的工廠模式管理不同飛碟的生產與回收,該工廠必須是場景單實例的!具體實現見參考資源 Singleton 模板類
  2. 近可能使用前面 MVC 結構實現人機交互與遊戲模型分離
  3. 如果你的使用工廠有疑問,參考:彈藥和敵人:減少,重用和再利用

參考博客

https://blog.csdn.net/jc2474223242/article/details/79975137?tdsourcetag=s_pcqq_aiomsg

遊戲UML圖

這裏參考了前輩的代碼,所以UML類圖幾乎一樣,就直接借用了。
在這裏插入圖片描述

實現思路

這裏主要介紹幾個比較大的模塊的大概思路,詳細說明可見代碼的註釋部分。

  • GUI頁面
    首先是用戶GUI頁面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UserGUI : MonoBehaviour {

    private FirstSceneController action;
    private GUIStyle fontstyle1 = new GUIStyle();

    //1
    private GUIStyle textStyle;
    private GUIStyle hintStyle;
    private GUIStyle btnStyle;

    //初始化
    void Start () {
        action = SSDirector.getInstance().currentSceneController as FirstSceneController;
        fontstyle1.fontSize = 20;
    }

    // Update is called once per frame
    private void OnGUI()
    {
        hintStyle = new GUIStyle {
            fontSize = 20,
            fontStyle = FontStyle.Normal
        };

        //標題
        textStyle = new GUIStyle {
            fontSize = 50,
            alignment = TextAnchor.MiddleCenter
        };
        textStyle.normal.textColor = Color.blue;
        GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 150, 100, 50), "UFO小遊戲!", textStyle);

        //button
        btnStyle = new GUIStyle("button") {
            fontSize = 15
        };
        btnStyle.normal.textColor = Color.black;
        if (GUI.Button(new Rect(0, 50, 80, 50), "重新開始", btnStyle))  //前兩個參數是位置(左上角),後兩個是大小
        {
            action.Restart();
        }
        if (GUI.Button(new Rect(0, 100, 80, 50), " 暫停 ", btnStyle))
        {
            action.Pause();
        }

        //得分
        if (action.flag == 0)
        {
            fontstyle1.normal.textColor = Color.red;
            GUI.Label(new Rect(0, 0, 300, 50), "得分: " +
                action.score + ", 回合: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
        }
        //結束
        else if (action.flag == 1)
        {
            fontstyle1.normal.textColor = Color.red;
            GUI.Label(new Rect(0, 0, 300, 50), "你的得分是 : " + action.score, fontstyle1);
        }
        //暫停
        else
        {
            fontstyle1.normal.textColor = Color.green;
            GUI.Label(new Rect(0, 0, 300, 50), "得分: " +
                action.score + ", 回合: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
            GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height/2-50, 300, 100), "暫停!", textStyle);
        }
    }
}

  • 由之前的兩次作業(牧師和魔鬼),我們已經有了一個較爲完整的MVC框架,所以這裏可以直接套用的之前的框架。但這裏比較特別的是,添加了一個飛碟工廠,用於減少資源開銷 ,詳細說明請見註釋:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DiskFactory : MonoBehaviour {
    //used和free分別表示已經在使用的和未被利用的飛碟對象
    public List<GameObject> used = new List<GameObject>();
    public List<GameObject> free = new List<GameObject>();

	//初始化
	void Start () { }

    public void GenDisk()
    {
        GameObject disk;
        //如果free的列表爲空,而我們有需要它,這時才創建新的對象
        if(free.Count == 0)
        {
            disk = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Disk"), Vector3.zero, Quaternion.identity);
        }
        //不然,就用之前的
        else
        {
            disk = free[0];
            free.RemoveAt(0);
        }
        float x = Random.Range(-10.0f, 10.0f);
        disk.transform.position = new Vector3(x, 0, 0);
        disk.transform.Rotate(new Vector3(x < 0? -x*9 : x*9, 0, 0));
        float r = Random.Range(0f, 1f);
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);
        Color color = new Color(r, g, b);
        disk.transform.GetComponent<Renderer>().material.color = color;
        used.Add(disk);
    }
    public void RecycleDisk(GameObject obj)
    {
        obj.transform.position = Vector3.zero;
        free.Add(obj);
    }
}
  • 場景控制器,依靠之前所做的MVC架構的分離作用,這裏僅需要改Awake,Update,GenGameObject這三個函數和各種變量。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//場景控制器
public class CCActionManager : SSActionManager, ISSActionCallback
{
    //這裏將動作變成一個列表,就能夠同時控制多個飛碟在遊戲中飛行
    public FirstSceneController sceneController;
    public List<CCMoveToAction> seq = new List<CCMoveToAction>();
    public UserClickAction userClickAction;
    public DiskFactory disks;
    
    protected new void Start()
    {
        sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
        sceneController.actionManager = this;
        disks = Singleton<DiskFactory>.Instance;
    }
    protected new void Update()
    {
        if(disks.used.Count > 0)
        {
            GameObject disk = disks.used[0];
            float x = Random.Range(-10, 10);
            CCMoveToAction moveToAction = CCMoveToAction.GetSSAction(new Vector3(x, 12, 0), 3 * (Mathf.CeilToInt(FirstSceneController.times / 10) + 1) * Time.deltaTime);
            seq.Add(moveToAction);
            this.RunAction(disk, moveToAction, this);
            disks.used.RemoveAt(0);
        }
        if (Input.GetMouseButtonDown(0) && sceneController.flag == 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitGameObject;
            if (Physics.Raycast(ray, out hitGameObject))
            {
                GameObject gameObject = hitGameObject.collider.gameObject;
                if (gameObject.tag == "disk")
                {
                    foreach(var k in seq)
                    {
                        if (k.gameObject == gameObject)
                            k.transform.position = k.target;
                    }
                    userClickAction = UserClickAction.GetSSAction();
                    this.RunAction(gameObject, userClickAction, this);
                }
            }
        }
        base.Update();
    }
    public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
        disks.RecycleDisk(source.gameObject);
        seq.Remove(source as CCMoveToAction);
        source.destory = true;
        if (FirstSceneController.times >= 30)
            sceneController.flag = 1;
    }
    public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
    }
    public void Pause()
    {
        if(sceneController.flag == 0)
        {
            foreach (var k in seq)
            {
                k.enable = false;
            }
            sceneController.flag = 2;
        }
        else if(sceneController.flag == 2)
        {
            foreach (var k in seq)
            {
                k.enable = true;
            }
            sceneController.flag = 0;
        }
    }
}

遊戲截圖

在這裏插入圖片描述

完整代碼

請見我的GitHub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章