示例#1
0
		private void Fire()
		{
			if(BDArmorySettings.GameIsPaused)
			{
				if(audioSource.isPlaying)
				{
					audioSource.Stop();	
				}
				return;
			}

			float timeGap = (60/roundsPerMinute) * TimeWarp.CurrentRate;
			
			if(Time.time-timeFired > timeGap && !isOverheated && !pointingAtSelf)
			{
				bool effectsShot = false;
				//Transform[] fireTransforms = part.FindModelTransforms("fireTransform");
				for(int i = 0; i < fireTransforms.Length; i++)
				{
					if(!Misc.CheckMouseIsOnGui() && WMgrAuthorized() && (BDArmorySettings.INFINITE_AMMO || part.RequestResource(ammoName, requestResourceAmount)>0))
					{
						Transform fireTransform = fireTransforms[i];
						spinningDown = false;

						//recoil
						if(hasRecoil)
						{
							gameObject.rigidbody.AddForceAtPosition((-fireTransform.forward) * (bulletVelocity*bulletMass), fireTransform.position, ForceMode.Impulse);
						}
						
						if(!effectsShot)
						{
							//sound
							if(oneShotSound)
							{
								audioSource.Stop();
								audioSource.PlayOneShot(fireSound);
							}
							else
							{
								wasFiring = true;
								if(!audioSource.isPlaying)
								{
									audioSource.clip = fireSound;
									audioSource.loop = false;
									audioSource.time = 0;
									audioSource.Play();	
								}
								else
								{
									if (audioSource.time >= fireSound.length)
									{
										audioSource.time = soundRepeatTime;	
									}
								}
							}

							//animation
							if(hasFireAnimation)
							{
								float unclampedSpeed = (roundsPerMinute*fireState.length)/60;
								fireAnimSpeed = Mathf.Clamp (unclampedSpeed, 1, 20);
								fireState.enabled = true;
								if(unclampedSpeed == fireAnimSpeed)
								{
									fireState.normalizedTime = 0;
								}
								fireState.speed = fireAnimSpeed;
								fireState.normalizedTime = Mathf.Repeat(fireState.normalizedTime, 1);
							}
							
							//muzzle flash

							foreach(Transform mtf in part.FindModelTransforms("muzzleTransform"))
							{
								KSPParticleEmitter pEmitter = mtf.gameObject.GetComponent<KSPParticleEmitter>();
								if(!pEmitter.useWorldSpace || oneShotWorldParticles)
								{
									pEmitter.Emit();
								}
							}

							foreach(var gpe in gaplessEmitters)
							{
								gpe.EmitParticles();	
							}
							
							//shell ejection
							if(BDArmorySettings.EJECT_SHELLS)
							{
								for(int e = 0; e < shellEjectTransforms.Length; e++)
								{
									Transform sTf = shellEjectTransforms[e];
									//GameObject ejectedShell = (GameObject) Instantiate(GameDatabase.Instance.GetModel("BDArmory/Models/shell/model"), sTf.position + (part.rb.velocity*Time.fixedDeltaTime), sTf.rotation);
									GameObject ejectedShell = shellPool.GetPooledObject();
									ejectedShell.transform.position = sTf.position;//+(part.rb.velocity*TimeWarp.fixedDeltaTime);
									ejectedShell.transform.rotation = sTf.rotation;
									ejectedShell.transform.localScale = Vector3.one * shellScale;
									ShellCasing shellComponent = ejectedShell.GetComponent<ShellCasing>();
									shellComponent.initialV = part.rb.velocity;
									ejectedShell.SetActive(true);
								}
							}
							effectsShot = true;
						}
						
						
						//firing bullet
						GameObject firedBullet = bulletPool.GetPooledObject();
						PooledBullet pBullet = firedBullet.GetComponent<PooledBullet>();
						firedBullet.transform.position = fireTransform.position;

						pBullet.mass = bulletMass;
                        pBullet.ballisticCoefficient = bulletBallisticCoefficient;
                        pBullet.flightTimeElapsed = 0;

						timeFired = Time.time;
						
						//Vector3 firedVelocity = fireTransform.rotation * new Vector3(randomZ,randomY,bulletVelocity).normalized * bulletVelocity;
						Vector3 firedVelocity = VectorUtils.WeightedDirectionDeviation(fireTransform.forward, maxDeviation) * bulletVelocity;
					

						//firedBullet.transform.position -= firedVelocity * Time.fixedDeltaTime;
						firedBullet.transform.position += part.rb.velocity * Time.fixedDeltaTime;
						pBullet.currentVelocity = part.rb.velocity + firedVelocity;

						pBullet.initialSpeed = bulletVelocity;
						pBullet.sourceVessel = this.vessel;
						pBullet.bulletTexturePath = bulletTexturePath;
						pBullet.projectileColor = projectileColorC;
						pBullet.startColor = startColorC;
						pBullet.fadeColor = fadeColor;

						tracerIntervalCounter++;
						if(tracerIntervalCounter > tracerInterval)
						{
							tracerIntervalCounter = 0;
							pBullet.tracerStartWidth = tracerStartWidth;
							pBullet.tracerEndWidth = tracerEndWidth;
						}
						else
						{
							pBullet.tracerStartWidth = nonTracerWidth;
							pBullet.tracerEndWidth = nonTracerWidth;
							pBullet.startColor.a *= 0.5f;
							pBullet.projectileColor.a *= 0.5f;
						}

						pBullet.tracerLength = tracerLength;
						pBullet.tracerDeltaFactor = tracerDeltaFactor;
						pBullet.tracerLuminance = tracerLuminance;

						pBullet.bulletDrop = bulletDrop;
						
						if(weaponType == "cannon")
						{
							pBullet.bulletType = PooledBullet.PooledBulletTypes.Explosive;
							pBullet.explModelPath = explModelPath;
							pBullet.explSoundPath = explSoundPath;
							pBullet.blastPower = cannonShellPower;
							pBullet.radius = cannonShellRadius;
							pBullet.airDetonation = airDetonation;
							pBullet.detonationRange = detonationRange;
						}
						else
						{
							pBullet.bulletType = PooledBullet.PooledBulletTypes.Standard;
							pBullet.airDetonation = false;
						}
                        switch(bulletDragType)
                        {
                            case BulletDragTypes.None:
                                pBullet.dragType = PooledBullet.BulletDragTypes.None;
                                break;
                            case BulletDragTypes.AnalyticEstimate:
                                pBullet.dragType = PooledBullet.BulletDragTypes.AnalyticEstimate;
                                break;
                            case BulletDragTypes.NumericalIntegration:
                                pBullet.dragType = PooledBullet.BulletDragTypes.NumericalIntegration;
                                break;
                        }

						pBullet.gameObject.SetActive(true);

						
						//heat
						heat += heatPerShot;
					}
					else
					{
						spinningDown = true;
						if(!oneShotSound && wasFiring)
						{
							audioSource.Stop ();
							wasFiring = false;
							audioSource2.PlayOneShot(overheatSound);	
						}
					}
				}


			}
			else
			{
				spinningDown = true;	
			}
		}