void CreateTriangleObject(
     Vector2 trianglePosition,
     int type,
     float globalAngle,
     touchVertex vertexCorner,
     touchVertex vertexOpposite,
     touchVertex vertexAdjecent)
 {
     if (allTriangles.Find(TriangleObject => TriangleObject.type == type) == null)
     {
         Debug.Log("Triangle Created, type: " + type);
         allTriangles.Add(new TriangleObject(
                              trianglePosition,
                              type,
                              globalAngle,
                              vertexCorner,
                              vertexOpposite,
                              vertexAdjecent,
                              Convert2DPosition(trianglePosition),
                              GetGameObject(type)));
     }
     else
     {
         //Debug.Log("triangle exist");
     }
 }
    /*
     *  void CreateTriangleObject(touchVertex corner, touchVertex adjacent, touchVertex opposite){
     *
     *      int triangleID = corner.touchId;
     *      List<touchVertex> triangleVertices = new List<touchVertex>();
     *      triangleVertices.Add(corner);
     *      triangleVertices.Add(adjacent);
     *      triangleVertices.Add(opposite);
     *      int type = GetTriangleType(corner, adjacent, opposite);
     *      corner.partOfTriangle = true;
     *      adjacent.partOfTriangle = true;
     *      opposite.partOfTriangle = true;
     *
     *      allTriangles.Add(new TriangleObject(triangleVertices, triangleID, type));
     *  }
     */
    int GetTriangleType(touchVertex corner, touchVertex adjacent, touchVertex opposite)
    {
        float ratio;

        ratio = (opposite.position - corner.position).magnitude / (adjacent.position - corner.position).magnitude;
        Debug.Log("Ratio: " + ratio);
        if (ratio < 0.70)
        {
            return(2);
        }
        else
        {
            return(3);
        }
    }
Example #3
0
 public TriangleObject(
     Vector2 pos,
     int triangleType,
     float globalAngle,
     touchVertex vertexCorner,
     touchVertex vertexOpposite,
     touchVertex vertexAdjecent,
     Vector3 worldPos,
     GameObject gameOb)
 {
     type             = triangleType;
     trianglePosition = pos;
     angle            = globalAngle;
     corner           = vertexCorner;
     opposite         = vertexOpposite;
     adjecent         = vertexAdjecent;
     worldPosition    = worldPos;
     currentGameOb    = gameOb;
 }
    // Update is called once per frame
    void Update()
    {
        int i = 0;

        //Clean up lists if no touches
        if (Input.touchCount == 0)
        {
            touches.Clear();
            allTriangles.Clear();
        }

        //Debug.Log("Triangles in allTriangles: " + allTriangles.Count);

        //Handle removal of touches
        i = 0;
        while (i < Input.touchCount)
        {
            Touch t = Input.GetTouch(i);
            if (t.phase == TouchPhase.Ended)
            {
                //Find the touch and if part of triangle
                touchVertex    thisTouch    = touches.Find(touchVertex => touchVertex.touchId == t.fingerId);
                TriangleObject thisTriangle = allTriangles.Find(TriangleObject => TriangleObject.type == thisTouch.triangleIndex);

                //If part of triangle, delete that triangle
                if (thisTriangle != null)
                {
                    //temp: send away lable
                    if (thisTriangle.type == 2)
                    {
                        Wall.gameObject.SetActive(false);
                        //T2.transform.eulerAngles = new Vector3(0, 0, 0);
                        //T2.transform.position = new Vector2(0, 2000);
                    }
                    else if (thisTriangle.type == 3)
                    {
                        Turret.gameObject.SetActive(false);
                        //T3.transform.eulerAngles = new Vector3(0, 0, 0);
                        //T3.transform.position = new Vector2(0, 2000);
                    }
                    allTriangles.RemoveAt(allTriangles.IndexOf(thisTriangle));

                    //Find the other vertices of the triangle and remove them from that triangle object
                    List <touchVertex> otherTrignaleTouches = touches.FindAll(touchVertex => touchVertex.triangleIndex == thisTouch.triangleIndex);
                    if (otherTrignaleTouches != null)
                    {
                        for (int v = 0; v < otherTrignaleTouches.Count; v++)
                        {
                            //TriangleIndex = 0 => No triangle
                            touches[v].triangleIndex = 0;
                        }
                    }
                }
                //Remove the touch
                touches.RemoveAt(touches.IndexOf(thisTouch));
            }
            i++;
        }

        //Registrer all new touches
        i = 0;
        while (i < Input.touchCount)
        {
            Touch t = Input.GetTouch(i);
            if (t.phase == TouchPhase.Began)
            {
                touches.Add(new touchVertex(t.fingerId, t.position, 0));
            }
            i++;
        }

        //Create all possible vectors for vertices thats is not part of triangle
        List <touchVertex> allUnassigned = touches.FindAll(touchVertex => touchVertex.triangleIndex == 0);

        if (allUnassigned.Count > 2 && allTriangles.Count < 3)
        {
            CreateVectors();
        }


        //Create possible triangles
        if (allTriangles.Count < 3)
        {
            CreateTriangles();
        }



        // If touches are moved
        i = 0;
        while (i < Input.touchCount)
        {
            Touch       t         = Input.GetTouch(i);
            touchVertex thisTouch = touches.Find(touchVertex => touchVertex.touchId == t.fingerId);
            if (t.phase == TouchPhase.Moved)
            {
                thisTouch.position = t.position;
                UpdateTrianglePositions();
                UpdateTriangleAngle();
            }
            i++;

            /*
             *  TriangleObject one = allTriangles.Find(TriangleObject => TriangleObject.type == 1);
             *  if(one != null){
             *      //Debug.Log(one.trianglePosition);
             *      triangleLableOne.transform.position = one.trianglePosition;
             *  }
             */
        }
    }
    //Check the angles between possible vectors on a touchVertex
    void CheckAngle(touchVertex vertex)
    {
        //Debug.Log("Checking Angles");
        Vector2 cathetusOne;
        Vector2 cathetusTwo;

        if (vertex.triangleIndex == 0)
        {
            for (int i = 0; i < vertex.touchVectors.Count && Input.touchCount > 2; i++)
            {
                cathetusOne = vertex.position - vertex.touchVectors[i].position;
                for (int u = 0; u < vertex.touchVectors.Count && u != i; u++)
                {
                    cathetusTwo = vertex.position - vertex.touchVectors[u].position;
                    float angle = Vector2.Angle(cathetusOne, cathetusTwo);
                    Debug.Log("Angle" + angle);
                    if (angle < 110 && angle > 78)
                    {
                        Vector2 triPos = GetTrianglePosition(vertex.position, vertex.touchVectors[i].position, vertex.touchVectors[u].position);
                        //Debug.Log("Angle:" + angle);
                        if (cathetusOne.magnitude > cathetusTwo.magnitude)
                        {
                            int type = GetTriangleType(vertex, vertex.touchVectors[i], vertex.touchVectors[u]);
                            //Debug.Log(type);
                            vertex.triangleIndex = type;
                            vertex.touchVectors[i].triangleIndex = type;
                            vertex.touchVectors[u].triangleIndex = type;
                            float globalAngle = GetTriangleAngle(cathetusOne);
                            CreateTriangleObject(
                                triPos,
                                type,
                                globalAngle,
                                vertex,
                                vertex.touchVectors[i],
                                vertex.touchVectors[u]
                                );

                            //CreateTriangleObject(vertex, vertex.touchVectors[i], vertex.touchVectors[u]);
                        }
                        else
                        {
                            int type = GetTriangleType(vertex, vertex.touchVectors[u], vertex.touchVectors[i]);
                            //Debug.Log(type);
                            vertex.triangleIndex = type;
                            vertex.touchVectors[i].triangleIndex = type;
                            vertex.touchVectors[u].triangleIndex = type;
                            float globalAngle = GetTriangleAngle(cathetusTwo);
                            CreateTriangleObject(
                                triPos,
                                type,
                                globalAngle,
                                vertex,
                                vertex.touchVectors[i],
                                vertex.touchVectors[u]);
                            //CreateTriangleObject(vertex, vertex.touchVectors[u], vertex.touchVectors[i]);
                        }
                    }
                    else
                    {
                        //Debug.Log("Incorrect Angle:" + angle);
                        // Debug.Log(angle);
                    }
                    UpdateTrianglePositions();
                    UpdateTriangleAngle();
                }
            }
        }
    }