3D遊戲編程 作業4 打飛碟

代碼地址在此Github

演示視頻的地址爲:演示視頻

首先是實現了UserGUI的內容:


	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	
	public class UserGUI : MonoBehaviour {
	
	    private FirstSceneController action;
	    private GUIStyle fontstyle1 = new GUIStyle();
	    void Start () {
	        action = SSDirector.getInstance().currentSceneController as FirstSceneController;
	        fontstyle1.fontSize = 50;
	    }
	    private void OnGUI()
	    {
	        if (GUI.Button(new Rect(Screen.width / 2 - 80, Screen.height-60, 80, 60), "RESTART"))
	        {
	            action.Restart();
	        }
	        if (GUI.Button(new Rect(Screen.width / 2 + 20, Screen.height-60, 80, 60), "Pause"))
	        {
	            action.Pause();
	        }
	        if (action.flag == 0)
	        {
	            fontstyle1.normal.textColor = Color.green;
	            GUI.Label(new Rect(Screen.width / 2 - 180, 0, 300, 100), "Score: " +
	                action.score + ", Round: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
	        }
	        else if (action.flag == 1)
	        {
	            fontstyle1.normal.textColor = Color.red;
	            GUI.Label(new Rect(Screen.width / 2 - 150, 0, 300, 100), "Your score is : " + action.score, fontstyle1);
	        }
	        else
	        {
	            fontstyle1.normal.textColor = Color.green;
	            GUI.Label(new Rect(Screen.width / 2 - 180, 0, 300, 100), "Score: " +
	                action.score + ", Round: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
	            fontstyle1.normal.textColor = Color.red;
	            GUI.Label(new Rect(Screen.width / 2 - 70, Screen.height/2-50, 300, 100), "Pause!", fontstyle1);
	        }
	    }
	}

做出一個好的界面來,然後是導演、場記之類的還是和之前做魔鬼與傳教士的差不多,這裏再實現一個工廠。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeetFactory : MonoBehaviour {
    public List<GameObject> used = new List<GameObject>();
    public List<GameObject> free = new List<GameObject>();

	void Start () { }

    public void GenSkeet()
    {
        GameObject skeet;
        if(free.Count == 0)
        {
            skeet = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Skeet"), Vector3.zero, Quaternion.identity);
        }
        else
        {
            skeet = free[0];
            free.RemoveAt(0);
        }
        float x = Random.Range(-10.0f, 10.0f);
        skeet.transform.position = new Vector3(x, 0, 0);
        skeet.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);
        skeet.transform.GetComponent<Renderer>().material.color = color;
        used.Add(skeet);
    }
    public void RecycleSkeet(GameObject obj)
    {
        obj.transform.position = Vector3.zero;
        free.Add(obj);
    }
}

最後對於飛碟的運動使用的MoveToAction方向。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CCMoveToAction : SSAction
{
    public Vector3 target;
    public float speed;

    public static CCMoveToAction GetSSAction(Vector3 target, float speed)
    {
        CCMoveToAction action = CreateInstance<CCMoveToAction>();
        action.target = target;
        action.speed = speed;
        return action;
    }

    public override void Start()
    {
        
    }
    public override void Update()
    {
        if(enable)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed);
            if (this.transform.position == target)
            {
                this.enable = false;
                this.callback.SSActionEvent(this);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章