Esempio n. 1
0
        private void AddChildPartAttachment(Part child)
        {
            AttachNode node = child.findAttachNodeByPart(part);

            if (node == null)
            {
                Debug.LogError("*ST* unable to find child node for child: " + child.transform);
                return;
            }

            Vector3        worldOffset = child.transform.TransformDirection(node.position);
            PartAttachment attach      = AddPartAttachment(child.transform.position + worldOffset, new TransformFollower.TransformTransformable(child.transform, node.position));

            attach.child = child;

            childAttachments.AddLast(attach);
            //Debug.LogWarning("*ST* Attaching child childAttachment: " + child.transform.name + " from child node " + node.id + " Offset=" + part.transform.InverseTransformDirection(child.transform.position + worldOffset));
        }
Esempio n. 2
0
        public void PartParentChanged(Part newParent)
        {
            if (parentAttachment != null)
            {
                RemovePartAttachment(parentAttachment);
                //Debug.LogWarning("Detatching: " + part + " from parent: " + newParent);
                parentAttachment = null;
            }

            if (newParent == null)
            {
                return;
            }

            AttachNode childToParent = part.findAttachNodeByPart(newParent);

            if (childToParent == null)
            {
                Debug.LogError("*ST* unable to find parent node from child: " + part.transform);
                return;
            }
            Vector3 position = transform.TransformPoint(childToParent.position);

            // ReSharper disable once InconsistentNaming
            Func <Vector3> Offset;

            if (nodeOffsets.TryGetValue(childToParent.id, out Offset))
            {
                position -= Offset();
            }

            Part root = EditorLogic.SortedShipList[0];

            //Debug.LogWarning("Attaching: " + part + " to new parent: " + newParent + " node:" + childToParent.id + " position=" + childToParent.position.ToString("G3"));

            // we need to delta this childAttachment down so that when the translation from the parent reaches here i ends in the right spot
            parentAttachment       = AddPartAttachment(position, new ParentTransformable(root, part, childToParent));
            parentAttachment.child = newParent;

            // for symetric attachments, seems required. Don't know why.
            shape.ForceNextUpdate();
        }
Esempio n. 3
0
        public void PartChildAttached(Part child)
        {
            AttachNode node = child.findAttachNodeByPart(part);

            if (node == null)
            {
                Debug.LogError("*ST* unable to find child node for child: " + child.transform);
                return;
            }
            Vector3 position = child.transform.TransformPoint(node.position);

            // Handle node offsets
            if (child.attachMode != AttachModes.SRF_ATTACH)
            {
                AttachNode ourNode = part.findAttachNodeByPart(child);
                if (ourNode == null)
                {
                    Debug.LogError("*ST* unable to find our node for child: " + child.transform);
                    return;
                }
                // ReSharper disable once InconsistentNaming
                Func <Vector3> Offset;
                if (nodeOffsets.TryGetValue(ourNode.id, out Offset))
                {
                    position -= Offset();
                }
            }

            //Debug.LogWarning("Attaching to parent: " + part + " child: " + child.transform.name);

            PartAttachment attach = AddPartAttachment(position, new TransformFollower.TransformTransformable(child.transform, node.position));

            attach.child = child;

            childAttachments.AddLast(attach);

            shape.ForceNextUpdate();
        }
        //[PartMessageListener(typeof(PartParentChanged), scenes: GameSceneFilter.AnyEditor)]
        public void PartParentChanged(Part newParent)
        {
            if (HighLogic.LoadedScene != GameScenes.EDITOR)
                return;

            AttachNode childToParent = null;
            if (newParent != null)
            {
                childToParent = part.findAttachNodeByPart(newParent);
            }

            if (shape == null || (newParent != null && childToParent == null)) //OnUpdate hasn't fired yet
            {
                toAttach.Enqueue(() => PartParentChanged(newParent));
                return;
            }
            //Debug.Log("PartParentChanged");
            if (parentAttachment != null)
            {
                RemovePartAttachment(parentAttachment);
                //Debug.LogWarning("Detatching: " + part + " from parent: " + newParent);
                parentAttachment = null;
            }

            if (newParent == null)
                return;

            //AttachNode childToParent = part.findAttachNodeByPart(newParent);
            //if (childToParent == null)
            //{
            //    Debug.LogError("*ST* unable to find parent node from child: " + part.transform);
            //    return;
            //}
            Vector3 position = transform.TransformPoint(childToParent.position);

            // ReSharper disable once InconsistentNaming
            Func<Vector3> Offset;
            if (nodeOffsets.TryGetValue(childToParent.id, out Offset))
                position -= Offset();

            Part root = EditorLogic.SortedShipList[0];

            //Debug.LogWarning("Attaching: " + part + " to new parent: " + newParent + " node:" + childToParent.id + " position=" + childToParent.position.ToString("G3"));

             //we need to delta this childAttachment down so that when the translation from the parent reaches here i ends in the right spot
            parentAttachment = AddPartAttachment(position, new ParentTransformable(root, part, childToParent));
            parentAttachment.child = newParent;

            // for symetric attachments, seems required. Don't know why.
            shape.ForceNextUpdate();
        }
 private void RemovePartAttachment(PartAttachment delete)
 {
     shape.RemoveAttachment(delete.data, false);
     Destroy(delete.follower.gameObject);
 }
Esempio n. 6
0
        private void UpdateAttachedParts()
        {
            // Update parent attachements.
            // Explain logic:
            //     xor nulls -> need to update
            //   then either both are null or both not so:
            //     both null (which means one is null) -> no need
            //     transforms discrepant -> need to update  (can this case actually happen?)
            if ((part.parent == null) != (parentAttachment == null) || (part.parent != null && part.parent != parentAttachment.child))
            {
                //Detach the old parent
                if (parentAttachment != null)
                {
                    RemovePartAttachment(parentAttachment);
                    //print("*ST* Removing old parent childAttachment:" + parentAttachment.child);

                    // Fix bug in KSP, if we don't do this then findAttachNodeByPart will return the surface child node next time
                    if (part.attachMode == AttachModes.SRF_ATTACH)
                    {
                        part.srfAttachNode.attachedPart = null;
                    }
                }

                if (part.parent == null)
                {
                    parentAttachment = null;
                }
                else
                {
                    Part       parent = part.parent;
                    AttachNode childToParent;

                    childToParent = part.findAttachNodeByPart(parent);
                    if (childToParent == null)
                    {
                        Debug.LogError("*ST* unable to find child node from parent: " + part.transform);
                        return;
                    }

                    Vector3 position = transform.TransformPoint(childToParent.position);
                    Part    root     = EditorLogic.SortedShipList[0];

                    //Debug.LogWarning("Attaching new parent: " + parent + " to " + childToParent.id + " position=" + childToParent.position.ToString("G3"));

                    // we need to delta this childAttachment down so that when the translation from the parent reaches here i ends in the right spot
                    parentAttachment       = AddPartAttachment(position, new ParentTransformable(root, part, childToParent));
                    parentAttachment.child = parent;

                    // for symetric attachments, seems required. Don't know why.
                    shape.ForceNextUpdate();
                }
            }

            LinkedListNode <PartAttachment> node = childAttachments.First;

            foreach (Part child in part.children)
            {
                while (node != null && node.Value.child != child)
                {
                    // node has been removed
                    RemoveChildPartAttachment(ref node);
                }
                if (node == null)
                {
                    // Node has been attached.
                    AddChildPartAttachment(child);
                    shape.ForceNextUpdate();
                }
                else if (node.Value.child == child)
                {
                    node = node.Next;
                }
            }
            while (node != null)
            {
                RemoveChildPartAttachment(ref node);
            }
        }
Esempio n. 7
0
 private void RemovePartAttachment(PartAttachment delete)
 {
     shape.RemoveAttachment(delete.data);
     Destroy(delete.follower.gameObject);
 }
Esempio n. 8
0
        private void PartParentChanged(Part newParent)
        {
            if (parentAttachment != null)
            {
                RemovePartAttachment(parentAttachment);
                //Debug.LogWarning("Detatching: " + part + " from parent: " + newParent);
                parentAttachment = null;
            }

            if (newParent == null)
                return;

            AttachNode childToParent = part.findAttachNodeByPart(newParent);
            if (childToParent == null)
            {
                Debug.LogError("*ST* unable to find child node from parent: " + part.transform);
                return;
            }

            Vector3 position = transform.TransformPoint(childToParent.position);
            Part root = EditorLogic.SortedShipList[0];

            //Debug.LogWarning("Attaching: " + part + " to new parent: " + newParent + " node:" + childToParent.id + " position=" + childToParent.position.ToString("G3"));

            // we need to delta this childAttachment down so that when the translation from the parent reaches here i ends in the right spot
            parentAttachment = AddPartAttachment(position, new ParentTransformable(root, part, childToParent));
            parentAttachment.child = newParent;

            // for symetric attachments, seems required. Don't know why.
            shape.ForceNextUpdate();
        }
Esempio n. 9
0
        /// <summary>
        /// Attach part to the creature.
        /// </summary>
        /// <param name="part">The part to attach.</param>
        /// <param name="slot">The slot in the list to put the part</param>
        public virtual void AddPart(Part part, int slot)
        {
            if (slot >= mPartAttachments.Count())
            {
                return;
            }

            List<PartBone> usedBones = GetPartBonesForPart(part);
            foreach (PartBone partBone in usedBones)
            {
                mUnusedPartBones.Remove(partBone);
            }

            mPartAttachments[slot] = new PartAttachment(part, usedBones);

            part.Creature = this;

            //mPartRotations = new Matrix[mPartAttachments[0].Bones.Count];
            //mBoneUp = new Vector3[mPartAttachments[0].Bones.Count];
            //mBoneForward = new Vector3[mPartAttachments[0].Bones.Count];
            //mBoneRight = new Vector3[mPartAttachments[0].Bones.Count];
            //for (int i = 0; i < PartAttachments[0].Bones.Count; ++i)
            //{
            //    mPartRotations[i] = Matrix.Identity;
            //    mBoneUp[i] = Vector3.Up;
            //    mBoneForward[i] = Vector3.Forward;
            //    mBoneRight[i] = Vector3.Right;
            //}
        }
Esempio n. 10
0
        private void UpdateAttachedParts()
        {
            // Update parent attachements.
            // Explain logic:
            //     xor nulls -> need to update
            //   then either both are null or both not so:
            //     both null (which means one is null) -> no need
            //     transforms discrepant -> need to update  (can this case actually happen?)
            if ((part.parent == null) != (parentAttachment == null) || (part.parent != null && part.parent != parentAttachment.child))
            {
                //Detach the old parent
                if (parentAttachment != null)
                {
                    RemovePartAttachment(parentAttachment);
                    //print("*ST* Removing old parent childAttachment:" + parentAttachment.child);

                    // Fix bug in KSP, if we don't do this then findAttachNodeByPart will return the surface child node next time
                    if (part.attachMode == AttachModes.SRF_ATTACH)
                        part.srfAttachNode.attachedPart = null;
                }

                if (part.parent == null)
                {
                    parentAttachment = null;
                }
                else
                {
                    Part parent = part.parent;
                    AttachNode childToParent;

                    childToParent = part.findAttachNodeByPart(parent);
                    if (childToParent == null)
                    {
                        Debug.LogError("*ST* unable to find child node from parent: " + part.transform);
                        return;
                    }

                    Vector3 position = transform.TransformPoint(childToParent.position);
                    Part root = EditorLogic.SortedShipList[0];

                    //Debug.LogWarning("Attaching new parent: " + parent + " to " + childToParent.id + " position=" + childToParent.position.ToString("G3"));

                    // we need to delta this childAttachment down so that when the translation from the parent reaches here i ends in the right spot
                    parentAttachment = AddPartAttachment(position, new ParentTransformable(root, part, childToParent));
                    parentAttachment.child = parent;

                    // for symetric attachments, seems required. Don't know why.
                    shape.ForceNextUpdate();
                }
            }

            LinkedListNode<PartAttachment> node = childAttachments.First;
            foreach (Part child in part.children)
            {
                while (node != null && node.Value.child != child)
                {
                    // node has been removed
                    RemoveChildPartAttachment(ref node);
                }
                if (node == null)
                {
                    // Node has been attached.
                    AddChildPartAttachment(child);
                    shape.ForceNextUpdate();
                }
                else if (node.Value.child == child)
                {
                    node = node.Next;
                }
            }
            while (node != null)
            {
                RemoveChildPartAttachment(ref node);
            }
        }