public void Explode() { ExplosionInfo explosion = new ExplosionInfo() { Position = transform.position, BlastForce = blastForce, Radius = blastRadius, UpwardModifier = explosionUpwardPush }; Collider[] objectsInRange = Physics.OverlapSphere(transform.position, blastRadius); // Apply force and damage to colliders and rigidbodies in range of the explosion foreach (Collider col in objectsInRange) { // Ignore terrain colliders if (col is TerrainCollider) continue; // Ignore self (the rocket) if (col == this.GetComponent<Collider>()) continue; // Apply explosive force if a rigidbody could be found on this object Rigidbody rbody = col.attachedRigidbody; if (rbody != null && !rbody.isKinematic) rbody.AddExplosionForce(blastForce, transform.position, blastRadius, explosionUpwardPush); // Check for Chip-Away Debris ChipAwayDebris chipAwayDebris = col.gameObject.GetComponent<ChipAwayDebris>(); if (chipAwayDebris != null) { if (Random.Range(1, 100) > 50) // Do this about half the time... { chipAwayDebris.BreakOff(blastForce, transform.position, blastRadius, explosionUpwardPush); continue; //Skip the destructible check if the debris hasn't chipped away yet. } else continue; } // If it's a Destructible object, apply damage to it based on proximity to the blast Destructible destructibleObj = col.gameObject.GetComponentInParent<Destructible>(); if (destructibleObj != null) { float proximity = (transform.position - col.transform.position).magnitude; float effectAmount = 1 - (proximity / blastRadius); if (effectAmount > 0f) destructibleObj.ApplyExplosiveDamage(explosion.AdjustEffect(effectAmount)); } } }
//TODO: Combine these methods into one ApplyDamage overload, and make ExplosionInfo and ImpactInfo inherit from an interface or base class. public void ApplyExplosiveDamage(ExplosionInfo explosionInfo) { if (this.IsDestroyed || this.IsInvulnerable) return; // don't try to apply damage to an already-destroyed object. //Debug.Log("Current hit points: " + CurrentHitPoints + "; blast force: " + explosionInfo.BlastForce); currentHitPoints -= explosionInfo.Damage; CheckForObliterate(explosionInfo.Damage); SetDamageLevel(); if (currentHitPoints <= 0) { this.IsDestroyed = true; PlayDamageLevelEffects(); if (canBeDestroyed) DestructionManager.Instance.ProcessDestruction(this, destroyedPrefab, explosionInfo, IsObliterated); } }