Exemplo n.º 1
0
        public void UpdatePointer()
        {
            // Stop pointer on map
            if (running && MapView.MapIsEnabled)
            {
                StopPointer();
                return;
            }

            // Remove pointer if not running.
            if (!running)
            {
                DestroyPointer();
                return;
            }

            // Hide pointer if the raycast do not hit anything.
            if (pointerTarget == PointerTarget.Nothing)
            {
                SetPointerVisible(false);
                return;
            }

            SetPointerVisible(true);

            // Custom rotation
            float rotDegree = 15;

            if (Input.GetKey(KeyCode.LeftShift))
            {
                rotDegree = 1;
            }
            if (GameSettings.Editor_rollLeft.GetKeyDown())
            {
                customRot -= new Vector3(0, -1, 0) * rotDegree;
            }
            if (GameSettings.Editor_rollRight.GetKeyDown())
            {
                customRot += new Vector3(0, -1, 0) * rotDegree;
            }
            if (GameSettings.Editor_pitchDown.GetKeyDown())
            {
                customRot -= new Vector3(1, 0, 0) * rotDegree;
            }
            if (GameSettings.Editor_pitchUp.GetKeyDown())
            {
                customRot += new Vector3(1, 0, 0) * rotDegree;
            }
            if (GameSettings.Editor_yawLeft.GetKeyDown())
            {
                customRot -= new Vector3(0, 0, 1) * rotDegree;
            }
            if (GameSettings.Editor_yawRight.GetKeyDown())
            {
                customRot += new Vector3(0, 0, 1) * rotDegree;
            }
            if (GameSettings.Editor_resetRotation.GetKeyDown())
            {
                customRot = new Vector3(0, 0, 0);
            }
            Quaternion rotAdjust =
                Quaternion.Euler(0, 0, customRot.z) * Quaternion.Euler(customRot.x, customRot.y, 0);

            // Move to position
            if (pointerTarget == PointerTarget.PartMount)
            {
                //Mount snap
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hoveredNode.nodeTransform);
            }
            else if (pointerTarget == PointerTarget.PartNode)
            {
                //Part node snap
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform,
                                     hoveredNode.nodeTransform, rotAdjust);
            }
            else
            {
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit, rotAdjust);
            }

            // Move above
            if (allowOffset)
            {
                if (pointerTarget != PointerTarget.PartMount)
                {
                    if (KIS_Shared.IsKeyDown(offsetUpKey) && aboveDistance < maxOffsetDist)
                    {
                        aboveDistance += aboveOffsetStep;
                    }
                    if (KIS_Shared.IsKeyDown(offsetDownKey) && aboveDistance > -maxOffsetDist)
                    {
                        aboveDistance -= aboveOffsetStep;
                    }
                    if (GameSettings.Editor_resetRotation.GetKeyDown())
                    {
                        aboveDistance = 0;
                    }
                    pointer.transform.position =
                        pointer.transform.position + (hit.normal.normalized * aboveDistance);
                }
            }

            //Check distance
            float sourceDist = 0;

            if (sourceTransform)
            {
                sourceDist =
                    Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, sourceTransform.position);
            }
            float targetDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, hit.point);

            //Set color
            Color color               = colorOk;
            bool  invalidTarget       = false;
            bool  notAllowedOnMount   = false;
            bool  cannotSurfaceAttach = false;
            bool  invalidCurrentNode  = false;
            bool  itselfIsInvalid     = !allowPartItself &&
                                        partToAttach != null && partToAttach.hasIndirectChild(hoveredPart);
            bool restrictedPart =
                allowedAttachmentParts != null && !allowedAttachmentParts.Contains(hoveredPart);

            switch (pointerTarget)
            {
            case PointerTarget.Static:
            case PointerTarget.StaticRb:
                invalidTarget = !allowStatic;
                break;

            case PointerTarget.KerbalEva:
                invalidTarget = !allowEva;
                break;

            case PointerTarget.Part:
                if (allowPart)
                {
                    if (useAttachRules)
                    {
                        if (hoveredPart.attachRules.allowSrfAttach)
                        {
                            invalidCurrentNode = currentAttachNode.nodeType != AttachNode.NodeType.Surface;
                        }
                        else
                        {
                            cannotSurfaceAttach = true;
                        }
                    }
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            case PointerTarget.PartMount:
                if (allowMount)
                {
                    ModuleKISPartMount pMount = hoveredPart.GetComponent <ModuleKISPartMount>();
                    var allowedPartNames      = new List <string>();
                    pMount.GetMounts().TryGetValue(hoveredNode, out allowedPartNames);
                    notAllowedOnMount =
                        partToAttach != null && !allowedPartNames.Contains(partToAttach.partInfo.name);
                    color = colorMountOk;
                }
                break;

            case PointerTarget.PartNode:
                invalidTarget = !allowStack;
                color         = colorStack;
                break;
            }

            // Handle generic "not OK" color.
            if (sourceDist > maxDist || targetDist > maxDist)
            {
                color = colorDistNok;
            }
            else if (invalidTarget || cannotSurfaceAttach || invalidCurrentNode ||
                     itselfIsInvalid || restrictedPart)
            {
                color = colorNok;
            }

            color.a = 0.5f;
            foreach (var mr in allModelRenderers)
            {
                mr.material.color = color;
            }

            //On click.
            if (Input.GetMouseButtonDown(0))
            {
                if (invalidTarget)
                {
                    ScreenMessaging.ShowInfoScreenMessage(TargetObjectNotAllowedMsg);
                    UISounds.PlayBipWrong();
                }
                else if (itselfIsInvalid)
                {
                    ScreenMessaging.ShowInfoScreenMessage(CannotAttachOnItselfMsg);
                    UISounds.PlayBipWrong();
                }
                else if (notAllowedOnMount)
                {
                    ScreenMessaging.ShowInfoScreenMessage(NotAllowedOnTheMountMsg);
                    UISounds.PlayBipWrong();
                }
                else if (cannotSurfaceAttach)
                {
                    ScreenMessaging.ShowInfoScreenMessage(TargetDoesntAllowSurfaceAttachMsg);
                    UISounds.PlayBipWrong();
                }
                else if (invalidCurrentNode)
                {
                    ScreenMessaging.ShowInfoScreenMessage(NodeNotForSurfaceAttachMsg);
                    UISounds.PlayBipWrong();
                }
                else if (sourceDist > maxDist)
                {
                    ScreenMessaging.ShowInfoScreenMessage(TooFarFromSourceMsg.Format(sourceDist, maxDist));
                    UISounds.PlayBipWrong();
                }
                else if (targetDist > maxDist)
                {
                    ScreenMessaging.ShowInfoScreenMessage(TooFarFromTargetMsg.Format(targetDist, maxDist));
                    UISounds.PlayBipWrong();
                }
                else if (restrictedPart)
                {
                    ScreenMessaging.ShowInfoScreenMessage(
                        CannotAttachToPartMsg.Format(hoveredPart.partInfo.title));
                    UISounds.PlayBipWrong();
                }
                else
                {
                    SendPointerClick(pointerTarget, pointer.transform.position, pointer.transform.rotation,
                                     hoveredPart, currentAttachNode.id, hoveredNode);
                }
            }
        }
Exemplo n.º 2
0
        public void UpdatePointer()
        {
            // Stop pointer on map
            if (running && MapView.MapIsEnabled)
            {
                StopPointer();
            }

            // Remove pointer if not running or if the raycast do not hit anything
            if (!running || pointerTarget == PointerTarget.Nothing)
            {
                if (pointer)
                {
                    UnityEngine.Object.Destroy(pointer);
                }
                return;
            }

            //Create pointer if needed
            if (!pointer)
            {
                GameObject modelGo      = partToAttach.FindModelTransform("model").gameObject;
                GameObject pointerModel = Mesh.Instantiate(modelGo, new Vector3(0, 0, 100), Quaternion.identity) as GameObject;
                foreach (Collider col in pointerModel.GetComponentsInChildren <Collider>())
                {
                    UnityEngine.Object.DestroyImmediate(col);
                }

                pointer = new GameObject("KISPointer");
                pointerModel.transform.parent        = pointer.transform;
                pointerModel.transform.localPosition = modelGo.transform.localPosition;
                pointerModel.transform.localRotation = modelGo.transform.localRotation;
                pointer.transform.localScale         = new Vector3(scale, scale, scale);

                allModelMr = new List <MeshRenderer>();
                // Remove attached tube mesh renderer if any
                List <MeshRenderer> tmpAllModelMr = new List <MeshRenderer>(pointerModel.GetComponentsInChildren <MeshRenderer>() as MeshRenderer[]);
                foreach (MeshRenderer mr in tmpAllModelMr)
                {
                    if (mr.name == "KAStube" || mr.name == "KASsrcSphere" || mr.name == "KASsrcTube" || mr.name == "KAStgtSphere" || mr.name == "KAStgtTube")
                    {
                        Destroy(mr);
                        continue;
                    }
                    allModelMr.Add(mr);
                    mr.material = new Material(Shader.Find("Transparent/Diffuse"));
                }
                // Set pointer attach node
                pointerNodeTransform               = new GameObject("KASPointerPartNode").transform;
                pointerNodeTransform.parent        = pointer.transform;
                pointerNodeTransform.localPosition = GetCurrentAttachNode().position;
                pointerNodeTransform.localRotation = KIS_Shared.GetNodeRotation(GetCurrentAttachNode());
            }

            // Custom rotation
            float rotDegree = 15;

            if (Input.GetKey(KeyCode.LeftShift))
            {
                rotDegree = 1;
            }
            if (GameSettings.Editor_rollLeft.GetKeyDown())
            {
                customRot -= new Vector3(0, -1, 0) * rotDegree;
            }
            if (GameSettings.Editor_rollRight.GetKeyDown())
            {
                customRot += new Vector3(0, -1, 0) * rotDegree;
            }
            if (GameSettings.Editor_pitchDown.GetKeyDown())
            {
                customRot -= new Vector3(1, 0, 0) * rotDegree;
            }
            if (GameSettings.Editor_pitchUp.GetKeyDown())
            {
                customRot += new Vector3(1, 0, 0) * rotDegree;
            }
            if (GameSettings.Editor_yawLeft.GetKeyDown())
            {
                customRot -= new Vector3(0, 0, 1) * rotDegree;
            }
            if (GameSettings.Editor_yawRight.GetKeyDown())
            {
                customRot += new Vector3(0, 0, 1) * rotDegree;
            }
            if (GameSettings.Editor_resetRotation.GetKeyDown())
            {
                customRot = new Vector3(0, 0, 0);
            }
            Quaternion rotAdjust = Quaternion.Euler(0, 0, customRot.z) * Quaternion.Euler(customRot.x, customRot.y, 0);

            // Move to position
            if (pointerTarget == PointerTarget.PartMount)
            {
                //Mount snap
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hoveredNode.nodeTransform);
                pointer.transform.rotation *= Quaternion.Euler(hoveredNode.orientation);
            }
            else if (pointerTarget == PointerTarget.PartNode)
            {
                //Part node snap
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hoveredNode.nodeTransform, rotAdjust);
            }
            else
            {
                KIS_Shared.MoveAlign(pointer.transform, pointerNodeTransform, hit, rotAdjust);
            }

            // Move above
            if (allowOffset)
            {
                if (pointerTarget != PointerTarget.PartMount)
                {
                    if (Input.GetKeyDown(offsetUpKey))
                    {
                        if (aboveDistance < maxOffsetDist)
                        {
                            aboveDistance += aboveOffsetStep;
                        }
                    }
                    if (Input.GetKeyDown(offsetDownKey))
                    {
                        if (aboveDistance > -maxOffsetDist)
                        {
                            aboveDistance -= aboveOffsetStep;
                        }
                    }
                    if (GameSettings.Editor_resetRotation.GetKeyDown())
                    {
                        aboveDistance = 0;
                    }
                    pointer.transform.position = pointer.transform.position + (hit.normal.normalized * aboveDistance);
                }
            }

            //Check distance
            bool isValidSourceDist = true;

            if (sourceTransform)
            {
                isValidSourceDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, sourceTransform.position) <= maxDist;
            }
            bool isValidTargetDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, hit.point) <= maxDist;

            //Set color
            Color color               = colorNok;
            bool  invalidTarget       = false;
            bool  notAllowedOnMount   = false;
            bool  cannotSurfaceAttach = false;
            bool  invalidCurrentNode  = false;
            bool  itselfIsInvalid     = false;

            switch (pointerTarget)
            {
            case PointerTarget.Static:
                if (allowStatic)
                {
                    color = colorOk;
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            case PointerTarget.StaticRb:
                if (allowStatic)
                {
                    color = colorOk;
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            case PointerTarget.KerbalEva:
                if (allowEva)
                {
                    color = colorOk;
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            case PointerTarget.Part:
                if (allowPart)
                {
                    if (hoveredPart == partToAttach && !allowPartItself)
                    {
                        itselfIsInvalid = true;
                    }
                    else
                    {
                        if (useAttachRules)
                        {
                            if (hoveredPart.attachRules.allowSrfAttach)
                            {
                                if (GetCurrentAttachNode().nodeType == AttachNode.NodeType.Surface)
                                {
                                    color = colorOk;
                                }
                                else
                                {
                                    invalidCurrentNode = true;
                                }
                            }
                            else
                            {
                                cannotSurfaceAttach = true;
                            }
                        }
                        else
                        {
                            color = colorOk;
                        }
                    }
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            case PointerTarget.PartMount:
                if (allowMount)
                {
                    ModuleKISPartMount pMount           = hoveredPart.GetComponent <ModuleKISPartMount>();
                    List <string>      allowedPartNames = new List <string>();
                    pMount.GetMounts().TryGetValue(hoveredNode, out allowedPartNames);
                    if (allowedPartNames.Contains(partToAttach.partInfo.name))
                    {
                        color = colorMountOk;
                    }
                    else
                    {
                        color             = colorMountNok;
                        notAllowedOnMount = true;
                    }
                }
                break;

            case PointerTarget.PartNode:
                if (allowStack)
                {
                    color = colorStack;
                }
                else
                {
                    invalidTarget = true;
                }
                break;

            default:
                break;
            }
            if (!isValidSourceDist || !isValidTargetDist)
            {
                color = colorDistNok;
            }
            color.a = 0.5f;
            foreach (MeshRenderer mr in allModelMr)
            {
                mr.material.color = color;
            }


            //On click
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                if (invalidTarget)
                {
                    ScreenMessages.PostScreenMessage("Target object is not allowed !");
                    audioBipWrong.Play();
                    return;
                }
                else if (itselfIsInvalid)
                {
                    ScreenMessages.PostScreenMessage("Cannot attach on itself !");
                    audioBipWrong.Play();
                    return;
                }
                else if (notAllowedOnMount)
                {
                    ScreenMessages.PostScreenMessage("This part is not allowed on the mount !");
                    audioBipWrong.Play();
                    return;
                }
                else if (cannotSurfaceAttach)
                {
                    ScreenMessages.PostScreenMessage("Target part do not allow surface attach !");
                    audioBipWrong.Play();
                    return;
                }
                else if (invalidCurrentNode)
                {
                    ScreenMessages.PostScreenMessage("This node cannot be used for surface attach !");
                    audioBipWrong.Play();
                    return;
                }
                else if (!isValidSourceDist)
                {
                    ScreenMessages.PostScreenMessage("Too far from source !");
                    audioBipWrong.Play();
                    return;
                }
                else if (!isValidTargetDist)
                {
                    ScreenMessages.PostScreenMessage("Too far from target !");
                    audioBipWrong.Play();
                    return;
                }
                else
                {
                    SendPointerClick(pointerTarget, pointer.transform.position, pointer.transform.rotation, hoveredPart, GetCurrentAttachNode().id, hoveredNode);
                }
            }
        }