コード例 #1
0
        private static void CheckForISpawnable(GameObject obj, SpawnAreaName areaName, string objectTag)
        {
            ISpawnable pooledObject = obj.GetComponent <ISpawnable>();

            pooledObject?.SetEnemySpawnName(objectTag);
            // TODO: Revisar si se necesita eventualmente
            pooledObject?.SetSpawnArea(areaName);
        }
コード例 #2
0
        private static SpawnPositionAndArea GetSpawningPosition(IReadOnlyList <SpawnArea> spawnAreas)
        {
            // Choose an area to spawn the enemy
            int randomArea = Random.Range(0, spawnAreas.Count);

            // Get the boundaries of the area
            Bounds bounds = spawnAreas[randomArea].spawnArea.bounds;

            // Grab the area's name
            SpawnAreaName areaName = spawnAreas[randomArea].spawnAreaName;

            // Get a spawning point inside the boundaries
            float xValue = Random.Range(bounds.min.x, bounds.max.x);
            float yValue = Random.Range(bounds.min.y, bounds.max.y);

            // Build and return the class
            return(new SpawnPositionAndArea(areaName, new Vector2(xValue, yValue)));
        }
コード例 #3
0
 public SpawnPositionAndArea(SpawnAreaName areaName, Vector2 position)
 {
     spawnAreaName = areaName;
     spawnPosition = position;
 }
コード例 #4
0
 public void SetSpawnArea(SpawnAreaName areaName) => _spawnArea = areaName;
コード例 #5
0
        private GameObject SpawnObjectFromPool(string objectTag, Vector3 position, Quaternion rotation, SpawnAreaName areaName)
        {
            GameObject objectToSpawn = _poolDictionary[objectTag].Dequeue();

            objectToSpawn.SetActive(true);
            objectToSpawn.transform.position = position;
            objectToSpawn.transform.rotation = rotation;
            if (areaName != SpawnAreaName.None)
            {
                // Check if it's an enemy and pass the area name
                CheckForISpawnable(objectToSpawn, areaName, objectTag);
            }
            // Check if it's an object with the OnSpawn Method
            CheckForIPooledObject(objectToSpawn);
            return(objectToSpawn);
        }
コード例 #6
0
        public GameObject Spawn(string objectTag, Vector3 position, Quaternion rotation, SpawnAreaName areaName)
        {
            if (!_poolDictionary.ContainsKey(objectTag))
            {
                // Pool doesn't exist
                return(null);
            }
            // Find the pool that corresponds to the spawned object.
            Pool nextObjectToSpawnPool = pools.Find(pool => pool.tag.Equals(objectTag));

            if (nextObjectToSpawnPool.recycle)
            {
                // Spawn the object
                GameObject objectToSpawn = SpawnObjectFromPool(objectTag, position, rotation, areaName);
                // Place the object inside the queue
                _poolDictionary[objectTag].Enqueue(objectToSpawn);
                return(objectToSpawn);
            }
            if (_poolDictionary[objectTag].Count > 0)
            {
                // Spawn the object
                GameObject objectToSpawn = SpawnObjectFromPool(objectTag, position, rotation, areaName);
                return(objectToSpawn);
            }
            // Pool empty
            return(null);
        }