蓝牙枪

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 枪(挂在摄像机身上)
/// </summary>
public class ARGun : MonoBehaviour
{
    //单例
    public static ARGun Instance = null;
    void Awake()
    {
        Instance = this;
    }
    //子弹夹
    Dictionary<string, List<Bullet>> bullets = new Dictionary<string, List<Bullet>>();


    // Use this for initialization
    void Start()
    {
        //初始化子弹
        InitBullet();
    }
    void InitBullet()
    {
        //创造50颗A类型的子弹
        List<Bullet> bulletA = BulletFactory.CreateBullet(BulletType.A, 50);
        //创造50颗A类型的子弹
        List<Bullet> bulletB = BulletFactory.CreateBullet(BulletType.B, 50);
        //把子弹添加到子弹夹中
        bullets.Add("A", bulletA);
        bullets.Add("B", bulletB);
    }
    // Update is called once per frame
    void Update()
    {
        //射线
        //sendBundle();
        //发射子弹
        shotBullet();
    }
    #region
    //void sendBundle() 
    //{
    //    //点击鼠标左键,僵尸减血
    //    if (Input.GetMouseButtonDown(0)) 
    //    {
    //        /*
    //        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    //        //定义一个碰撞对象
    //        RaycastHit hitInfo;
    //        if (Physics.Raycast(ray, out hitInfo, 1000)) 
    //        {
    //            GameObject obj = hitInfo.collider.gameObject;
    //            if (obj.tag == "Respawn") 
    //            { 


    //                //僵尸减血
    //                Zombie zom =obj.GetComponent<Zombie>();
    //                zom.setHP(2);


    //            }
    //        }
    //         */
    //        //创建一个子弹
    //        GameObject qiu = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    //        //设置子弹的位置
    //        qiu.transform.position = GameObject.Find("").transform.position;
    //        //设置球体的大小
    //        qiu.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    //        //添加刚体
    //        Rigidbody r = qiu.AddComponent<Rigidbody>();
    //        //添加力
    //        r.AddForce(this.transform.forward * 500f * Time.deltaTime);
    //    }
    //    //创建一个子弹
    //    GameObject zidan = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    //    //设置子弹的位置
    //    zidan.transform.position = GameObject.Find("").transform.position;
    //    //设置子弹的大小
    //    zidan.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    //    //为子弹添加刚体
    //    Rigidbody b = zidan.AddComponent<Rigidbody>();
    //    //为小球添加力
    //    b.AddForce(this.transform.forward * 500f * Time.deltaTime);
    //}
    #endregion
    /// <summary>
    /// 点击鼠标左键,发射子弹
    /// </summary>
    void shotBullet()
    {
        //点击鼠标左键,发射A类型的子弹
        if (Input.GetMouseButtonDown(0))
        {
            #region
            ////创建一个子弹
            //GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            ////设置子弹的位置(空实体)
            //bullet.transform.position = GameObject.Find("bullet").transform.position;
            ////子弹的旋转
            //bullet.transform.rotation = GameObject.Find("bullet").transform.rotation;
            ////设置子弹的大小
            //bullet.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
            ////为子弹添加刚体
            //Rigidbody rigBullet = bullet.AddComponent<Rigidbody>();
            ////为子弹添加一个力
            //rigBullet.AddForce(bullet.transform.forward *1000f);
            #endregion
            Send("A");
        }
        //点击鼠标右键,发射B类型的子弹
        if (Input.GetMouseButtonDown(1))
        {
            Send("B");
        }
    }
    /// <summary>
    /// 发送子弹
    /// </summary>
    /// <param name="type">子弹的类型</param>
    public void Send(string type)
    {
        if (bullets[type].Count <= 0) 
        {
            Debug.Log("子弹不够");
            return;
        }
        //得到第一个子弹
        Bullet b = bullets[type][0];
        //找到子弹组件
        Transform t = GameObject.Find("bullet").transform;//.position;
        //子弹飞出去
        b.Move(t.position, t.rotation);
        //把子弹从弹夹中删除
        bullets[type].RemoveAt(0);
    }
    /// <summary>
    /// 回收子弹
    /// </summary>
    /// <param name="b"></param>
    public void reBackBullet(Bullet b)
    {
        //子弹的类型
        string type = System.Enum.GetName(b.type.GetType(), b.type);
        //把子弹回收到弹夹中
        bullets[type].Add(b);
    }
}

猜你喜欢

转载自blog.csdn.net/wk201403010114/article/details/78464860
今日推荐