Exemplo n.º 1
0
 private void ProcessFreeAttachPlacement(RaycastResult raycast)
 {
     if (NewSpawnedPart == null)
     {
         Mode = AddonMode.None;
         if (Mode == AddonMode.FreeAttach)
         {
             CurrentTargeter.AbortLink();
         }
         else if (Mode == AddonMode.AttachKerbalHook)
         {
             CurrentKerbalTargeter.Abort();
         }
         Debug.Log("[IRAS][ERR] no target part ready - aborting FreeAttach");
         return;
     }
     if (partPlacementInProgress)
     {
         return;
     }
     partPlacementInProgress = true;
     StartCoroutine(PlaceNewPart(raycast));
 }
Exemplo n.º 2
0
        private void ProcessUpdate()
        {
            try
            {
                if (HighLogic.LoadedSceneIsFlight)
                {
                    if (NewSpawnedPart != null)
                    {
                        var justDestroyPart = false;
                        if (CurrentTargeter != null || CurrentKerbalTargeter != null)
                        {
                            NewSpawnedPart.transform.position = CurrentKerbalTargeter != null
                                ? CurrentKerbalTargeter.transform.position
                                : CurrentTargeter.transform.position;
                        }
                        else
                        {
                            var module = NewSpawnedPart.Modules[Config.Instance.ModuleActiveStrutFreeAttachTarget];
                            if (module != null)
                            {
                                var target = module as ModuleActiveStrutFreeAttachTarget;
                                if (target != null)
                                {
                                    target.Die();
                                }
                                else
                                {
                                    justDestroyPart = true;
                                }
                            }
                            else
                            {
                                justDestroyPart = true;
                            }
                        }
                        if (justDestroyPart)
                        {
                            Destroy(NewSpawnedPart);
                            NewSpawnedPart = null;
                        }
                    }
                    _processIdResets();
                }
                if (Config.Instance.ShowStraightOutHint && straightOutHintActiveParts != null)
                {
                    var remList   = new List <StraightOutHintActivePart>();
                    var renewList = new List <StraightOutHintActivePart>();
                    foreach (var straightOutHintActivePart in straightOutHintActiveParts)
                    {
                        if (straightOutHintActivePart.HasToBeRemoved)
                        {
                            remList.Add(straightOutHintActivePart);
                        }
                        else
                        {
                            renewList.Add(straightOutHintActivePart);
                        }
                    }
                    foreach (var straightOutHintActivePart in remList)
                    {
                        straightOutHintActiveParts.Remove(straightOutHintActivePart);
                        Destroy(straightOutHintActivePart.HintObject);
                    }
                    foreach (var straightOutHintActivePart in renewList)
                    {
                        UpdateStraightOutHint(straightOutHintActivePart.Module, straightOutHintActivePart.HintObject);
                    }
                }
                var resetList = new List <HighlightedPart>();
                if (targetHighlightedParts != null)
                {
                    resetList =
                        targetHighlightedParts.Where(
                            targetHighlightedPart =>
                            targetHighlightedPart != null && targetHighlightedPart.HasToBeRemoved).ToList();
                }
                foreach (var targetHighlightedPart in resetList)
                {
                    targetHighlightedPart.Part.SetHighlightDefault();
                    try
                    {
                        if (targetHighlightedParts != null)
                        {
                            targetHighlightedParts.Remove(targetHighlightedPart);
                        }
                    }
                    catch (NullReferenceException)
                    {
                        //multithreading issue occured here, don't know if fixed
                    }
                }
                if (targetHighlightedParts != null)
                {
                    foreach (var targetHighlightedPart in targetHighlightedParts)
                    {
                        var part = targetHighlightedPart.Part;
                        part.SetHighlightColor(Color.cyan);
                        part.SetHighlight(true, false);
                    }
                }
                if (Mode == AddonMode.None || (CurrentTargeter == null && CurrentKerbalTargeter == null))
                {
                    if (resetAllHighlighting)
                    {
                        resetAllHighlighting = false;
                        var asList = Utilities.GetAllActiveStruts();
                        foreach (var moduleActiveStrut in asList)
                        {
                            moduleActiveStrut.part.SetHighlightDefault();
                        }
                    }
                    if (connector != null)
                    {
                        connector.SetActive(false);
                    }
                    return;
                }
                resetAllHighlighting = true;
                if (Mode == AddonMode.Link)
                {
                    HighlightCurrentTargets();
                }
                var mp = Utilities.GetMouseWorldPosition();
                var transformForRaycast = Mode == AddonMode.AttachKerbalHook
                    ? CurrentKerbalTargeter.transform
                    : CurrentTargeter.ModelFeatures[ModuleActiveStrut.ModelFeaturesType.HeadExtension]
                        ? CurrentTargeter.StrutOrigin
                        : CurrentTargeter.Origin;
                var rightDir = Mode == AddonMode.AttachKerbalHook ? Vector3.zero : CurrentTargeter.RealModelForward;
                var raycast  = Mode == AddonMode.AttachKerbalHook
                    ? Utilities.PerformRaycastFromKerbal(transformForRaycast.position, mp.Item1, transformForRaycast.up,
                                                         CurrentKerbalTargeter.part.vessel)
                    : Utilities.PerformRaycast(transformForRaycast.position, mp.Item1,
                                               CurrentTargeter.IsFlexible ? transformForRaycast.up : rightDir,
                                               CurrentTargeter.IsFlexible ? CurrentTargeter.part : null);
                PointToMousePosition(mp.Item1, raycast);
                if (!raycast.HitResult)
                {
                    var handled = false;
                    if (Mode == AddonMode.Link && Input.GetKeyDown(KeyCode.Mouse0))
                    {
                        CurrentTargeter.AbortLink();
                        CurrentTargeter.UpdateGui();
                        handled = true;
                    }
                    if ((Mode == AddonMode.FreeAttach || Mode == AddonMode.AttachKerbalHook) &&
                        (Input.GetKeyDown(KeyCode.X) || Input.GetKeyDown(KeyCode.Escape)))
                    {
                        if (HighLogic.LoadedSceneIsFlight && Input.GetKeyDown(KeyCode.Escape))
                        {
                            Input.ResetInputAxes();
                        }
                        Mode = AddonMode.None;
                        if (Mode == AddonMode.AttachKerbalHook)
                        {
                            CurrentKerbalTargeter.Abort();
                        }
                        else
                        {
                            CurrentTargeter.UpdateGui();
                            CurrentTargeter.AbortLink();
                            CurrentTargeter = null;
                        }
                        CheckForInEditorAbort();
                        handled = true;
                    }
                    connector.SetActive(false);
                    if (HighLogic.LoadedSceneIsEditor && handled)
                    {
                        Input.ResetInputAxes();
                        InputLockManager.RemoveControlLock(Config.Instance.EditorInputLockId);
                    }
                    return;
                }
                var validPos = _determineColor(mp.Item1, raycast);
                if (validPos && Mode == AddonMode.FreeAttach && HighLogic.LoadedSceneIsEditor)
                {
                    InputLockManager.RemoveControlLock(Config.Instance.EditorInputLockId);
                    noInputAxesReset = true;
                }
                else if (HighLogic.LoadedSceneIsEditor)
                {
                    InputLockManager.SetControlLock(Config.Instance.EditorInputLockId);
                    noInputAxesReset = false;
                }
                ProcessUserInput(mp.Item1, raycast, validPos);
            }
            catch (NullReferenceException e)
            {
                Debug.Log("[IRAS] addon update exception catched: " + e);

                /*
                 * For no apparent reason an exception is thrown on first load.
                 * I found no way to circumvent this.
                 * Since the exception has to be handled only once we are
                 * "just" entering the try block constantly which I consider
                 * still to be preferred over an unhandled exception.
                 */
            }
        }
Exemplo n.º 3
0
        private static IEnumerator PlaceNewPart(RaycastResult raycast)
        {
            var activeVessel = FlightGlobals.ActiveVessel;

            NewSpawnedPart.transform.position        = raycast.Hit.point;
            NewSpawnedPart.rigidbody.velocity        = raycast.HittedPart.rigidbody.velocity;
            NewSpawnedPart.rigidbody.angularVelocity = raycast.HittedPart.rigidbody.angularVelocity;
            yield return(new WaitForSeconds(0.1f));

            NewSpawnedPart.transform.rotation = raycast.HittedPart.transform.rotation;
            NewSpawnedPart.transform.position = raycast.Hit.point;

            NewSpawnedPart.transform.LookAt(Mode == AddonMode.AttachKerbalHook
                ? CurrentKerbalTargeter.transform.position
                : CurrentTargeter.transform.position);
            NewSpawnedPart.transform.rotation =
                Quaternion.FromToRotation(NewSpawnedPart.transform.up, raycast.Hit.normal) *
                NewSpawnedPart.transform.rotation;

            yield return(new WaitForFixedUpdate());

            var targetModuleName = Config.Instance.ModuleActiveStrutFreeAttachTarget;

            if (!NewSpawnedPart.Modules.Contains(targetModuleName))
            {
                Debug.Log("[IRAS][ERR] spawned part contains no target module. Panic!!");
                NewSpawnedPart.decouple();
                Destroy(NewSpawnedPart);
            }
            var module = NewSpawnedPart.Modules[Config.Instance.ModuleActiveStrutFreeAttachTarget];

            if (raycast.HittedPart.vessel != activeVessel)
            {
                if (module != null)
                {
                    var targetModule = module as ModuleActiveStrutFreeAttachTarget;
                    if (targetModule != null)
                    {
                        var dockingVesselName = activeVessel.GetName();
                        var dockingVesselType = activeVessel.vesselType;
                        var dockingVesselId   = activeVessel.rootPart.flightID;
                        var vesselInfo        = new DockedVesselInfo
                        {
                            name        = dockingVesselName,
                            vesselType  = dockingVesselType,
                            rootPartUId = dockingVesselId
                        };
                        targetModule.part.Couple(activeVessel.rootPart);
                        targetModule.part.Undock(vesselInfo);
                    }
                }
            }
            NewSpawnedPart.transform.position = raycast.Hit.point;
            if (module != null)
            {
                var targetModule = module as ModuleActiveStrutFreeAttachTarget;
                if (targetModule != null)
                {
                    targetModule.CreateJointToParent(raycast.HittedPart);
                }
            }
            yield return(new WaitForFixedUpdate());

            yield return(new WaitForSeconds(0.1f));

            yield return(new WaitForFixedUpdate());

            if (Mode == AddonMode.AttachKerbalHook)
            {
                CurrentKerbalTargeter.PlaceHook(NewSpawnedPart);
            }
            else
            {
                CurrentTargeter.PlaceFreeAttach(NewSpawnedPart);
            }
            partPlacementInProgress = false;
            NewSpawnedPart          = null;
        }