/// <summary> /// Creates an environment at the given location, pos. /// </summary> /// <param name="towerPos"></param> public void CreateEnvironment(Vector2 pos, float?scaleFactor = null, string imagePath = "rectangle", Color?overlayColor = null, Rectangle?sourceRectangle = null, Vector2?scale = null) { if (InactiveEnvironmentList.Count > 0) { GameObject go = InactiveEnvironmentList[0]; InactiveEnvironmentList.Remove(go); go.components.RemoveAll(c => c is SpriteRenderer); go.AddComponnent(new SpriteRenderer(go, imagePath, 0f, 1f, scaleFactor, sourceRectangle, scale)); go.GetComponent <SpriteRenderer>().Color = overlayColor.HasValue ? overlayColor.Value : Color.White; AddActive.Add(InactiveEnvironmentList[0]); InactiveEnvironmentList.Remove(InactiveEnvironmentList[0]); } else { GameObject obj = new GameObject(pos); obj.AddComponnent(new SpriteRenderer(obj, imagePath, 0f, 1f, scaleFactor, sourceRectangle, scale)); obj.GetComponent <SpriteRenderer>().Color = overlayColor == null ? Color.Black : overlayColor.Value; obj.AddComponnent(new Environment(obj)); obj.LoadContent(GameWorld.Instance.Content); obj.AddComponnent(new CollisionRectangle(obj)); obj.GetComponent <CollisionRectangle>().LoadContent(GameWorld.Instance.Content); AddActive.Add(obj); } }
IEnumerator GenerateEnemy(ObjectPooler enemyPool, AddActive addActive, GetActive getActive, int phase) { int active = 0; while (true) { if (generate) { // Determine how many enemies are currently active. active = getActive(); // Get the current phase to determine how many enemies should be generated. int max = References.global.phaseManager.phaseMultipliers[phase]; //if( max >= 3 ) // max = 3; // Generate the appropriate number of enemies. while (active < max) { CreateObject(enemyPool); active = addActive(); // Wait a short time between each generated enemy. yield return(new WaitForSeconds(0.25f)); } } // Wait 1 second before checking if more enemies should be generated. yield return(new WaitForSeconds(1)); } }
/// <summary> /// Adds gameObjects from AddActive list to their correct active lists. /// </summary> public void AddToActive() { for (int i = 0; i < AddActive.Count(); i++) { GameObject go = AddActive[i]; if (go.GetComponent <Clutter>() != null) { ActiveClutterList.Add(go); if (InactiveClutterList.Contains(go)) { InactiveClutterList.Remove(go); } } if (go.GetComponent <Projectile>() != null) { ActiveProjectileList.Add(go); if (InactiveProjectileList.Contains(go)) { InactiveProjectileList.Remove(go); } } if (go.GetComponent <Environment>() != null) { ActiveEnvironmentList.Add(go); if (InactiveEnvironmentList.Contains(go)) { InactiveEnvironmentList.Remove(go); } } } AddActive.Clear(); }
void Start() { if (generate) { // Initialize delegates. AddActive addA = AddEnemyTypeA; AddActive addB = AddEnemyTypeB; GetActive getA = GetEnemyTypeA; GetActive getB = GetEnemyTypeB; // Initialize object pools. enemyPoolA.Initialize(enemyA, 5, trans); enemyPoolB.Initialize(enemyB, 5, trans); // Start generation coroutines. StartCoroutine(GenerateEnemy(enemyPoolA, addA, getA, (int)References.GamePhases.TypeAEnemyPhase)); StartCoroutine(GenerateEnemy(enemyPoolB, addB, getB, (int)References.GamePhases.TypeBEnemyPhase)); } }