private void killMissile() { // The missile was already "killed" either by way of despawning or by Destroy -- don't kill it again if (!gameObject.activeInHierarchy) { return; } bool dodestroy = true; if (firedBy != null) { // If the firedBy game object is not an enemy (it is a player), then notify // the gameobject that this particular missile is no loner "alive". Basically, allowing a // "reset" for the player missile, as only 1 player missile can exist at a time. if (firedBy.tag != "Enemy") { firedBy.SendMessage("firedMissileDead", SendMessageOptions.DontRequireReceiver); } // Missile was fired by an "enemy" --- so, instead of destroying the missile, despawn it else { RFObjectPool.Despawn(this.gameObject); dodestroy = false; } firedBy = null; } if (dodestroy) { gameObject.SetActive(false); Destroy(this.gameObject); } }
/// <summary> /// Called when an enemy should fire. If there are already MAX_ENEMY_MISSILES, then no firing will /// take place. Checks the ACTIVE CHILD count, since pooling is used to spawn/despawn (activeate/deactive) /// enemy missiles. /// </summary> /// <param name="enemy"></param> private bool enemyFireMissile(EnemyController enemy) { int activeCount = 0; bool fired = false; for (int cnt = 0; cnt < EnemyMissileContainer.childCount; cnt++) { if (EnemyMissileContainer.GetChild(cnt).gameObject.activeInHierarchy == true) { activeCount++; } } // Fire a missile from the invader if the maximum number of active missiles don't already exist. if (activeCount < GameGlobals.MAX_ENEMY_MISSILES) { GameObject missile = RFObjectPool.Spawn(EnemyMissilePrefab, Vector3.zero, Quaternion.identity); if (missile != null) { MissileController mc = missile.GetComponent <MissileController>(); mc.EnemyFired(enemy.gameObject); fired = true; } } return(fired); }
// Update is called once per frame void Update() { Vector3 pos = transform.position; pos.y += (yvel * Time.deltaTime); transform.position = pos; // Spawn a trace. Traces have a limited lifetime; they don't actully move--just shrink down and despawn // However, there can be quite a few on screen at once, so they are spawned/despawed with an object pool if (dropTime <= 0.0f) { GameObject trace = RFObjectPool.Spawn("MissileTrail", pos, Quaternion.identity); dropTime = TracerSpawnDelay; } dropTime -= Time.deltaTime; if (pos.y > GameGlobals.MISSILE_CONSTRAINT_Y || pos.y < -GameGlobals.MISSILE_CONSTRAINT_Y) { killMissile(); } }