コード例 #1
0
        public void SetupBodyPart(BodyPartController bpc)
        {
            BodyPartController flipped = Instantiate(bpc.gameObject, bpc.transform.parent).GetComponent <BodyPartController>();

            bpc.Flipped     = flipped;
            flipped.Flipped = bpc;

            flipped.gameObject.name = bpc.gameObject.name + "(Flipped)";
            if (bpc is LimbController)
            {
                LimbController limb = bpc as LimbController;

                limbs.Add(limb);
                limbs.Add(limb.FlippedLimb);
            }
            else
            {
                flipped.Model.localScale = new Vector3(-flipped.Model.localScale.x, flipped.Model.localScale.y, flipped.Model.localScale.z);
            }

            #region Interact
            UnityAction onPress = delegate
            {
                CreatureCreator.Instance.CameraOrbit.Freeze();

                bpc.transform.SetParent(Dynamic.Transform);
                flipped.transform.SetParent(Dynamic.Transform);

                bpc.gameObject.SetLayerRecursively(LayerMask.NameToLayer("Ignore Raycast"), new List <string> {
                    "Tools"
                });
                flipped.gameObject.SetLayerRecursively(LayerMask.NameToLayer("Ignore Raycast"), new List <string> {
                    "Tools"
                });
            };
            UnityAction onRelease = delegate
            {
                CreatureCreator.Instance.CameraOrbit.Unfreeze();

                DetachBodyPart(bpc);
                DetachBodyPart(flipped);

                if (Physics.Raycast(RectTransformUtility.ScreenPointToRay(CreatureCreator.Instance.CameraOrbit.Camera, Input.mousePosition), out RaycastHit raycastHit) && raycastHit.collider.CompareTag("Player"))
                {
                    AttachBodyPart(bpc);
                    AttachBodyPart(flipped);
                }
                else
                {
                    audioSource.PlayOneShot(poofAudioClip);
                    Instantiate(poofEffect, bpc.Drag.IsPressing ? bpc.transform.position : flipped.transform.position, Quaternion.identity, Dynamic.Transform);
                    RemoveFromStatistics(bpc.name);

                    if (bpc is LimbController)
                    {
                        LimbController limb = bpc as LimbController;

                        limbs.Remove(limb);
                        limbs.Remove(limb.FlippedLimb);
                    }

                    Destroy(bpc.gameObject);
                    Destroy(flipped.gameObject);
                }

                bpc.gameObject.SetLayerRecursively(LayerMask.NameToLayer("Body"), new List <string> {
                    "Tools"
                });
                flipped.gameObject.SetLayerRecursively(LayerMask.NameToLayer("Body"), new List <string> {
                    "Tools"
                });

                bpc.Drag.Plane = flipped.Drag.Plane = new Plane(Vector3.right, Vector3.zero);
            };
            UnityAction onDrag = delegate
            {
                if (Physics.Raycast(RectTransformUtility.ScreenPointToRay(CreatureCreator.Instance.CameraOrbit.Camera, Input.mousePosition), out RaycastHit raycastHit) && raycastHit.collider.CompareTag("Player"))
                {
                    bpc.Drag.Draggable = false;

                    bpc.transform.position = raycastHit.point;
                    bpc.transform.rotation = Quaternion.LookRotation(raycastHit.normal);

                    if (Mathf.Abs(bpc.transform.position.x) > settings.MergeThreshold)
                    {
                        flipped.gameObject.SetActive(true);
                        flipped.transform.position = new Vector3(-bpc.transform.position.x, bpc.transform.position.y, bpc.transform.position.z);
                        flipped.transform.rotation = Quaternion.Euler(bpc.transform.rotation.eulerAngles.x, -bpc.transform.rotation.eulerAngles.y, -bpc.transform.rotation.eulerAngles.z);
                    }
                    else
                    {
                        flipped.gameObject.SetActive(false);
                        bpc.transform.position = new Vector3(0, bpc.transform.position.y, bpc.transform.position.z);
                        bpc.transform.rotation = Quaternion.LookRotation(new Vector3(0, raycastHit.normal.y, raycastHit.normal.z));

                        if (bpc is LimbController)
                        {
                            foreach (Transform bone in (bpc as LimbController).Bones)
                            {
                                bone.position = new Vector3(0, bone.position.y, bone.position.z);
                            }
                        }
                    }
                }
                else
                {
                    bpc.Drag.Draggable = true;
                    flipped.gameObject.SetActive(false);
                }
            };
            UnityAction onFlippedDrag = delegate
            {
                if (Physics.Raycast(RectTransformUtility.ScreenPointToRay(CreatureCreator.Instance.CameraOrbit.Camera, Input.mousePosition), out RaycastHit raycastHit) && raycastHit.collider.CompareTag("Player"))
                {
                    flipped.Drag.Draggable = false;

                    flipped.transform.position = raycastHit.point;
                    flipped.transform.rotation = Quaternion.LookRotation(raycastHit.normal);

                    if (Mathf.Abs(flipped.transform.position.x) > settings.MergeThreshold)
                    {
                        bpc.gameObject.SetActive(true);
                        bpc.transform.position = new Vector3(-flipped.transform.position.x, flipped.transform.position.y, flipped.transform.position.z);
                        bpc.transform.rotation = Quaternion.Euler(flipped.transform.rotation.eulerAngles.x, -flipped.transform.rotation.eulerAngles.y, -flipped.transform.rotation.eulerAngles.z);
                    }
                    else
                    {
                        bpc.gameObject.SetActive(false);
                        flipped.transform.position = new Vector3(0, flipped.transform.position.y, flipped.transform.position.z);
                        flipped.transform.rotation = Quaternion.LookRotation(new Vector3(0, raycastHit.normal.y, raycastHit.normal.z));

                        if (bpc is LimbController)
                        {
                            foreach (Transform bone in (flipped as LimbController).Bones)
                            {
                                bone.position = new Vector3(0, bone.position.y, bone.position.z);
                            }
                        }
                    }
                }
                else
                {
                    flipped.Drag.Draggable = true;
                    bpc.gameObject.SetActive(false);
                }
            };

            bpc.Drag.OnPress.AddListener(onPress);
            bpc.Drag.OnDrag.AddListener(onDrag);
            bpc.Drag.OnRelease.AddListener(onRelease);
            flipped.Drag.OnPress.AddListener(onPress);
            flipped.Drag.OnDrag.AddListener(onFlippedDrag);
            flipped.Drag.OnRelease.AddListener(onRelease);
            #endregion
        }