Unity3D作业五的项目一

题目:

写一篇短文,描述以下游戏需求的实现。请写步骤,贴代码并解释:

  • 实现点击效果。

    • 用 Plane 或其他物体做地面, tag 为“Finish”
    • 点击地面后,出现一个圆形攻击标记,两秒后自动消失。注意:该攻击标记不能挡住点击。(Primitive Objects / Cylinder)
    • 请使用一个简单工厂创建、管理这些的标记,并自动收回这些标记(注意,这些对象创建后,放在列表内,不必释放)。

    用户仅需申请使用即可 GameObject myFactory.placeAttackMark(Vector3 postion)


回答:

这次作业,部分代码参考了师兄的代码。

先上UMLet图


为了简化代码,我将本来应该是封装的私有变量,改为公有变量,如果需要保证数据的封装,只需要多加上一个公有的成员函数用来返回私有变量的值就行了。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Com.Mygame;
namespace TimeTime {

    public class TimeControl : MonoBehaviour
    {
        public GameMaster GameM;
        private float time;
        // Use this for initialization
        void Start()
        {

        }

        public void setting()
        {
            time = Time.time;
            GameM = ActionManager.master;
        }

        // Update is called once per frame
        void Update()
        {
            if (Time.time - time > 2.0f)
            {
                Debug.Log("Should be destroyed");
                GameM.MarkComplete(this.gameObject);
                Destroy(this);
            }
        }
    }

}



using UnityEngine;
using System.Collections;
using Com.Mygame;
using System.Collections.Generic;
using TimeTime;

public class GameMaster : MonoBehaviour
{
    public GameObject Mark;
    private Vector3 UnusedPlace = new Vector3(1000f, 1000f, 1000f);
    private List<GameObject> using_Symbol = new List<GameObject>();
    private List<GameObject> unuse_Symbol = new List<GameObject>();

    void Start()
    {
        ActionManager.master = this;
       
        for (int i = 0; i < 3; i++)
        {
            GameObject newMark = GameObject.Instantiate(Mark, UnusedPlace, Quaternion.identity) as GameObject;
            unuse_Symbol.Add(newMark);
        }
    }

    public void AskForMark(Vector3 point)
    {
       
         if (unuse_Symbol.Count == 0)
          {
              GameObject newMark = GameObject.Instantiate(Mark, UnusedPlace, Quaternion.identity) as GameObject;
              unuse_Symbol.Add(newMark);
        }

        GameObject OneObject = unuse_Symbol[0];
        OneObject.transform.position = point;
        TimeControl ac = OneObject.AddComponent<TimeControl>();
        ac.setting();
        unuse_Symbol.RemoveAt(0);
        using_Symbol.Add(OneObject);

    }

    public void MarkComplete(GameObject obj)
    {
        using_Symbol.Remove(obj);
        unuse_Symbol.Add(obj);
        obj.transform.position = UnusedPlace;
    }
}

using UnityEngine;
using System.Collections;
using Com.Mygame;

public class GameControl : MonoBehaviour
{

   public Camera cam;
   private ActionManager myFactory;

    void Start()
    {
        Debug.Log("Camera can work sucessfully!");
        myFactory = ActionManager.getInstance();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mp = Input.mousePosition;

            Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(mp);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.gameObject.tag == "Finish")
                {
                    myFactory.placeAttackMark(hit.point);
                }
            }
        }
    }
}

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

namespace Com.Mygame
{
    public class ActionManager : System.Object
    {
        private static ActionManager _instance;
        public static GameMaster master;

        public static ActionManager getInstance()
        {
            if (_instance == null)
            {
                _instance = new ActionManager();
            }
            return _instance;
        }

        public void placeAttackMark(Vector3 point)
        {

            Debug.Log("This is the Hit Position" + point);
            //GameObject master = GameObject.Find("GameMaster");
            master.AskForMark(point);

            // showSymbol ac = mark.AddComponent<showSymbol>();
            //  ac.setting(point);
        }
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值