Exemplo n.º 1
0
 public static void ShowGUI(BDModularGuidance missile_module)
 {
     if (instance != null)
     {
         instance.missile_module = missile_module;
         instance.UpdateGUIState();
     }
 }
Exemplo n.º 2
0
        public void CheckDetonationState()
        {
            //Guard clauses
            if (!TargetAcquired)
            {
                return;
            }

            var targetDistancePerFrame  = TargetVelocity * Time.fixedDeltaTime;
            var missileDistancePerFrame = vessel.Velocity() * Time.fixedDeltaTime;

            var futureTargetPosition  = (TargetPosition + targetDistancePerFrame);
            var futureMissilePosition = (vessel.CoM + missileDistancePerFrame);

            var relativeSpeed = (TargetVelocity - vessel.Velocity()).magnitude * Time.fixedDeltaTime;

            switch (DetonationDistanceState)
            {
            case DetonationDistanceStates.NotSafe:
                //Lets check if we are at a safe distance from the source vessel

                using (var hitsEnu = Physics.OverlapSphere(futureMissilePosition, GetBlastRadius() * 3f, 557057).AsEnumerable().GetEnumerator())
                {
                    while (hitsEnu.MoveNext())
                    {
                        if (hitsEnu.Current == null)
                        {
                            continue;
                        }
                        try
                        {
                            Part partHit = hitsEnu.Current.GetComponentInParent <Part>();

                            if (partHit?.vessel == SourceVessel)
                            {
                                //We found a hit to the vessel
                                return;
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                }

                //We are safe and we can continue with the cruising phase

                DetonationDistanceState = DetonationDistanceStates.Cruising;
                break;

            case DetonationDistanceStates.Cruising:

                if (Vector3.Distance(futureMissilePosition, futureTargetPosition) < GetBlastRadius() * 10)
                {
                    //We are now close enough to start checking the detonation distance
                    DetonationDistanceState = DetonationDistanceStates.CheckingProximity;
                }
                else
                {
                    BDModularGuidance bdModularGuidance = this as BDModularGuidance;

                    if (bdModularGuidance == null)
                    {
                        return;
                    }

                    if (Vector3.Distance(futureMissilePosition, futureTargetPosition) > this.DetonationDistance)
                    {
                        return;
                    }

                    DetonationDistanceState = DetonationDistanceStates.CheckingProximity;
                }
                break;

            case DetonationDistanceStates.CheckingProximity:

                if (DetonationDistance == 0)
                {
                    if (weaponClass == WeaponClasses.Bomb)
                    {
                        return;
                    }

                    if (TimeIndex > 1f)
                    {
                        //Vector3 floatingorigin_current = FloatingOrigin.Offset;

                        Ray rayFuturePosition = new Ray(vessel.CoM, futureMissilePosition);

                        var hitsFuture = Physics.RaycastAll(rayFuturePosition, (float)missileDistancePerFrame.magnitude, 557057).AsEnumerable();

                        using (var hitsEnu = hitsFuture.GetEnumerator())
                        {
                            while (hitsEnu.MoveNext())
                            {
                                RaycastHit hit = hitsEnu.Current;

                                try
                                {
                                    var hitPart = hit.collider.gameObject.GetComponentInParent <Part>();

                                    if (hitPart?.vessel != SourceVessel && hitPart?.vessel != vessel)
                                    {
                                        //We found a hit to other vessel
                                        vessel.SetPosition(hit.point);
                                        DetonationDistanceState = DetonationDistanceStates.Detonate;
                                        Detonate();
                                        return;
                                    }
                                }
                                catch
                                {
                                    // ignored
                                }
                            }
                        }
                    }

                    previousPos = part.transform.position;
                }
                else
                {
                    float optimalDistance = (float)(Math.Max(DetonationDistance, relativeSpeed));

                    using (var hitsEnu = Physics.OverlapSphere(vessel.CoM, optimalDistance, 557057).AsEnumerable().GetEnumerator())
                    {
                        while (hitsEnu.MoveNext())
                        {
                            if (hitsEnu.Current == null)
                            {
                                continue;
                            }

                            try
                            {
                                Part partHit = hitsEnu.Current.GetComponentInParent <Part>();

                                if (partHit?.vessel == vessel || partHit?.vessel == SourceVessel)
                                {
                                    continue;
                                }

                                Debug.Log("[BDArmory]: Missile proximity sphere hit | Distance overlap = " + optimalDistance + "| Part name = " + partHit.name);

                                //We found a hit a different vessel than ours
                                DetonationDistanceState = DetonationDistanceStates.Detonate;
                                return;
                            }
                            catch
                            {
                                // ignored
                            }
                        }
                    }
                }

                break;
            }

            if (BDArmorySettings.DRAW_DEBUG_LABELS)
            {
                Debug.Log("[BDArmory]: DetonationDistanceState = : " + DetonationDistanceState);
            }
        }