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);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值