打枪的实现

Posted charsoul

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打枪的实现相关的知识,希望对你有一定的参考价值。

  前段时间有点忙,都快吧blog的事情忘了,尽量更新吧。

  写了一个Enums的类,想把所有的Enum都放进去,目前有:

public enum ItemType { weapon, equipment, food, medicine, truck, spacial, }

    public enum WeaponType { closeAttack, fastShoot, shotgun, sniper, missile, }

    public enum EquipmentType { wearing, throwing,}

    public enum Respond {nul, ok, error, }

  那个Respond是给MassageCenter回应消息用的。

 

还有Item类:

[Header("Item")]

    public uint m_id;       //物品id

    public string m_name;   //物品名

    public Enums.ItemType m_itemType;   //物品种类

    public string m_iconPath;//物品图路径

    public string m_info;   //物品描述

    public uint m_weight;   //物品重量

    public int[] m_recoveriedBenefit;    //回收利益

    private void Awake()
    {
        UpdateIcon();
    }

    public void UpdateIcon()
    {
        if (null != m_iconPath && this.GetComponent<Image>())
        {
            this.GetComponent<Image>().overrideSprite = Resources.Load(m_iconPath, typeof(Sprite)) as Sprite;
        }
    }
}

  暂时就想到这些属性,顺便加了个更新Icon的。

  因为是自己一个人做,就没管那些GET SET,嫌麻烦,直接全部public 。。(请不要学我)

  然后写了几个类来继承它,比如Weapon:

public class Weapon : Item{

    [Header("Weapon")]
    //武器种类
    public Enums.WeaponType m_weaponType;
    //子弹种类
    public string m_bulletPath;
    //伤害
    public int m_damage;
    //冷却时间(一发/一次)
    public float m_cooldownTime;
    //能量消耗
    public float m_energyCost;
    //当前耐久
    public int m_durability;
    //最大耐久度
    public int m_maxDurability;
}

  

  有了武器,得有子弹啊,然后写了个bullet:

    public float m_speed;   //子弹速度
    public Vector3 m_dir;   //子弹方向
    public float m_damage;  //子弹初始伤害
    public float m_lifeTime;//子弹存活时间
    public string m_invokePath;//子弹碰撞后调用物品

    private GameObject m_invokeGameObj;
    private float time;


    private void Start()
    {
        m_invokeGameObj = Resources.Load(m_invokePath) as GameObject;
    }

    private void Update()
    {
        //float angle = Vector3.Angle(m_dir, Vector3.up);
        //this.gameObject.transform.Rotate(Vector3.back, angle);
        
        time += Time.deltaTime;
        if (time >= m_lifeTime)
        {
            time = 0;
            ItemPool.DestroyActiveObject(this.name, this.gameObject);
        }
        this.transform.position += m_dir * m_speed * Time.deltaTime;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (m_invokeGameObj)
        {
            GameObject go;
            if (null == (go = ItemPool.GetObject(m_invokeGameObj.name)))
                go = Instantiate(m_invokeGameObj);
            go.transform.parent = this.transform.parent;
            go.transform.localPosition = this.transform.localPosition;

        }
        if (collision.tag == "Enemy")
        {
        }
        time = 0;
        ItemPool.DestroyActiveObject(this.name, this.gameObject);
    }

  

  放在prefab上,挺好用。当然,这里用在远程攻击武器上,近战我还没写,近战武器攻击图太难画了。。。没有美术的日子真难过。。。

  再上个weaponController 把weapon连上控制, 就大功告成了。

public class WeaponsControler : MonoBehaviour{

    public Transform m_weapon;


    private float time;     //冷却时间
    private Vector2 weaponDir = Vector2.zero;   //武器的方向
    private Weapon weaponClass; //武器类
    private GameObject bullet;

    private void Awake()
    {
        weaponClass = m_weapon.GetComponent<Weapon>();
        if (weaponClass.m_weaponType == Enums.WeaponType.closeAttack)
        { }
        else
            bullet = Resources.Load(weaponClass.m_bulletPath) as GameObject;
    }

    private void Start()
    {
        MessageCenter.AddMessageListener("VirtlRocker_Right", OnWeaponMove);
    }
    private void Update()
    {
        WeaponMoveUpdate();
        AttackUpdate();
    }
    private void WeaponMoveUpdate()
    {       
        if (weaponDir != Vector2.zero)
        {
            Vector3 tmpDir = new Vector3(0, 0, Vector2.Angle(weaponDir, Vector2.right));
            if (Vector2.Dot(weaponDir, Vector2.right) < 0)
            {
                m_weapon.localScale = new Vector3(-1, 1, 1);
                if (Vector2.Dot(weaponDir, Vector2.up) < 0)
                    m_weapon.eulerAngles = new Vector3(0, 0, (180 - Vector2.Angle(weaponDir, Vector2.right)));
                else
                    m_weapon.eulerAngles = new Vector3(0, 0, (Vector2.Angle(weaponDir, Vector2.right) - 180));
            }
            else
            {
                m_weapon.localScale = new Vector3(1, 1, 1);
                if (Vector2.Dot(weaponDir, Vector2.up) < 0)
                    m_weapon.eulerAngles = new Vector3(0, 0, -Vector2.Angle(weaponDir, Vector2.right));
                else
                    m_weapon.eulerAngles = new Vector3(0, 0, Vector2.Angle(weaponDir, Vector2.right));
            }          
        }
    }

    private void AttackUpdate()
    {
        if (weaponDir != Vector2.zero)
        {
            time += Time.deltaTime;
            if (time < weaponClass.m_cooldownTime)
                return;
            if (weaponClass.m_weaponType == Enums.WeaponType.closeAttack)
            {
            }
            else
            {
                if (bullet)
                {
                    Debug.Log(bullet.name);
                    GameObject go;
                    if ((go = ItemPool.GetObject(bullet.name)) == null)
                        go = Instantiate(bullet);
                    go.GetComponent<Bullet>().m_dir = weaponDir;
                    go.transform.eulerAngles = new Vector3(0, 0, Vector2.Angle(weaponDir, Vector2.right));
                    go.transform.parent = this.transform.parent;
                    go.transform.localPosition = this.transform.localPosition + new Vector3(weaponDir.x, weaponDir.y) * 30;
                }
            }        
        }
        time = 0;
    }

    private void OnWeaponMove(KeyValues kv)
    {
        weaponDir = ((Vector2)kv.Values).normalized;
    }
}

 

  至此,实现了用右边的虚拟摇杆打手枪的目标。

以上是关于打枪的实现的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net MVC 代码片段问题中的 Jqgrid 实现

代码片段 - Golang 实现集合操作

mapHDOJ5233 Gunner II

如何实现具有不同片段/布局的 ViewPager

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

是否可以动态编译和执行 C# 代码片段?