public bool Add(int index, Vector3 position, Quaternion rotation, float size)
        {
            if ((data.bones.Count + 1) <= settings.MinMaxBones.y && (statistics.Complexity + 1) <= settings.MaximumComplexity)
            {
                #region Detach Body Parts
                BodyPartController[] bpcs = root.GetComponentsInChildren <BodyPartController>();

                foreach (BodyPartController bpc in bpcs)
                {
                    DetachBodyPart(bpc);
                    bpc.transform.SetParent(null, true);
                }
                #endregion

                #region Bone
                GameObject boneGameObject = Instantiate(boneTool, root, false);
                boneGameObject.name  = "Bone." + (root.childCount - 1);
                boneGameObject.layer = LayerMask.NameToLayer("Tools");

                if (data.bones.Count == 0)
                {
                    DestroyImmediate(boneGameObject.GetComponent <HingeJoint>());
                }
                #endregion

                #region Tools
                Drag drag = boneGameObject.GetComponent <Drag>();
                drag.OnPress.AddListener(delegate
                {
                    CreatureCreator.Instance.CameraOrbit.Freeze();
                });
                drag.OnRelease.AddListener(delegate
                {
                    CreatureCreator.Instance.CameraOrbit.Unfreeze();
                    UpdateMeshCollider();
                    UpdateBoneConfiguration();

                    foreach (LimbController limb in limbs)
                    {
                        limb.UpdateMeshCollider();
                    }
                });

                Hover hover = boneGameObject.GetComponent <Hover>();
                hover.OnEnter.AddListener(delegate
                {
                    if (!Input.GetMouseButton(0))
                    {
                        CreatureCreator.Instance.CameraOrbit.Freeze();
                        SetBonesVisibility(true);
                    }
                });
                hover.OnExit.AddListener(delegate
                {
                    if (!Input.GetMouseButton(0))
                    {
                        CreatureCreator.Instance.CameraOrbit.Unfreeze();

                        if (!Selected)
                        {
                            SetBonesVisibility(false);
                        }
                    }
                });

                Click click = boneGameObject.GetComponent <Click>();
                click.OnClick.AddListener(delegate
                {
                    SetSelected(true);
                });

                frontArrow.SetParent(root.GetChild(0));
                backArrow.SetParent(root.GetChild(root.childCount - 1));
                frontArrow.localPosition = backArrow.localPosition = Vector3.zero;
                frontArrow.localRotation = Quaternion.Euler(0, 180, 0);
                backArrow.localRotation  = Quaternion.identity;
                #endregion

                data.bones.Insert(index, new Bone(position, rotation, size));
                statistics.Complexity++;

                Setup();

                #region Reattach Body Parts
                foreach (BodyPartController bpc in bpcs)
                {
                    AttachBodyPart(bpc);
                }
                #endregion

                return(true);
            }
            return(false);
        }
        private void Initialize()
        {
            #region Creature
            transform.position = Vector3.zero;

            GameObject model = new GameObject("Model");
            model.transform.SetParent(transform);
            model.transform.localPosition = Vector3.zero;
            model.transform.localRotation = Quaternion.identity;

            root = new GameObject("Root").transform;
            root.SetParent(transform);
            root.localPosition = Vector3.zero;
            root.localRotation = Quaternion.identity;

            skinnedMeshRenderer                     = model.AddComponent <SkinnedMeshRenderer>();
            skinnedMeshRenderer.sharedMesh          = mesh = model.AddComponent <MeshFilter>().sharedMesh = new Mesh();
            skinnedMeshRenderer.rootBone            = root.transform;
            skinnedMeshRenderer.updateWhenOffscreen = true;

            skinnedMeshRenderer.sharedMaterial = bodyMaterial;
            SetColours(Color.white, Color.black);
            SetPattern("");

            audioSource        = gameObject.AddComponent <AudioSource>();
            audioSource.volume = 0.25f;

            outline = model.AddComponent <Outline>();
            outline.OutlineWidth = 5f;

            meshCollider = gameObject.AddComponent <MeshCollider>();
            mesh.name    = "Body";
            #endregion

            #region Tools
            Drag drag = gameObject.AddComponent <Drag>();
            drag.OnPress.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Freeze();
            });
            drag.OnRelease.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Unfreeze();
                UpdateBoneConfiguration();

                foreach (LimbController limb in limbs)
                {
                    limb.UpdateMeshCollider();
                }
            });

            Click click = gameObject.AddComponent <Click>();
            click.OnClick.AddListener(delegate
            {
                SetSelected(true);
            });

            Hover hover = gameObject.AddComponent <Hover>();
            hover.OnEnter.AddListener(delegate
            {
                if (!Input.GetMouseButton(0))
                {
                    CreatureCreator.Instance.CameraOrbit.Freeze();
                    SetBonesVisibility(true);
                }
            });
            hover.OnExit.AddListener(delegate
            {
                if (!Input.GetMouseButton(0))
                {
                    CreatureCreator.Instance.CameraOrbit.Unfreeze();

                    if (!Selected)
                    {
                        SetBonesVisibility(false);
                    }
                }
            });

            backArrow  = Instantiate(stretchTool).transform;
            frontArrow = Instantiate(stretchTool).transform;
            frontArrow.Find("Model").localPosition = backArrow.Find("Model").localPosition = Vector3.forward * (settings.Length / 2f + settings.Radius + 0.05f);

            Drag frontArrowDrag = frontArrow.GetComponentInChildren <Drag>();
            frontArrowDrag.OnPress.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Freeze();
            });
            frontArrowDrag.OnRelease.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Unfreeze();
            });
            frontArrowDrag.OnDrag.AddListener(delegate
            {
                Vector3 displacement = frontArrowDrag.TargetWorldPosition - frontArrow.position;
                if (displacement.magnitude > settings.Length)
                {
                    if (Vector3.Dot(displacement.normalized, frontArrow.forward) > 0.1f && (data.bones.Count + 1) <= settings.MinMaxBones.y)
                    {
                        UpdateBoneConfiguration();

                        Vector3 direction   = (frontArrowDrag.TargetMousePosition - frontArrow.position).normalized;
                        Vector3 position    = data.bones[0].Position + direction * settings.Length;
                        Quaternion rotation = Quaternion.LookRotation(-direction, frontArrow.up);

                        if (Add(0, position, rotation, Mathf.Clamp(data.bones[0].Size * 0.75f, 0f, 100f)))
                        {
                            audioSource.PlayOneShot(stretchAudioClip);
                        }

                        frontArrowDrag.OnMouseDown();
                    }
                    else if (Vector3.Dot(displacement.normalized, frontArrow.forward) < -0.1f)
                    {
                        if (RemoveFromFront())
                        {
                            audioSource.PlayOneShot(stretchAudioClip);
                        }
                    }
                }
            });

            Drag backArrowDrag = backArrow.GetComponentInChildren <Drag>();
            backArrowDrag.OnPress.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Freeze();
            });
            backArrowDrag.OnRelease.AddListener(delegate
            {
                CreatureCreator.Instance.CameraOrbit.Unfreeze();
            });
            backArrowDrag.OnDrag.AddListener(delegate
            {
                Vector3 displacement = backArrowDrag.TargetWorldPosition - backArrow.position;
                if (displacement.magnitude > settings.Length)
                {
                    if (Vector3.Dot(displacement.normalized, backArrow.forward) > 0.1f && (data.bones.Count + 1) <= settings.MinMaxBones.y)
                    {
                        UpdateBoneConfiguration();

                        Vector3 direction   = (backArrowDrag.TargetMousePosition - backArrow.position).normalized;
                        Vector3 position    = data.bones[root.childCount - 1].Position + direction * settings.Length;
                        Quaternion rotation = Quaternion.LookRotation(direction, backArrow.up);

                        if (Add(root.childCount, position, rotation, Mathf.Clamp(data.bones[root.childCount - 1].Size * 0.75f, 0f, 100f)))
                        {
                            audioSource.PlayOneShot(stretchAudioClip);
                        }

                        backArrowDrag.OnMouseDown();
                    }
                    else if (Vector3.Dot(displacement.normalized, backArrow.forward) < -0.1f)
                    {
                        if (RemoveFromBack())
                        {
                            audioSource.PlayOneShot(stretchAudioClip);
                        }
                    }
                }
            });
            #endregion
        }
Пример #3
0
        protected override void Start()
        {
            base.Start();

            hover.OnEnter.AddListener(delegate
            {
                if (!Input.GetMouseButton(0))
                {
                    SetToolsVisibility(true);
                }
            });
            hover.OnExit.AddListener(delegate
            {
                if (!Input.GetMouseButton(0))
                {
                    SetToolsVisibility(false);
                }
            });

            Drag.OnRelease.AddListener(delegate
            {
                UpdateMeshCollider();
                FlippedLimb.UpdateMeshCollider();

                if (!hover.IsOver)
                {
                    SetToolsVisibility(false);
                }
            });
            Drag.OnDrag.AddListener(delegate
            {
                FlippedLimb.bones[bones.Length - 1].position = new Vector3(-bones[bones.Length - 1].position.x, bones[bones.Length - 1].position.y, bones[bones.Length - 1].position.z);
            });

            scroll.OnScrollUp.RemoveAllListeners();
            scroll.OnScrollDown.RemoveAllListeners();

            for (int i = 2; i < bones.Length; i++)
            {
                int index = i;

                Transform bone        = bones[index];
                Transform flippedBone = FlippedLimb.bones[index];

                #region Interact
                Hover boneHover = bone.GetComponentInChildren <Hover>();
                boneHover.OnEnter.AddListener(delegate
                {
                    if (!Input.GetMouseButton(0))
                    {
                        SetToolsVisibility(true);
                    }
                });
                boneHover.OnExit.AddListener(delegate
                {
                    if (!Input.GetMouseButton(0))
                    {
                        SetToolsVisibility(false);
                    }
                });

                Drag boneDrag = bone.GetComponentInChildren <Drag>();
                boneDrag.OnPress.AddListener(delegate
                {
                    CreatureCreator.Instance.CameraOrbit.Freeze();

                    SetToolsVisibility(true);
                });
                boneDrag.OnDrag.AddListener(delegate
                {
                    flippedBone.position = new Vector3(-bone.position.x, bone.position.y, bone.position.z);
                });
                boneDrag.OnRelease.AddListener(delegate
                {
                    CreatureCreator.Instance.CameraOrbit.Unfreeze();

                    if (!boneHover.IsOver && !hover.IsOver)
                    {
                        SetToolsVisibility(false);
                    }

                    UpdateMeshCollider();
                    FlippedLimb.UpdateMeshCollider();
                });
                #endregion
            }

            UpdateMeshCollider();
            SetToolsVisibility(false);
        }