Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        { //mouse is down
            currentMousePosition = Input.mousePosition;
            //get the mouse's world position
            currentMousePosition = camera.ScreenToWorldPoint(new Vector3(currentMousePosition.x, currentMousePosition.y, 10));

            if (((Vector3.Distance(currentMousePosition, lastMousePosition) > threshold) &&
                 (maxDrawDistance - distanceDrawn > Vector3.Distance(currentMousePosition, lastMousePosition)) ||
                 (points.Count == 0)))        //do not keep point if it is too close, or if it exceeds remaining draw distance
            //unless it is the first point, in which case it should be added regardless
            {
                //check if the new point overlaps some collider that we can't draw over
                LayerMask  mask        = LayerMask.GetMask("Cutouts", "Terrain");
                Collider2D hitCollider = Physics2D.OverlapPoint((Vector2)currentMousePosition, mask);
                if (hitCollider != null)
                {
                    return;                     //if it does don't place it
                }
                //check if the new point overlaps a drawable zone
                mask        = LayerMask.GetMask("Draw Area");
                hitCollider = Physics2D.OverlapPoint((Vector2)currentMousePosition, mask);
                if (hitCollider == null)
                {
                    return;                     //if it doesn't don't place it
                }

                //for points after the first, we need to check for self-intersections and going through cutouts.
                if (points.Count != 0)
                {
                    if (!ic.CheckIfAddable(currentMousePosition, points, threshold))                      //check self-intersection, separate check because it is long
                    {
                        return;
                    }
                    distanceDrawn += Vector3.Distance(currentMousePosition, lastMousePosition);                     //reduce drawing distance
                }

                //add the point to the line
                points.Add(currentMousePosition);
                lrenderer.positionCount++;
                lrenderer.SetPosition(lrenderer.positionCount - 1, points[points.Count - 1]);

                lastMousePosition = currentMousePosition;                 //update position of last point
            }
        }
        else //if the mouse isn't down, input is done
        {
            if (points.Count > 1f)   //if there are more than 2 points already drawn, try to materialize it.
            {
                spawner.MaterializeObject(points);
                SetupNewObject();
            }
        }
        if (Input.GetMouseButtonDown(1))          //on right-click, cancel the drawing
        {
            SetupNewObject();
        }
    }