Example #1
0
 /// <summary>
 /// <para>Instantiates the first parent asteroids with a random initial direction</para>
 /// <para>For each asteroid spawn it calls InstantiateChildrenPrefabs</para>
 /// </summary>
 /// <param name="asteroidPrefab"></param>
 private void InstantiateRandomAsteroidPrefab(AsteroidPrefab asteroidPrefab)
 {
     for (var i = 0; i < asteroidPrefab.initialQuantity; i++)
     {
         var asteroid = Instantiate(asteroidPrefab.prefab, GetRandomPosition(), Quaternion.identity, _asteroidsAnchor);
         var asteroidTransform = asteroid.transform;
         asteroid.SetInitialDirection(Quaternion.Euler(0, 0, Random.Range(0, 360)) * asteroidTransform.up);
         asteroid.OnDestroy += () => _onAsteroidDestroyed(asteroid);
         _asteroids.Add(asteroid);
         asteroid.Children = InstantiateChildrenPrefabs(asteroidPrefab, asteroid);
         asteroid.ActivateRigidbody();
     }
 }
Example #2
0
        /// <summary>
        /// <para>Instantiates asteroids with the given prefab and quantity. Sets its parent to the parent passed
        /// as parameter. It gives the asteroids a random position inside the parent and a random rotation</para>
        /// </summary>
        /// <param name="asteroidPrefab"></param>
        /// <param name="parent">The parent for the instantiated asteroids</param>
        /// <param name="quantity">How many asteroids will be instantiated from the prefab</param>
        /// <returns>Spawned children</returns>
        private List<Asteroid> InstantiateChildrenAsteroidPrefabs(AsteroidPrefab asteroidPrefab, Asteroid parent, int quantity)
        {
            var children = new List<Asteroid>();
            var parentTransform = parent.transform;
            var separationBetweenAsteroids = 360 / quantity;
            var initialAngle = Random.Range(0, 360);
            var distance = asteroidSpawnerScriptableObject.spaceBetweenChildren;
            for (var i = 0; i < quantity; i++)
            {
                var rotation = Quaternion.Euler(0 ,0 ,(initialAngle + i *separationBetweenAsteroids) % 360);
                var offset = rotation * parentTransform.up * distance;
                var asteroid = Instantiate(asteroidPrefab.prefab, parentTransform.position + offset, Quaternion.identity, 
                    parentTransform);
                asteroid.Parent = parent;
                asteroid.SetInitialDirection(offset.normalized);
                asteroid.OnDestroy += () => _onAsteroidDestroyed(asteroid);
                _asteroids.Add(asteroid);
                children.Add(asteroid);
            }

            return children;
        }
Example #3
0
        /// <summary>
        /// <para>Searches the correspond prefab for the children of the parent and then calls the method
        /// InstantiateChildrenAsteroidPrefabs with the prefab found</para>
        /// </summary>
        /// <param name="asteroidPrefab">Children prefab</param>
        /// <param name="parent"></param>
        /// <returns>Spawned children</returns>
        private List<Asteroid> InstantiateChildrenPrefabs(AsteroidPrefab asteroidPrefab, Asteroid parent)
        {
            var asteroids = new List<Asteroid>();
            if (asteroidPrefab.childrenQuantity == 0) return asteroids;
            for (var i = 0; i < asteroidSpawnerScriptableObject.asteroidPrefabs.Length; i++)
            {
                var prefab = asteroidSpawnerScriptableObject.asteroidPrefabs[i];

                if (asteroidPrefab != prefab ||
                    i == asteroidSpawnerScriptableObject.asteroidPrefabs.Length - 1) continue;

                var prefabToSpawn = asteroidSpawnerScriptableObject.asteroidPrefabs[i + 1];
                var result = InstantiateChildrenAsteroidPrefabs(prefabToSpawn, parent, asteroidPrefab.childrenQuantity);
                foreach (var asteroidChildren in result)
                {
                    asteroidChildren.Children = InstantiateChildrenPrefabs(prefabToSpawn, asteroidChildren);
                }

                return result;
            }

            return asteroids;
        }
 public bool Equals(AsteroidPrefab other)
 {
     return(Equals(prefab, other.prefab) && size == other.size && childrenQuantity == other.childrenQuantity &&
            initialQuantity == other.initialQuantity);
 }