コード例 #1
0
        private IPoolAble CreateNew(InstantiationMethod instantiationMethod)
        {
            IPoolAble poolAble = instantiationMethod(this);

            poolAble.Inactivated += PoolAble_Inactivated;
            return(poolAble);
        }
コード例 #2
0
        private void PoolAble_Inactivated(object sender, PoolAbleEventArgs e)
        {
            var hashCode = e.HashCode;
            int max      = pools[hashCode].Max;

            if (pools[hashCode].PoolingObjects.Count >= max)
            {
                IPoolAble poolAble = sender as IPoolAble;
                poolAble.Inactivated -= PoolAble_Inactivated;
                MonoBehaviour behaviour = poolAble as MonoBehaviour;
                pools[hashCode].PoolingObjects.Remove(poolAble);
                Destroy(behaviour);
            }
        }
コード例 #3
0
        public void ChangeBullet(GameObject newBullet)
        {
            BulletPrefab = newBullet;
            GenericObjectPool genericObjectPool = GenericObjectPool.Current;

            genericObjectPool.Init(BulletPrefab.GetHashCode(), behaviour =>
            {
                GameObject bullet = (GameObject)Instantiate(
                    BulletPrefab);

                IPoolAble poolAble = bullet.GetComponent <IPoolAble>();
                poolAble.Init(BulletPrefab.GetHashCode());
                return(poolAble);
            }, 20, 100);
        }
コード例 #4
0
        public override void Start()
        {
            InitWeaponHolder();

            GenericObjectPool genericObjectPool = GenericObjectPool.Current;

            genericObjectPool.Init(BulletPrefab.GetHashCode(), behaviour =>
            {
                GameObject bullet = (GameObject)Instantiate(
                    BulletPrefab);

                IPoolAble poolAble = bullet.GetComponent <IPoolAble>();
                poolAble.Init(BulletPrefab.GetHashCode());
                return(poolAble);
            }, 20, 100);
        }
コード例 #5
0
        public virtual void Attack()
        {
            if (Ammunition == 0)
            {
                return;
            }
            if (Time.time - _lastShootTime < ReloadTime)
            {
                return;
            }
            if (transform.CompareTag("Player") && holdingType == PlayerAnimation.WeaponType.Melee)
            {
                GetComponent <PlayerAnimation>().AnimateSlash();
            }

            Quaternion rotation = BulletSpawnPosition.root.rotation;



            IPoolAble poolAble = GenericObjectPool.Current.Get(BulletPrefab.GetHashCode());

            Bullet     bullet1          = poolAble as Bullet;
            GameObject bulletGameObject = bullet1.gameObject;

            bulletGameObject.transform.parent   = null;
            bulletGameObject.transform.position = BulletSpawnPosition.position;
            bullet1.spawnPosition = BulletSpawnPosition.position;
            bulletGameObject.transform.rotation = rotation;

            AudioManager.PlayShootSound(InventaryItemName, _bulletSpawnPosition, transform.tag);

            //var bullet = (GameObject)Instantiate(
            //BulletPrefab,
            //BulletSpawnPosition.position,
            //rotation);
            //int code = bullet.GetHashCode();
            if (Ammunition != -1)
            {
                Ammunition--;
            }

            _lastShootTime  = Time.time;
            bullet1.shooter = gameObject;
            bullet1.SetActive();
        }
コード例 #6
0
        public void Init(int hashCode, InstantiationMethod instantiationMethod, int min = 10, int max = 20)
        {
            if (!pools.ContainsKey(hashCode))
            {
                PoolingElementContext poolingElementContext = new PoolingElementContext();
                poolingElementContext.InstationMethod = instantiationMethod;
                poolingElementContext.Min             = min;
                poolingElementContext.Max             = max;
                poolingElementContext.PoolingObjects  = new List <IPoolAble>();
                pools.Add(hashCode, poolingElementContext);

                List <IPoolAble> poolAbles = pools[hashCode].PoolingObjects;

                for (int i = 0; i < min; i++)
                {
                    IPoolAble poolAble = CreateNew(instantiationMethod);
                    poolAbles.Add(poolAble);
                }
            }
        }
コード例 #7
0
        public IPoolAble Get(int hashCode)
        {
            var context = pools[hashCode];

            IPoolAble poolAbleToReturn = null;

            foreach (var poolAble in context.PoolingObjects)
            {
                if (poolAble.IsInActive)
                {
                    poolAbleToReturn = poolAble;
                    break;
                }
            }

            if (poolAbleToReturn == null)
            {
                IPoolAble poolAble = CreateNew(context.InstationMethod);
                context.PoolingObjects.Add(poolAble);
                poolAbleToReturn = poolAble;
            }
            return(poolAbleToReturn);
        }