Example #1
0
    /// <summary>
    /// Releases bullet (back to pool or destroy).
    /// </summary>
    public void ReleaseBullet(UbhBullet bullet, bool destroy = false)
    {
        if (bullet == null || bullet.gameObject == null)
        {
            return;
        }

        bullet.OnFinishedShot();

        UbhBulletManager.instance.RemoveBullet(bullet);

#if USING_CORE_GAME_KIT
        // +++++ Replace PoolingSystem with DarkTonic's CoreGameKit. +++++
        InitializePoolBoss();
        PoolBoss.Despawn(bullet.transform);
#else
        if (destroy)
        {
            Destroy(bullet.gameObject);
            Destroy(bullet);
            bullet = null;
            return;
        }
        bullet.SetActive(false);
#endif
    }
Example #2
0
    /// <summary>
    /// Get Bullet from object pool or instantiate.
    /// </summary>
    public UbhBullet GetBullet(GameObject goPrefab, Vector3 position, bool forceInstantiate = false)
    {
        if (goPrefab == null)
        {
            return(null);
        }

        UbhBullet bullet = null;

#if USING_CORE_GAME_KIT
        // +++++ Replace PoolingSystem with DarkTonic's CoreGameKit. +++++
        InitializePoolBoss();
        Transform trans = PoolBoss.Spawn(goPrefab.transform, position, UbhUtil.QUATERNION_IDENTITY, transform);
        if (trans == null)
        {
            return(null);
        }

        bullet = trans.gameObject.GetComponent <UbhBullet>();
        if (bullet == null)
        {
            bullet = trans.gameObject.AddComponent <UbhBullet>();
        }
#else
        int key = goPrefab.GetInstanceID();

        if (m_pooledBulletDic.ContainsKey(key) == false)
        {
            m_pooledBulletDic.Add(key, new PoolingParam());
        }

        PoolingParam poolParam = m_pooledBulletDic[key];

        if (forceInstantiate == false && poolParam.m_bulletList.Count > 0)
        {
            if (poolParam.m_searchStartIndex < 0 || poolParam.m_searchStartIndex >= poolParam.m_bulletList.Count)
            {
                poolParam.m_searchStartIndex = poolParam.m_bulletList.Count - 1;
            }

            for (int i = poolParam.m_searchStartIndex; i >= 0; i--)
            {
                if (poolParam.m_bulletList[i] == null || poolParam.m_bulletList[i].gameObject == null)
                {
                    poolParam.m_bulletList.RemoveAt(i);
                    continue;
                }
                if (poolParam.m_bulletList[i].isActive == false)
                {
                    poolParam.m_searchStartIndex = i - 1;
                    bullet = poolParam.m_bulletList[i];
                    bullet.SetActive(true);
                    break;
                }
            }
            if (bullet == null)
            {
                for (int i = poolParam.m_bulletList.Count - 1; i > poolParam.m_searchStartIndex; i--)
                {
                    if (poolParam.m_bulletList[i] == null || poolParam.m_bulletList[i].gameObject == null)
                    {
                        poolParam.m_bulletList.RemoveAt(i);
                        continue;
                    }
                    if (i < poolParam.m_bulletList.Count && poolParam.m_bulletList[i].isActive == false)
                    {
                        poolParam.m_searchStartIndex = i - 1;
                        bullet = poolParam.m_bulletList[i];
                        bullet.SetActive(true);
                        break;
                    }
                }
            }
        }

        if (bullet == null)
        {
            //edit by feng


            GameObject go = Instantiate(goPrefab, transform);
            bullet = go.GetComponent <UbhBullet>();
            if (bullet == null)
            {
                bullet = go.AddComponent <UbhBullet>();
            }
            poolParam.m_bulletList.Add(bullet);
            poolParam.m_searchStartIndex = poolParam.m_bulletList.Count - 1;
            bullet.SetActive(true);
        }

        bullet.transform.SetPositionAndRotation(position, UbhUtil.QUATERNION_IDENTITY);
#endif

        UbhBulletManager.instance.AddBullet(bullet);

        return(bullet);
    }