示例#1
0
    public Homotopy newHomotopy(MPath path1, MPath midPath)
    {
        Homotopy hom = new Homotopy(path1, midPath, homotopyMaterial);

        Debug.Log(midPath.ToString());
        Debug.Log(midPath.Count.ToString());

        var color = path1.GetColor();

        midPath.SetColor(color);
        color.a = color.a / 3f;
        path1.SetColor(color);
        //		for (int i = 0; i < Statics.numHomotopyLines; i++) {
//			GameObject lineObj = new GameObject ();
//			var line = lineObj.AddComponent<LineRenderer> ();
//			line.positionCount = hom.counter;
//			line.startWidth = Statics.lineThickness / 3f;
//			line.endWidth = Statics.lineThickness / 3f;
//			line.numCornerVertices = 0;
//			line.GetComponent<Renderer> ().material = pathMaterial;
//			line.GetComponent<Renderer> ().material.SetColor ("_Color", Statics.homotopyColor);
////			hom.homotopyLines.Add (line);
//		}
        for (int i = 0; i < path1.Count; i++)
        {
            midPath.SetPosition(i, path1.GetPosition(i));
            if (path1.hasNormals)
            {
                midPath.SetNormal(i, path1.GetNormal(i));
            }
        }
        midPath.SetMesh();
//		int counter = 1;
//		float sum = 0f;
//		var length = midPath.Length ();
//		for (int i = 0; i < count - 1; i++) {
//			if (sum > counter * (length / (Statics.numHomotopyLines + 1))) {
//				var line = hom.homotopyLines [counter - 1];
//				line.SetPosition (0, midpath.GetPosition (i));
//				counter++;
//			}
//			sum += Vector3.Distance (midpath.GetPosition (i), midpath.GetPosition (i + 1));
//		}
        return(hom);
    }
示例#2
0
    void setPathToDrag(Vector3 touchPoint, int pathNumber)
    {
        int     numOnPath = -1;
        Vector3 vector    = new Vector3();
        bool    isOnPath  = GetPositionOnPath(pathNumber, touchPoint, ref numOnPath, ref vector);
        Path    path;

        if (pathNumber == -2)
        {
            path = actHomotopy.midPath;
        }
        else
        {
            path = level.paths [pathNumber];
        }
        if (isOnPath && numOnPath != 0 && numOnPath != path.Count - 1)
        {
            Debug.Log("Start Dragging Line " + pathNumber);
            if (pathNumber != -2)
            {
                var path1 = level.paths [pathNumber];
                if (actHomotopy != null)
                {
                    Destroy(GameObject.Find("MidPath"));
                    actHomotopy.Clear();
                    actHomotopy = null;
                }
                var actMidPath = level.NewPath(path1.color, "MidPath", path1.dotFrom, path1.dotTo);
                actHomotopy = level.NewHomotopy(path1, actMidPath);
            }
            upperBound       = actHomotopy.midPath.line.positionCount;
            lowerBound       = -1;
            draggingPath     = -2;
            draggingPosition = numOnPath;
        }
        else
        {
            Debug.Log("Not on Line");
        }
    }
示例#3
0
    // Update
    void Update()
    {
        int tapCount = Input.touchCount;

        if (tapCount == 1 && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))            // && !EventSystem.current.IsPointerOverGameObject (Input.GetTouch (0).fingerId)
        {
            Touch   touch1     = Input.GetTouch(0);
            Vector3 touchPoint = Camera.main.ScreenToWorldPoint(touch1.position);
            touchPoint.z = 0;
            if (touch1.phase == TouchPhase.Began)
            {
                /**
                 *
                 *                  START
                 *
                 *
                 * */

                touchStart    = touchPoint;
                touchDuration = Time.time;
                bool       isHit = false;
                RaycastHit hit3D = new RaycastHit();
                var        ray   = Camera.main.ScreenPointToRay(touch1.position);
                isHit = Physics.Raycast(ray, out hit3D);
                RaycastHit2D hit = Physics2D.Raycast(touchPoint, Vector3.zero);
                if (isHit)
                {
                    Debug.Log("Hit! 3d");
                    if (hit3D.collider.gameObject.CompareTag("Dot") && level.pathDrawingAllowed && !isDrawingPath)
                    {
                        DrawLine3D(hit3D);
                        Statics.isDragging = true;
                    }
                    else if (hit3D.collider.gameObject.CompareTag("Obstacle") && level.staticsAllowed)
                    {
                        var index = level.statics.IndexOf(hit.collider.gameObject);
                        StartDraggingObstacle(index, touchPoint);
                        Statics.isDragging = true;
                    }
                    else if (level.homotopiesAllowed)
                    {
                        int  pathNumber = -1;
                        bool isOnPath   = IsOnPath(hit3D.point, ref pathNumber, actHomotopy != null);
                        if (isOnPath)
                        {
                            Debug.Log("Started Dragging Path");
                            setPathToDrag(hit3D.point, pathNumber);
                            Statics.isDragging = true;
                            touchPos           = farAwayVec;
                        }
                        else
                        {
                            draggingPath = -1;
                            Debug.Log("Tried Dragging, but no path there");
                        }
                    }
                }
            }
            else if (touch1.phase == TouchPhase.Moved)
            {
                /**
                 *
                 *                  MOVE
                 *
                 *
                 * */
                if (Statics.drawCircle)
                {
                    circle.transform.localScale = Vector3.one * Vector3.Magnitude(circle.transform.position - touchPoint);
                }
                else if (Statics.drawRectangle)
                {
                    var xFactor = drawPos.x - touchPoint.x;
                    var yFactor = drawPos.y - touchPoint.y;
                    rectangle.transform.localScale = new Vector3(xFactor, yFactor, 0);
                    rectangle.transform.position   = drawPos - new Vector3(xFactor / 2f, yFactor / 2f, 0);
                }
                else if (draggingObstacle != null)
                {
                    DragObstacle(touchPoint);
                }
                else
                {
                    Ray        ray = Camera.main.ScreenPointToRay(touch1.position);
                    RaycastHit hit = new RaycastHit();
                    if (Physics.Raycast(ray, out hit))
                    {
                        lastTouch = touchPos;
                        touchPos  = Misc.SetOnSurface(hit.point, Statics.dotSpacer);
                        if (dotMove != null)
                        {
                            Statics.isDragging = true;
                            var direction = touchPos - dotMove.transform.position;
                            moveDot(direction);
                        }
                        else if (draggingPath != -1 && lastTouch != farAwayVec)
                        {
                            DragPath(touchPos, lastTouch);
                        }
                    }
                }
            }
            else if (touch1.phase == TouchPhase.Ended || touch1.phase == TouchPhase.Canceled)
            {
                /**
                 *
                 *                  END
                 *
                 *
                 * */
                if (drawing)
                {
                    Debug.Log("Drawing ended");
                    drawing               = false;
                    Statics.drawCircle    = false;
                    Statics.drawRectangle = false;
                }
                touchDuration = Time.time - touchDuration;
                var touchDistance = Vector2.Distance(touchStart, touchPoint);
                Debug.Log("Touch Duration = " + touchDuration + ", touch distance = " + touchDistance);
                if (touchDuration < 0.2f)
                {
                    int  pathNum  = -1;
                    bool isOnPath = IsOnPath(touchPoint, ref pathNum);
                    if (level.dotSettingAllowed && !isOnPath)
                    {
                        DrawDot(touchPoint);
                    }
                }
                if (dotMove != null)
                {
                    Debug.Log("Finish Line");
                    dotMove.GetComponent <Rigidbody> ().velocity = Vector3.zero;
                    FinishLine();
                }
                else if (draggingObstacle != null)
                {
                    Debug.Log("Finish dragging gameObject");
                    draggingObstacle   = null;
                    Statics.isDragging = false;
                }

                if (draggingPath != -1)
                {
                    Debug.Log("Finish dragging Path");
                    //Guess path which is trying do be reached, and snuggle this path against the other
                    EndDraggingPath();
                    Statics.isDragging = false;
                }
                touchDuration = 0f;
            }
        }
        else if (tapCount == 2 && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(1).fingerId))
        {
            Touch touch1 = Input.GetTouch(0);
            Touch touch2 = Input.GetTouch(1);
            if (touch2.phase == TouchPhase.Began)
            {
                bool       isHit1 = false;
                RaycastHit hit3D1 = new RaycastHit();
                isHit1 = Physics.Raycast(Camera.main.ScreenPointToRay(touch1.position), out hit3D1);

                bool       isHit2 = false;
                RaycastHit hit3D2 = new RaycastHit();
                isHit2 = Physics.Raycast(Camera.main.ScreenPointToRay(touch2.position), out hit3D2);

                if (isHit1 && isHit2)
                {
                    int  pathNumber1 = -1;
                    int  pathNumber2 = -1;
                    bool isOnPath1   = IsOnPath(hit3D1.point, ref pathNumber1);
                    bool isOnPath2   = IsOnPath(hit3D2.point, ref pathNumber2);
                    if (isOnPath1 && isOnPath2 && pathNumber1 != pathNumber2)
                    {
                        Debug.Log("Try to concatenate Path " + pathNumber1 + " and Path " + pathNumber2);
                        var path1 = level.paths [pathNumber1];
                        var path2 = level.paths [pathNumber2];
                        if (path1.dotTo.Equals(path2.dotFrom))
                        {
                            actHomotopy.Clear();
                            actHomotopy  = null;
                            draggingPath = -1;
                            Debug.Log("Concatenate!");
                            path1.Concatenate(path2);
                            level.paths.Remove(path2);
                        }
                        else
                        {
                            Debug.Log("Not concatenable");
                        }
                    }
                }
            }
        }
    }
示例#4
0
    void CheckIfHomotopyDone()
    {
        Debug.Log("Start CheckIfHomotopyDone");
        var        midPath        = actHomotopy.midPath;
        var        dotFromPos     = midPath.dotFrom.transform.position;
        var        dotToPos       = midPath.dotTo.transform.position;
        var        circleSize     = level.dotPrefab.transform.localScale.x;
        var        pathHomClasses = level.pathHomClasses;
        List <int> homClass       = null;
        var        indexOfPath1   = level.paths.IndexOf(actHomotopy.path1);

        foreach (var item in pathHomClasses)
        {
            if (item.Contains(indexOfPath1))
            {
                homClass = item;
            }
        }
        Debug.Log("Homclass of this homotopy contains " + string.Join(",", homClass.Select(x => x.ToString()).ToArray()));
        foreach (var otherPathNum in homClass)
        {
            if (otherPathNum != indexOfPath1)
            {
                var otherPath = level.paths [otherPathNum];
                Debug.Log("Check Path " + otherPath.pathNumber);
                bool homotopic = true;
                Debug.Log("Check if homotopic");
                for (int i = 0; i < midPath.Count; i++)
                {
                    var midPathPos = midPath.line.GetPosition(i);
                    if (Vector2.Distance(midPathPos, dotFromPos) > circleSize && Vector2.Distance(midPathPos, dotToPos) > circleSize)
                    {
                        bool existsNearNode = false;
                        for (int j = 0; j < otherPath.Count; j++)
                        {
                            var otherPathPos = otherPath.line.GetPosition(j);
                            var dist         = Vector2.Distance(midPathPos, otherPathPos);
                            if (dist < Statics.homotopyNearness)
                            {
                                existsNearNode = true;
                                break;
                            }
                        }
                        if (!existsNearNode)
                        {
                            homotopic = false;
                            break;
                        }
                    }
                }
                if (homotopic)
                {
                    for (int i = 0; i < otherPath.Count; i++)
                    {
                        var otherPathPos = otherPath.line.GetPosition(i);
                        if (Vector2.Distance(otherPathPos, dotFromPos) > circleSize && Vector2.Distance(otherPathPos, dotToPos) > circleSize)
                        {
                            bool existsNearNode = false;
                            for (int j = 0; j < midPath.Count; j++)
                            {
                                var midPathPos = midPath.line.GetPosition(j);
                                var dist       = Vector2.Distance(otherPathPos, midPathPos);
                                if (dist < Statics.homotopyNearness)
                                {
                                    existsNearNode = true;
                                    break;
                                }
                            }
                            if (!existsNearNode)
                            {
                                homotopic = false;
                                break;
                            }
                        }
                    }
                }
                Debug.Log("Check if homotopic, done : " + homotopic.ToString());
                if (homotopic)
                {
                    if (Statics.mesh)
                    {
                        actHomotopy.setMesh(level.GetStaticPositions());
                    }
                    else
                    {
                        actHomotopy.AddCurveToBundle(level.pathFactory, draggingPosition);
                    }
                    actHomotopy.SetColor(level.GetRandomColor());
                    otherPath.SetColor(midPath.color);
                    Destroy(actHomotopy.midPath.line.gameObject);
                    actHomotopy = null;
                    break;
                }
            }
        }
    }