/// <summary>
        /// Set the sprite for a given object
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bankEntry"></param>
        /// <param name="bulletGameObject"></param>
        /// <returns></returns>
        public BulletScript SetBulletSettings(BulletObject bullet, BulletBankEntry bankEntry, GameObject bulletGameObject)
        {
            // Change sprite if the field has been filled
            if (bankEntry.sprite != null)
            {
                SpriteRenderer spriteRenderer = bulletGameObject.GetComponentInChildren <SpriteRenderer>();
                if (spriteRenderer != null)
                {
                    spriteRenderer.sprite = bankEntry.sprite;
                }
            }

            // Make sure we have the appropriate script
            BulletScript bulletScript = bulletGameObject.GetComponent <BulletScript>();

            if (bulletScript == null)
            {
                bulletScript = bulletGameObject.AddComponent <BulletScript>();
            }

            bulletScript.DestroyWhenOutOfScreen = bankEntry.DestroyWhenOutOfScreen;

            // Auto destruction
            if (bankEntry.TimeToLiveInSeconds > 0)
            {
                StartCoroutine(DestroyAfterTimeOut(bullet, bankEntry.TimeToLiveInSeconds));
            }

            return(bulletScript);
        }
        private IEnumerator DestroyAfterTimeOut(BulletObject bullet, float duration)
        {
            yield return(new WaitForSeconds(duration));

            if (bullet != null)
            {
                RemoveBullet(bullet);
            }
        }
        /// <summary>
        /// Create a bullet using the bullet bank
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bulletName"></param>
        /// <returns></returns>
        public BulletScript CreateBulletFromBank(BulletObject bullet, string bulletName)
        {
            // Look in the bank
            BulletBankEntry bankEntry = GetBulletPrefabFromBank(bulletName);

            // Instatiate a prefab
            GameObject bulletGameObject = Instantiate(bankEntry.prefab, bullet.position, Quaternion.identity) as GameObject;

            return(SetBulletSettings(bullet, bankEntry, bulletGameObject));
        }
        /// <summary>
        /// Clean bullet
        /// </summary>
        /// <param name="deadBullet"></param>
        public void RemoveBullet(BulletMLLib.Bullet deadBullet)
        {
            if (OnBulletDestroyed != null)
            {
                OnBulletDestroyed(deadBullet);
                return;
            }

            if (deadBullet is BulletObject)
            {
                BulletObject b = (BulletObject)deadBullet;

                if (b != null && b.Parent != null && b.Parent != null)
                {
                    Destroy(b.Parent);
                }
            }
        }
        /// <summary>
        /// Bullet spawn. Intantiate appropriate prefab.
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bulletName"></param>
        void BulletSpawnedHandler(BulletObject bullet, string bulletName)
        {
            BulletScript bulletScript = null;

            if (OnBulletSpawned != null)
            {
                bulletScript = OnBulletSpawned(bullet, bulletName);
            }
            else
            {
                bulletScript = CreateBulletFromBank(bullet, bulletName);
            }

            // Bullet properties
            bulletScript.Bullet           = bullet;
            bulletScript.Bullet.Scale     = scale;
            bulletScript.Bullet.TimeSpeed = timeSpeed;
        }
        /// <summary>
        /// New bullet creation
        /// </summary>
        /// <remarks>Not the bullet spawn, just the container creation, we don't know the bullet name here</remarks>
        /// <returns></returns>
        public BulletMLLib.Bullet CreateBullet(BulletMLLib.Bullet source, bool top)
        {
            // Try to get the parent
            GameObject         gameObject = null;
            BulletSourceScript emitter    = null;

            if (source is TopBullet)
            {
                emitter = ((TopBullet)source).Parent;
                if (emitter != null)
                {
                    gameObject = emitter.gameObject;
                }
            }
            else if (source is BulletObject)
            {
                gameObject = ((BulletObject)source).Parent;
            }

            // Create a top bullet (weird case)
            if (top)
            {
                return(new TopBullet(this, emitter));
            }
            else
            {
                // Create a bullet
                BulletObject bullet = null;
                if (OnBulletCreated != null)
                {
                    bullet = OnBulletCreated(gameObject);
                }
                else
                {
                    bullet = new BulletObject(this, gameObject);
                }
                bullet.OnBulletSpawned += BulletSpawnedHandler;

                return(bullet);
            }
        }
Beispiel #7
0
 private IEnumerator DestroyAfterTimeOut(BulletObject bullet, float duration)
 {
     yield return new WaitForSeconds(duration);
     if (bullet != null)
     {
         RemoveBullet(bullet);
     }
 }
Beispiel #8
0
        /// <summary>
        /// Set the sprite for a given object
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bankEntry"></param>
        /// <param name="bulletGameObject"></param>
        /// <returns></returns>
        public BulletScript SetBulletSettings(BulletObject bullet, BulletBankEntry bankEntry, GameObject bulletGameObject)
        {
            // Change sprite if the field has been filled
            if (bankEntry.sprite != null)
            {
                SpriteRenderer spriteRenderer = bulletGameObject.GetComponentInChildren<SpriteRenderer>();
                if (spriteRenderer != null)
                {
                    spriteRenderer.sprite = bankEntry.sprite;
                }
            }

            // Make sure we have the appropriate script
            BulletScript bulletScript = bulletGameObject.GetComponent<BulletScript>();
            if (bulletScript == null)
            {
                bulletScript = bulletGameObject.AddComponent<BulletScript>();
            }

            bulletScript.DestroyWhenOutOfScreen = bankEntry.DestroyWhenOutOfScreen;

            // Auto destruction
            if (bankEntry.TimeToLiveInSeconds > 0)
            {
                StartCoroutine(DestroyAfterTimeOut(bullet, bankEntry.TimeToLiveInSeconds));
            }

            return bulletScript;
        }
Beispiel #9
0
        /// <summary>
        /// Create a bullet using the bullet bank
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bulletName"></param>
        /// <returns></returns>
        public BulletScript CreateBulletFromBank(BulletObject bullet, string bulletName)
        {
            // Look in the bank
            BulletBankEntry bankEntry = GetBulletPrefabFromBank(bulletName);

            // Instatiate a prefab
            GameObject bulletGameObject = Instantiate(bankEntry.prefab, bullet.position, Quaternion.identity) as GameObject;
            bulletGameObject.transform.parent = bulletParent;

            return SetBulletSettings(bullet, bankEntry, bulletGameObject);
        }
Beispiel #10
0
        /// <summary>
        /// Bullet spawn. Intantiate appropriate prefab.
        /// </summary>
        /// <param name="bullet"></param>
        /// <param name="bulletName"></param>
        void BulletSpawnedHandler(BulletObject bullet, string bulletName)
        {
            BulletScript bulletScript = null;

            if (OnBulletSpawned != null)
            {
                bulletScript = OnBulletSpawned(bullet, bulletName);
            }
            else
            {
                bulletScript = CreateBulletFromBank(bullet, bulletName);
            }

            // Bullet properties
            bulletScript.Bullet = bullet;
            bulletScript.Bullet.Scale = scale;
            bulletScript.Bullet.TimeSpeed = timeSpeed;
        }
Beispiel #11
0
        /// <summary>
        /// New bullet creation
        /// </summary>
        /// <remarks>Not the bullet spawn, just the container creation, we don't know the bullet name here</remarks>
        /// <returns></returns>
        public BulletMLLib.Bullet CreateBullet(BulletMLLib.Bullet source, bool top)
        {
            // Try to get the parent
            GameObject gameObject = null;
            BulletSourceScript emitter = null;

            if (source is TopBullet)
            {
                emitter = ((TopBullet)source).Parent;
                if (emitter != null)
                {
                    gameObject = emitter.gameObject;
                }
            }
            else if (source is BulletObject)
            {
                gameObject = ((BulletObject)source).Parent;
            }

            // Create a top bullet (weird case)
            if (top)
            {
                return new TopBullet(this, emitter);
            }
            else
            {
                // Create a bullet
                BulletObject bullet = null;
                if (OnBulletCreated != null)
                {
                    bullet = OnBulletCreated(gameObject);
                }
                else
                {
                    bullet = new BulletObject(this, gameObject);
                }
                bullet.OnBulletSpawned += BulletSpawnedHandler;

                return bullet;
            }
        }