public static void CreateExplosion(Vector3 position, float tntMassEquivalent, string explModelPath, string soundPath, bool isMissile = true, float caliber = 0, Part explosivePart = null, Vector3 direction = default(Vector3)) { if (!HighLogic.LoadedSceneIsFlight) { return; } var go = GameDatabase.Instance.GetModel(explModelPath); var soundClip = GameDatabase.Instance.GetAudioClip(soundPath); Quaternion rotation; if (direction == default(Vector3)) { rotation = Quaternion.LookRotation(VectorUtils.GetUpDirection(position)); } else { rotation = Quaternion.LookRotation(direction); } GameObject newExplosion = (GameObject)Instantiate(go, position, rotation); ExplosionFx eFx = newExplosion.AddComponent <ExplosionFx>(); eFx.ExSound = soundClip; eFx.AudioSource = newExplosion.AddComponent <AudioSource>(); eFx.AudioSource.minDistance = 200; eFx.AudioSource.maxDistance = 5500; eFx.AudioSource.spatialBlend = 1; eFx.Range = BlastPhysicsUtils.CalculateBlastRange(tntMassEquivalent); eFx.AudioSource.maxDistance = eFx.Range; eFx.Position = position; eFx.Power = tntMassEquivalent; eFx.IsMissile = isMissile; eFx.Caliber = caliber; eFx.ExplosivePart = explosivePart; eFx.Direction = direction; if (tntMassEquivalent <= 5) { eFx.AudioSource.minDistance = 4f; eFx.AudioSource.maxDistance = 3000; eFx.AudioSource.priority = 9999; } newExplosion.SetActive(true); IEnumerator <KSPParticleEmitter> pe = newExplosion.GetComponentsInChildren <KSPParticleEmitter>().Cast <KSPParticleEmitter>() .GetEnumerator(); while (pe.MoveNext()) { if (pe.Current == null) { continue; } pe.Current.emit = true; } pe.Dispose(); }
public static void CreateExplosion(Vector3 position, float tntMassEquivalent, string explModelPath, string soundPath, ExplosionSourceType explosionSourceType, float caliber = 0, Part explosivePart = null, string sourceVesselName = null, string sourceWeaponName = null, Vector3 direction = default(Vector3), bool isfx = false) { CreateObjectPool(explModelPath, soundPath); Quaternion rotation; if (direction == default(Vector3)) { rotation = Quaternion.LookRotation(VectorUtils.GetUpDirection(position)); } else { rotation = Quaternion.LookRotation(direction); } GameObject newExplosion = explosionFXPools[explModelPath + soundPath].GetPooledObject(); newExplosion.transform.SetPositionAndRotation(position, rotation); ExplosionFx eFx = newExplosion.GetComponent <ExplosionFx>(); eFx.Range = BlastPhysicsUtils.CalculateBlastRange(tntMassEquivalent); eFx.Position = position; eFx.Power = tntMassEquivalent; eFx.ExplosionSource = explosionSourceType; eFx.SourceVesselName = sourceVesselName != null ? sourceVesselName : explosionSourceType == ExplosionSourceType.Missile ? (explosivePart != null && explosivePart.vessel != null ? explosivePart.vessel.GetName() : null) : null; // Use the sourceVesselName if specified, otherwise get the sourceVesselName from the missile if it is one. eFx.SourceWeaponName = sourceWeaponName; eFx.Caliber = caliber; eFx.ExplosivePart = explosivePart; eFx.Direction = direction; eFx.isFX = isfx; eFx.pEmitters = newExplosion.GetComponentsInChildren <KSPParticleEmitter>(); eFx.audioSource = newExplosion.GetComponent <AudioSource>(); if (tntMassEquivalent <= 5) { eFx.audioSource.minDistance = 4f; eFx.audioSource.maxDistance = 3000; eFx.audioSource.priority = 9999; } newExplosion.SetActive(true); }
void Detonate() { if (!parentPart.partName.Contains("exploding")) { bool excessFuel = false; parentPart.partName += "exploding"; PartResource fuel = parentPart.Resources.Where(pr => pr.resourceName == "LiquidFuel").FirstOrDefault(); PartResource ox = parentPart.Resources.Where(pr => pr.resourceName == "Oxidizer").FirstOrDefault(); if (fuel != null) { tntMassEquivilent += Mathf.Clamp((float)fuel.amount, ((float)fuel.maxAmount * 0.05f), ((float)fuel.maxAmount * 0.2f)); if (fuel != null && ox != null) { tntMassEquivilent += Mathf.Clamp((float)ox.amount, ((float)ox.maxAmount * 0.1f), ((float)ox.maxAmount * 0.3f)); tntMassEquivilent *= 1.3f; } if (fuel.amount > fuel.maxAmount * 0.3f) { excessFuel = true; } } PartResource mp = parentPart.Resources.Where(pr => pr.resourceName == "MonoPropellant").FirstOrDefault(); if (mp != null) { tntMassEquivilent += Mathf.Clamp((float)mp.amount, ((float)mp.maxAmount * 0.1f), ((float)mp.maxAmount * 0.3f)); if (mp.amount > mp.maxAmount * 0.3f) { excessFuel = true; } } PartResource ec = parentPart.Resources.Where(pr => pr.resourceName == "ElectricCharge").FirstOrDefault(); if (ec != null) { tntMassEquivilent += ((float)ec.maxAmount / 5000); //fix for cockpit batteries weighing a tonne+ ec.maxAmount = 0; ec.isVisible = false; parentPart.RemoveResource(ec);//destroy battery. not calling part.destroy, since some batteries in cockpits. Misc.Misc.RefreshAssociatedWindows(parentPart); } if (excessFuel) { float blastRadius = BlastPhysicsUtils.CalculateBlastRange(tntMassEquivilent); using (var blastHits = Physics.OverlapSphere(parentPart.transform.position, blastRadius, 9076737).AsEnumerable().GetEnumerator()) { while (blastHits.MoveNext()) { if (blastHits.Current == null) { continue; } try { Part partHit = blastHits.Current.GetComponentInParent <Part>(); if (partHit != null && partHit.mass > 0) { Rigidbody rb = partHit.Rigidbody; Vector3 distToG0 = parentPart.transform.position - partHit.transform.position; Ray LoSRay = new Ray(parentPart.transform.position, partHit.transform.position - parentPart.transform.position); RaycastHit hit; if (Physics.Raycast(LoSRay, out hit, distToG0.magnitude, 9076737)) { KerbalEVA eva = hit.collider.gameObject.GetComponentUpwards <KerbalEVA>(); Part p = eva ? eva.part : hit.collider.gameObject.GetComponentInParent <Part>(); if (p == partHit) { if (rb == null) { return; } BulletHitFX.AttachFire(hit, p, 1, SourceVessel, 20); } } } } catch (Exception e) { Debug.LogWarning("[BDArmory.FireFX]: Exception thrown in Detonate: " + e.Message + "\n" + e.StackTrace); } } } } ExplosionFx.CreateExplosion(parentPart.transform.position, tntMassEquivilent, explModelPath, explSoundPath, ExplosionSourceType.Bullet, 0, null, parentPart.vessel != null ? parentPart.vessel.name : null, null); // needs to be Explosiontype Bullet since missile only returns Module MissileLauncher gameObject.SetActive(false); } }