예제 #1
0
 public static void AddPoint(string n, PosAndRot p)
 {
     if (!points.ContainsKey(n))
     {
         points.Add(n, p);
     }
 }
예제 #2
0
    public static PosAndRot operator +(PosAndRot a, PosAndRot b)
    {
        PosAndRot newTrans = new PosAndRot();

        newTrans.position = a.position + b.position;
        newTrans.rotation = a.rotation * b.rotation;
        return(newTrans);
    }
예제 #3
0
 /// <summary>
 /// Creates the new line object.
 /// </summary>
 /// <returns>
 /// The new object's LineRenderer
 /// </returns>
 /// <param name='posAndRot'>
 /// The PosAndRot to centre the object on
 /// </param>
 /// <param name='parent'>
 /// The object to be parented to
 /// </param>
 /// <param name='iteration'>
 /// The current iteration
 /// </param>
 LineRenderer CreateNewLineObject(PosAndRot posAndRot, GameObject parent, int iter)
 {
     GameObject temp = new GameObject("Shape " + (++numOfShapes).ToString());
     temp.transform.position = new Vector3(posAndRot.pos.x, 0, posAndRot.pos.y);
     temp.transform.parent = parent.transform;
     //if(shape.edges%2 != 0){
         temp.transform.localRotation = new Quaternion(0, (iter-1)%2 * 180f/shape.edges, 0, 0);
     //}
     return temp.AddComponent<LineRenderer>();
 }
        public static void ToLocal()
        {
            var parent = new GameObject();

            parent.transform.position = Vector3.one;
            parent.transform.rotation = Quaternion.LookRotation(Vector3.up);
            var pr = new PosAndRot(Vector3.one, new Vector3(90, 0, 0));

            Debug.LogFormat("Local: {0}", parent.transform.InverseTransformPosAndRot(pr));
            Debug.LogFormat("World: {0}", pr);
        }
예제 #5
0
 /// <inheritdoc/>
 public override void OnBeforeDebugAdjustablesUpdate()
 {
     _dbgOldConnectorState = connectorState;
     if (connectorState == ConnectorState.Deployed)
     {
         _dbgOldCableLength = currentCableLength;
         SaveConnectorModelPosAndRot();
         _dbgOldConnectorPosAndRot = persistedConnectorPosAndRot;
         SetConnectorState(ConnectorState.Locked);
     }
     base.OnBeforeDebugAdjustablesUpdate();
 }
예제 #6
0
 void AddNeighbours(PosAndRot posAndRot)
 {
     Vector2 pos = posAndRot.pos;
     float b = ((shape.angle + posAndRot.rot)/2*Mathf.Deg2Rad);
     int temp = 0;
     for(int a = 0; a < shape.edges; a++, b += (shape.angle * Mathf.Deg2Rad)){
         tempShapes.Add(new PosAndRot(new Vector2(pos.x + ((shape.radius * Mathf.Cos(Mathf.PI/shape.edges)) * 2 * Mathf.Cos(b)),
             pos.y + ((shape.radius * Mathf.Cos(Mathf.PI/shape.edges)) * 2 * Mathf.Sin(b))), posAndRot.rot));
         temp++;
     }
     //Debug.Log("Finished Adding " + temp + " Neighbours");
 }
예제 #7
0
 /// <inheritdoc/>
 public override void OnSave(ConfigNode node)
 {
     // Persist the connector data only if its position is not fixed to the winch model.
     // It must be the peristsent state since the state machine can be in a different state at this
     // moment (e.g. during the vessel backup).
     if (!persistedIsConnectorLocked && !isLinked)
     {
         persistedConnectorPosAndRot = gameObject.transform.InverseTransformPosAndRot(
             new PosAndRot(connectorModelObj.position, connectorModelObj.rotation.eulerAngles));
     }
     else
     {
         // In linked or locked state the connector is fixed to either source or target part.
         persistedConnectorPosAndRot = null;
     }
     base.OnSave(node);
 }
예제 #8
0
 /// <inheritdoc/>
 public override void OnDebugAdjustablesUpdated()
 {
     base.OnDebugAdjustablesUpdated();
     AsyncCall.CallOnEndOfFrame(
         this,
         () => {
         if (_dbgOldConnectorState == ConnectorState.Deployed)
         {
             HostedDebugLog.Warning(
                 this, "Restoring connector: state={0}, at={1}, length={2}",
                 _dbgOldConnectorState, _dbgOldConnectorPosAndRot, _dbgOldCableLength);
             persistedConnectorPosAndRot = _dbgOldConnectorPosAndRot;
             SetConnectorState(_dbgOldConnectorState);
             SetCableLength(_dbgOldCableLength);
         }
     },
         skipFrames: 1); // To match the base class delay.
 }
예제 #9
0
        /// <summary>Saves the connector relative position and rotation.</summary>
        /// <remarks>If there is no physical connector started, then erases any saved state.</remarks>
        /// <param name="saveNonPhysical">
        /// Tells to update state to the connector model position if the physical connector is not
        /// started. However, the state will be saved only if there was no previous state.
        /// </param>
        void SaveConnectorModelPosAndRot(bool saveNonPhysical = false)
        {
            if (!saveNonPhysical && connectorObj == null)
            {
                persistedConnectorPosAndRot = null;
                return;
            }
            if (saveNonPhysical && connectorObj == null && persistedConnectorPosAndRot != null)
            {
                // For non physical connector only update connector if not yet updated. To allow restoring
                // a deployed connector at an arbitrary location.
                return;
            }
            var connector = connectorObj ?? GetConnectorModel();

            persistedConnectorPosAndRot = gameObject.transform.InverseTransformPosAndRot(
                new PosAndRot(connector.position, connector.rotation.eulerAngles));
        }
예제 #10
0
    private static void StoreNewPosition()
    {
        PosAndRot  temp = new PosAndRot();
        Quaternion rot  = SceneView.lastActiveSceneView.camera.transform.rotation;
        Vector3    pos  = SceneView.lastActiveSceneView.camera.transform.position;

        Transform[] cam = Selection.transforms;
        if (cam.Length != 0)
        {
            var snapCam = cam[0].GetComponent <MN_SnapCamera>();
            Undo.RecordObject(snapCam, "Added a camera spot");

            temp.pos = pos;
            temp.rot = rot.eulerAngles;

            snapCam.mPosAndRots.Add(temp);

            PrefabUtility.RecordPrefabInstancePropertyModifications(snapCam);
        }
    }
예제 #11
0
    private PosAndRot GetTransform(ref CathodeNodeEntity node)
    {
        PosAndRot toReturn = new PosAndRot();

        foreach (CathodeParameterReference paramRef in node.nodeParameterReferences)
        {
            CathodeParameter param = commandsPAK.GetParameter(paramRef.offset);
            if (param == null)
            {
                continue;
            }
            if (param.dataType != CathodeDataType.POSITION)
            {
                continue;
            }
            CathodeTransform transform = (CathodeTransform)param;
            toReturn.position = transform.position;
            toReturn.rotation = Quaternion.Euler(transform.rotation);
            break;
        }
        return(toReturn);
    }
예제 #12
0
    private IEnumerator RecursiveLoad(CathodeFlowgraph flowgraph, GameObject parentTransform, System.Action <int, GameObject> loadModelCallback)
    {
        for (int i = 0; i < flowgraph.nodes.Count; i++)
        {
            CathodeNodeEntity node     = flowgraph.nodes[i];
            CathodeFlowgraph  nextCall = commandsPAK.GetFlowgraph(node.nodeType);
            if (nextCall == null)
            {
                continue;
            }
            PosAndRot  trans           = GetTransform(ref node);
            GameObject nextFlowgraphGO = new GameObject(nextCall.name);
            nextFlowgraphGO.transform.parent        = parentTransform.transform;
            nextFlowgraphGO.transform.localPosition = trans.position;
            nextFlowgraphGO.transform.localRotation = trans.rotation;
            StartCoroutine(RecursiveLoad(nextCall, nextFlowgraphGO, loadModelCallback));
        }

        StartCoroutine(LoadModelReferenceNodes(flowgraph, parentTransform, loadModelCallback));
        StartCoroutine(LoadPlayerTriggerBoxNodes(flowgraph, parentTransform, loadModelCallback));

        yield break;
    }
예제 #13
0
 /// <summary>Sets position and rotation in one call./// </summary>
 /// <param name="node">The node to update.</param>
 /// <param name="posAndRot">The position and rotation in wolrd space.</param>
 public static void SetPosAndRot(this Transform node, PosAndRot posAndRot)
 {
     node.SetPositionAndRotation(posAndRot.pos, posAndRot.rot);
 }
예제 #14
0
 /// <summary>
 /// Transforms a pos&amp;rot object from the local space to the world space. The opposite to
 /// <see cref="InverseTransformPosAndRot"/>.
 /// </summary>
 /// <param name="node">The node to use as a parent.</param>
 /// <param name="posAndRot">The object in local space.</param>
 /// <returns>A new pos&amp;rot object in the wold space.</returns>
 /// <example>
 /// <code source="Examples/Extensions/PosAndRotExtensions-Examples.cs" region="ToWorld"/>
 /// </example>
 public static PosAndRot TransformPosAndRot(this Transform node, PosAndRot posAndRot)
 {
     return(posAndRot.Transform(node));
 }
예제 #15
0
        /// <summary>Intializes the connector model object and its anchors.</summary>
        /// <remarks>
        /// <para>
        /// If the connector model is not found then a stub object will be created. There will be no visual
        /// representation but the overall functionality of the winch should keep working.
        /// </para>
        /// <para>
        /// If the connector doesn't have the anchors then the missed ones will be created basing on the
        /// provided position/rotation. If the config file doesn't provide anything then the anchors will
        /// have a zero position and a random rotation.
        /// </para>
        /// </remarks>
        void LoadOrCreateConnectorModel()
        {
            var          ConnectorModelName      = "ConnectorModel" + part.Modules.IndexOf(this);
            var          ConnectorParkAnchorName = "ConnectorParkAnchor" + part.Modules.IndexOf(this);
            const string CableAnchorName         = "CableAnchor";
            const string PartAnchorName          = "PartAnchor";

            if (!PartLoader.Instance.IsReady())
            {
                // Make the missing models and set the proper hierarchy.
                connectorModelObj    = Hierarchy.FindPartModelByPath(part, connectorModel);
                connectorCableAnchor = connectorCableAttachAt != ""
          ? Hierarchy.FindPartModelByPath(part, connectorCableAttachAt) : null;
                connectorPartAnchor = connectorPartAttachAt != ""
          ? Hierarchy.FindPartModelByPath(part, connectorPartAttachAt) : null;

                if (connectorModelObj == null)
                {
                    HostedDebugLog.Error(this, "Cannot find a connector model: {0}", connectorModel);
                    // Fallback to not have the whole code to crash.
                    connectorModelObj = new GameObject().transform;
                }
                connectorModelObj.name   = ConnectorModelName;
                connectorModelObj.parent = nodeTransform;


                if (connectorCableAnchor == null)
                {
                    if (connectorCableAttachAt != "")
                    {
                        HostedDebugLog.Error(
                            this, "Cannot find cable anchor transform: {0}", connectorCableAttachAt);
                    }
                    connectorCableAnchor = new GameObject().transform;
                    var posAndRot = PosAndRot.FromString(connectorCableAttachAtPosAndRot);
                    Hierarchy.MoveToParent(connectorCableAnchor, connectorModelObj,
                                           newPosition: posAndRot.pos, newRotation: posAndRot.rot);
                }
                connectorCableAnchor.name   = CableAnchorName;
                connectorCableAnchor.parent = connectorModelObj;

                if (connectorPartAnchor == null)
                {
                    if (connectorPartAttachAt != "")
                    {
                        HostedDebugLog.Error(
                            this, "Cannot find part anchor transform: {0}", connectorPartAttachAt);
                    }
                    connectorPartAnchor = new GameObject().transform;
                    var posAndRot = PosAndRot.FromString(connectorPartAttachAtPosAndRot);
                    Hierarchy.MoveToParent(connectorPartAnchor, connectorModelObj,
                                           newPosition: posAndRot.pos, newRotation: posAndRot.rot);
                }
                connectorPartAnchor.name   = PartAnchorName;
                connectorPartAnchor.parent = connectorModelObj;

                partCableAnchor = new GameObject(ConnectorParkAnchorName).transform;
                Hierarchy.MoveToParent(
                    partCableAnchor, nodeTransform, newPosition: connectorParkPositionOffset);
            }
            else
            {
                connectorModelObj    = nodeTransform.Find(ConnectorModelName);
                connectorCableAnchor = connectorModelObj.Find(CableAnchorName);
                connectorPartAnchor  = connectorModelObj.Find(PartAnchorName);
                partCableAnchor      = nodeTransform.Find(ConnectorParkAnchorName);
            }
            AlignTransforms.SnapAlign(connectorModelObj, connectorCableAnchor, partCableAnchor);
        }