Пример #1
0
    void Update()
    {
        if (!PlacementObject)
        {
            enabled = false;
            return;
        }

        if (!SelectionManager.Current.IsPointerOnGameplay())
        {
            return;
        }

        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 1000, RaycastMask))
        {
            if (LastSym != SymmetryWindow.GetSymmetryType())
            {
                GenerateSymmetry();
            }

            if (!PlacementObject.activeSelf)
            {
                PlacementObject.SetActive(true);
                for (int i = 0; i < PlacementSymmetry.Length; i++)
                {
                    PlacementSymmetry[i].SetActive(true);
                }
            }


            // Rotate
            bool Rotating = false;
            if (SelectionManager.AllowRotation)
            {
                if (Input.GetKeyDown(KeyCode.E))
                {
                    /*if (MinRotAngle > 1)
                     * {
                     *      PlaceAngle += MinRotAngle;
                     *      if (PlaceAngle >= 360)
                     *              PlaceAngle = 0;
                     *      PlacementObject.transform.rotation = Quaternion.Euler(Vector3.up * PlaceAngle);
                     * }
                     * else*/
                    {
                        RotationStartMousePos = Input.mousePosition;
                        StartRotation         = PlacementObject.transform.eulerAngles;
                    }
                }
                else if (Input.GetKeyUp(KeyCode.E))
                {
                    ChangeControlerType.ChangeCurrentControler(0);
                }
                else if (Input.GetKey(KeyCode.E))
                {
                    Rotating = true;

                    Vector3 NewRot = StartRotation + Vector3.down * ((Input.mousePosition.x - RotationStartMousePos.x) * 0.5f);

                    if (MinRotAngle > 1)
                    {
                        int ClampedRot = (int)(NewRot.y / MinRotAngle);
                        NewRot.y = MinRotAngle * ClampedRot;
                    }

                    PlacementObject.transform.eulerAngles = NewRot;
                }
            }

            //Scale
            bool Scaling = false;
            if (!Rotating && SelectionManager.AllowScale)
            {
                if (Input.GetKeyDown(KeyCode.R))
                {
                    RotationStartMousePos = Input.mousePosition;
                    StartScale            = PlacementObject.transform.localScale;
                    LastHitPoint          = hit.point;
                }
                else if (Input.GetKeyUp(KeyCode.R))
                {
                    ChangeControlerType.ChangeCurrentControler(0);
                }
                else if (Input.GetKey(KeyCode.R))
                {
                    Scaling = true;

                    Vector3 Scale        = StartScale;
                    float   ScalingScale = (Scale.x + Scale.y + Scale.z) / 3f;
                    float   ScalePower   = (Input.mousePosition.x - RotationStartMousePos.x) * 0.003f * ScalingScale;

                    Scale.x = Mathf.Clamp(Scale.x + ScalePower, 0.005f, 100);
                    Scale.y = Mathf.Clamp(Scale.y + ScalePower, 0.005f, 100);
                    Scale.z = Mathf.Clamp(Scale.z + ScalePower, 0.005f, 100);
                    PlacementObject.transform.localScale = Scale;
                }
            }

            if (!Rotating)
            {
                Vector3 Point = hit.point;
                if (Scaling)
                {
                    Point = LastHitPoint;
                }

                if (SelectionManager.Current.SnapToGrid)
                {
                    PlacementObject.transform.position = ScmapEditor.SnapToGridCenter(Point, true, SnapToWater);
                }
                else if (SnapToWater)
                {
                    PlacementObject.transform.position = ScmapEditor.ClampToWater(Point);
                }
                else
                {
                    PlacementObject.transform.position = Point;
                }
            }

            UpdateSymmetryObjects();

            // Action
            if (Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.R) || Input.GetKey(KeyCode.B) || Input.GetKey(KeyCode.M))
            {
            }
            else if (Input.GetMouseButtonDown(0))
            {
                Place();
            }
        }
        else if (PlacementObject.activeSelf)
        {
            PlacementObject.SetActive(false);
            for (int i = 0; i < PlacementSymmetry.Length; i++)
            {
                PlacementSymmetry[i].SetActive(false);
            }
        }
    }