Unity3D作业五项目二

题目:

编程实践,请写步骤,贴代码并解释:

  • 写个用鼠标打飞碟的游戏。

  • 游戏要分多个 round , 飞碟数量每个 round 都是 n 个,但色彩,大小;发射位置,速度,角度,每次发射数量可以变化。

    游戏过程中,仅能创建 n 个飞碟, 且不容许初始化阶段生成任何飞碟。 飞碟线路计算请使用 mathf 类。 向下加速度 a 是常数。 飞碟被用户击中,则回收。并按你定义的规则计算分数。飞碟落地(y < c),则自动回收。

  • 请画 UML 图,描述主要对象及其关系


回答:

这次作业本应该使用单实例工厂化完成的,不过由于我对单实例工厂化的理解不够深入,所以没有采用,而是准备再下一次的作业当中完成对于类的修改。在这个作业中,我将 SceneController作为联系各个类的中间类,并且是单实例的,在这个类中,我连接了其他的所有类,并且再其他所有类中都引用了这个类。其他所有类都继承monobehaviou,因此也是单实例的,各个类之间,只需要通过 SceneController这个中间类,就可以拿到所需要类的内容,为了简化处理,使用了公有变量。

Unity3D作业五项目二_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
using UnityEngine.UI;
using rule;

public class UserControl : MonoBehaviour
{

    public Text Main_Text;   // 显示主提示
    public Text Score_Text;  // 显示得分   
    public Text Level_Text;  // 显示回合  
    public SceneController SceneCon;
    public Camera cam;
    public bool IsFinish = false; 

    void Start()
    {
        SceneCon = SceneController.GetInstance();
        SceneCon.UserC = this;
        Main_Text.text = "Gaming";
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space") && IsFinish == false)
        {
           SceneCon.GameC.Launch_UFO();
        }
        else if (Input.GetMouseButtonDown(0))
        {
            Vector3 mp = Input.mousePosition;
            Ray ray = cam.GetComponent().ScreenPointToRay(mp);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.gameObject.tag == "UFO")
                {
                    SceneCon.GameC.Hit_UFO(hit.transform.gameObject);
                    SceneCon.rule.HitUFO();
                }
            }
        }

        Level_Text.text = "Level: " + SceneCon.Now_Level;
        Score_Text.text = "Score: " + SceneCon.Now_Score;
    }



}

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




namespace MyGame
{

    public class UFOControl : MonoBehaviour
    {
        public List UFO_Ready_List;
        public List UFO_Active_List;   
        public List UFO_UnActive_List;
        public GameObject UFO_Pre;  //  UFO预制
        private Vector3 UnusedPlace = new Vector3(1000f, 1000f, 1000f);

        public SceneController scene;

        public void Start()
        {
            scene = SceneController.GetInstance();
            scene.ufoc = this;

            for(int i=0; i<10;i++)
            {
                GameObject Temp = GameObject.Instantiate(UFO_Pre, UnusedPlace, Quaternion.identity) as GameObject;
                UFO_Ready_List.Add(Temp);
            }

        }




        //函数,函数
        //发射判断
        
        public void Launch_Ready_UFO()
        {
            if(UFO_Ready_List.Count  == 0)
            {
                scene.NextLevel();
                for(int i = 0; i< 10; i++)
                {
                    if(UFO_Active_List.Count !=0)
                    {
                        GameObject temp = UFO_Active_List[0];
                        temp.transform.position = UnusedPlace;
                        UFO_Ready_List.Add(temp);
                        UFO_Active_List.Remove(temp);
                    }
                    else if(UFO_UnActive_List.Count !=0)
                    {
                        GameObject temp = UFO_UnActive_List[0];
                        temp.transform.position = UnusedPlace;
                        UFO_Ready_List.Add(temp);
                        UFO_UnActive_List.Remove(temp);
                    }
                }
            }
        }
        


    }
}

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

public class TimeControl : MonoBehaviour
{

    public UFOControl UFOCon;
    private float time;

    public float UFO_Speed;
    public SceneController scene;
    public float UFO_Speed_y;
    public float UFO_Speed_x;
    // Use this for initialization
    void Start()
    {
        scene = SceneController.GetInstance();
    }

    public void setting(float Speed)
    {
        time = Time.time;
        UFO_Speed = Speed;
  

    }

    // Update is called once per frame
    void Update()
    {

        UFO_Speed_y = UnityEngine.Random.Range(0, 3) + UFO_Speed;
        UFO_Speed_x = UnityEngine.Random.Range(-10, 10) + UFO_Speed;
        transform.Translate(Vector3.down * Time.deltaTime*UFO_Speed_y, Space.World);
        transform.Translate(Vector3.left * Time.deltaTime*UFO_Speed_x);

        if (this.gameObject.transform.position.y < -3)
        {
            scene.rule.MissUFO();
        }

        if (Time.time - time > 8 || this.gameObject.transform.position.y < -3)
        {
            scene.ufoc.UFO_Active_List.Remove(this.gameObject);
            scene.ufoc.UFO_UnActive_List.Add(this.gameObject);
            this.gameObject.transform.position = new Vector3(1000f, 1000f, 1000f);
            Destroy(this);
        }
    }
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
using rule;

namespace MyGame
{


    public class SceneController : System.Object
    {
        private static SceneController _instance;
        
        public int Now_Score;   //当前分数
        public Level Now_Level = Level.Ready;//当前关卡



        public GameControl GameC; //和GameControl联通
        public UserControl UserC;   //  和UserControl联通
        public Rule rule;   //和Rule联通
        public UFOControl ufoc; //和UFOControl联通


        public static SceneController GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SceneController();
            }
            return _instance;
        }

        //判断是否进入下一个LEVEL
        public void NextLevel()
        {
            if (Now_Level == Level.Ready)
                rule.Load_Reources(Now_Level);
            else if (Now_Level == Level.First)
                rule.Load_Reources(Now_Level);
            else if (Now_Level == Level.Second)
                rule.Load_Reources(Now_Level);
            else if (Now_Level == Level.Tird)
            {
                if(Now_Score > 0)
                {
                    UserC.IsFinish = true;
                    UserC.Main_Text.text = "win";
                }
                else
                {
                    UserC.IsFinish = true;
                    UserC.Main_Text.text= "Fail";
                }
            }
        }

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;
using rule;

public class GameControl : MonoBehaviour
{

    public Color UFO_Color;                // 飞碟颜色  
    public float UFO_Speed;                // 发射速度  
    public SceneController scene;


    void Awake()
    {
        scene = SceneController.GetInstance();
        scene.GameC = this;
        
    }
    //初始化场景
    public void setting(Color color, float speed)
    {
        UFO_Color = color;
        UFO_Speed = speed;
    }

    // Use this for initialization
    void Start()
    {

    }

    public void Launch_UFO()
    {
            int i = 0;
        if (scene.Now_Level == Level.First)
            i = 1;
        else if (scene.Now_Level == Level.Second)
            i = 2;
        else if (scene.Now_Level == Level.Tird)
            i = 5;

        for(int j = 0; j < i;j++)
        {
            scene.ufoc.Launch_Ready_UFO();
            GameObject Temp = scene.ufoc.UFO_Ready_List[0];
            scene.ufoc.UFO_Ready_List.Remove(Temp);
            Temp.GetComponent().material.color = UFO_Color;
            TimeControl ac = Temp.AddComponent();
            Temp.transform.position = new Vector3(0+Random.Range(-10,10), 8+Random.Range(-4,5), 0);
            ac.setting(UFO_Speed);
            scene.ufoc.UFO_Active_List.Add(Temp);

        }

    }

    public void Hit_UFO(GameObject temp)
    {
        scene.ufoc.UFO_Active_List.Remove(temp);
        scene.ufoc.UFO_UnActive_List.Add(temp);
        temp.transform.position = new Vector3(1000f, 1000f, 1000f);
       
    }



    // Update is called once per frame
    void Update()
    {

    }
}

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

namespace rule
{
    public enum Level { Ready, First, Second, Tird }; //关卡信息
    public class Rule : MonoBehaviour
    {

        public Color UFO_Color;                //颜色
        public int UFO_Number = 10;         // 飞碟一共的数量
        public int UFO_Speed;               //飞碟的速度
        public int OneUFOScore = 1;     //击中飞碟得到的分数
        public int OneUFOFail = 1;       //飞碟掉落失去的分数


        public SceneController scene;

        void Awake()
        {
            scene = SceneController.GetInstance();
            scene.rule = this;
        }

        void Start()
        {
            scene.NextLevel();
        }

        // 击中飞碟得分  
        public void HitUFO()
        {
            scene.Now_Score += OneUFOScore;
            Debug.Log("GODDDDDDDDDDDDD" + scene.Now_Score);
        }

        // 没击中飞碟失分  
        public void MissUFO()
        {
            scene.Now_Score -= OneUFOScore;
            Debug.Log("MISSSSSSSSSSSSSS" + scene.Now_Score);
        }

        public void Load_Reources(Level level)
        {
            if (level == Level.Ready)
            {
                UFO_Color = Color.blue;
                UFO_Speed = 1;
                scene.Now_Level = Level.First;
                scene.GameC.setting(UFO_Color, UFO_Speed);
            }
            else if (level == Level.First)
            {
                UFO_Color = Color.green;
                UFO_Speed = 3;
                scene.Now_Level = Level.Second;
                scene.GameC.setting(UFO_Color, UFO_Speed);
            }
            else if (level == Level.Second)
            {
                UFO_Color = Color.red;
                UFO_Speed = 5;
                scene.Now_Level = Level.Tird;
                scene.GameC.setting(UFO_Color, UFO_Speed);
            }
        }
    }

}



你可能感兴趣的:(作业)